lua-users home
lua-l archive

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


2017-11-03 7:50 GMT+02:00 Dirk Laurie <dirk.laurie@gmail.com>:
> 2017-11-02 17:42 GMT+02:00 Viacheslav Usov <via.usov@gmail.com>:
>> 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.
>
> If by "C/Lua interface" you include calling Lua from C, then you are
> out of my league. You are in effect writing your own Lua interpreter.
> Of course extra precautions to conserve stack integrity are needed, as
> the manual states.
>
>>> 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.
>
> I will be more impressed if you can crash my code by using it as a
> module of C routines to be called from Lua.

OK, I can spare you the effort. If I run your code line by line in the
interpreter (without 'local'), it is fine. If I put it inside do ...
end,
it crashes spectacularly. You are right.

The reason it is in the API must be that it is needed to code the
standard library, which prides itself on being totally written in the
API.