lua-users home
lua-l archive

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


Hello list:

While using luaL_Buffer, I find something weird: when I
init a luaL_Buffer, add a large string into it, and then
remove the bottom element of the stack, an exception
"attempt to call a nil value" throws.

Here is the code(compiled with lua 5.4.3):

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>

int
main(int argc, char **argv)
{
  char dummy[1025];
  lua_State *L;
  luaL_Buffer b;

  L = luaL_newstate();
  luaL_openlibs(L);

  lua_pushliteral(L, "nothing special");
  luaL_buffinit(L, &b);
  luaL_addlstring(&b, dummy, 1025);
  lua_remove(L, 1);

  return 0;
}

Jump into the codebase and I find that after adding a large string
into luaL_Buffer, the internal buffer is replaced by an UBox which
is made to be a tbc value and is linked into L->tbclist, However,
when I call lua_remove(), it rotates the bottom element to the top
but forgets(?) to update L->tbclist, which still points to the top
of the stack, and then lua_pop() pops the top element, in which it
tries to call the nonexistent "__close" metamethod of the element
and thus throws an exception.

I think it is a bug but not absolutely sure about that, because
rotating the stack is so common that I cannot imagine what other
tbc values will be(I haven't read the whole codebase yet :)), and
btw, the usage of luaL_Buffer is also a little weird thus maybe I
just missed something. Wish someone can help me with this problem.


-- balus