lua-users home
lua-l archive

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


On Thu, Nov 2, 2017 at 1:53 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> It is there for greater efficiency.

Efficiency of what? Everything that luaL_Buffer and Co does can be done using the non-auxiliary API. It does not use any magic that is unavailable to users otherwise.

> Oh, that is overstating the case. The public (or at least that section of it that can write code in the Lua C API) is not so delicate.

In the context of this thread, where the original poster, who is not a Lua neophyte, wonders whether the use of luaL_Buffer can straddle the C/Lua interface, this is a very strange statement.

> What I envisaged in my earlier reply is in fact quite easy, almost trivial. See attachment. (Lua 5.3).

And your code is proof enough that you, another experienced Lua user, misunderstood both the manual and my explanation, because it has exactly the problem I wrote about and it nicely demonstrates "it works when I test it, but it crashes randomly
in production, and no one knows why". 

Here is my test of your code:

lua_State *L = luaL_newstate();

luaL_openlibs(L);
luaopen_buffer(L);
lua_setglobal(L, "b");
luaL_dostring(L,
"local x = b.new()"
"b.append(x, string.rep('x', 100000))"
"b.append(x, string.rep('x', 100000))"
"print(b.flush(x))"
);

When I run the above, it crashes within the second call to luaL_addvalue(), due to a heap corruption. If I remove the second b.append... line, then it crashes within luaL_pushresult(), for the same reason.

Exercise for the reader: explain why.

Hint: set up a breakpoint in boxgc() in the lauxlib.c.

Cheers,
V.