lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> This version incorrectly changes the value at index 2 to an empty
> string (in unrope). This may cause memcpy to copy from a NULL pointer,
> although my operating system's implementation of memcpy cowardly
> avoids this.

Thanks for spotting a problem there, though I don't actually get your
explanation. What I see is that nil (or any other non-string value)
would be accepted as a 2nd arg and then sent to luaL_addvalue, where it
would be converted to NULL and then potentially crash the program, as you
point out.

> I think there also may be a problem with the stack not being large
> enough to accommodate the luaL_Buffer but I have not explored this yet.

Ignacio has spotted that. I hope the version below fixes all this...
(I really should not debug my code in public like that :-)

static int unrope (lua_State *L) {
  luaL_Buffer b;
  luaL_checktype(L,1,LUA_TTABLE);
  lua_settop(L,2);
  if (lua_isnil(L,2)) {lua_pushliteral(L,""); lua_replace(L,2);} 
  else luaL_checkstring(L,2);
  lua_insert(L,1);
  luaL_checkstack(L,MAXLEVEL-1+LUA_MINSTACK,"cannot grow stack");
  lua_settop(L,MAXLEVEL+1);
  luaL_buffinit(L,&b);
  doconcat(L,&b,2,0);
  luaL_pushresult(&b);
  return 1;
}

Anyway, the point of the code was to promote unrope. But perhaps the bad code
has given unrope a bad name! (I hope not because ropes are nice and easy to
build and use in Lua; unrope was just meant as an efficient concat for ropes,
though it is a bit tricky to get right...)

Thanks for all the feedback.