lua-users home
lua-l archive

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


Never mind, I found the root case.  Intermittent use of luaL_Buffers is not possible.

- mb


> Am 11.01.2021 um 09:16 schrieb Marc Balmer <marc@msys.ch>:
> 
> I have a C program that uses two luaL_Buffer variables to build up content dynamically.  I init both, then I write something to buffer B, after that, buffer A gets filled by repeated calls to luaL_addchar, in the end I push both buffers and out them:
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> #include <lua.h>
> #include <lauxlib.h>
> 
> int
> main(int argc, char *argv[])
> {
>        lua_State *L;
>        luaL_Buffer A, B;
>        int count, n;
> 
>        count = atoi(argv[1]);
> 
>        L = luaL_newstate();
>        luaL_buffinit(L, &A);
>        luaL_buffinit(L, &B);
> 
>        luaL_addstring(&B, "_ENV = ...\n");
> 
>        for (n = 0; n < count; n++)
>                luaL_addchar(&A, 'N');
>        luaL_addchar(&A, '\n');
> 
>        luaL_pushresult(&B);
>        luaL_pushresult(&A);
> 
>        printf("Lua buffer A:\n");
>        printf("%s\n", lua_tostring(L, -1));
> 
>        printf("Lua buffer B:\n");
>        printf("%s\n", lua_tostring(L, -2)); /* this fails if count is >= 1024 */
> 
>        lua_close(L);
>        return 0;
> }
> 
> This works up to a certain number of calls to luaL_addchar(&A, 'N').  In my case, if count is 1024 or greater, I get a segmentation fault.
> 
> The real world code where this happens in the reader() function in https://github.com/arcapos/luatemplate/blob/master/reader.c where two buffers are used to build two parts of Lua source code.
> 
> The second call to luaL_pushresult() seems to somehow "destroy" what the first call to luaL_pushresult() did put on the stack.
> 
> It happens only on Lua 5.4, but not on Lua 5.3, apparently.
> 
> fwiw, I used a RHEL 8 system for testing.
> 
> Any ideas or suggestions?
> 
> - mb