[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: A question regarding the use of luaL_Buffer in Lua 5.4
- From: Marc Balmer <marc@...>
- Date: Mon, 11 Jan 2021 09:16:56 +0100
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