Hello,
A memory leak is happening on execution of a Lua chunk below.
It is reproduced on the latest version of PUC Rio Lua (104b0fc7008b1f6b7d818985fbbad05cd37ee654)
that built with enabled AddressSanitizer instrumentation.
How to reproduce:
1. git clone
https://github.com/lua/lua2. cd lua
3. make MYCFLAGS="-O0 -ggdb3 -g -fsanitize=address" MYLDFLAGS=-fsanitize=address -j
4. create a C file:
cat << EOF > loadstring_memleak.c
#include <stdio.h>
#include <assert.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
int sz = 2048;
char buf[sz];
FILE *file = fopen("chunk.txt", "rb");
assert(file);
fread(buf, sizeof(char), sz, file);
luaL_loadstring(L, buf);
lua_pcall(L, 0, 0, 0);
fclose(file);
lua_settop(L, 0);
lua_close(L);
return 0;
}
EOF
5. Build: clang loadstring_memleak.c -g -ggdb3 -O0 -o loadstring_memleak -I. -L. -llua -lm -fsanitize=address
6. Download a chunk.txt (also attached): curl -O
https://gist.githubusercontent.com/ligurio/79f40162cece323ae930c144088f9083/raw/9edaeeef81e5408c23107c70f949c4ba5685108b/chunk.txt7. Execute: ./loadstring_memleak
Full report:
=================================================================
==3099029==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 1536 byte(s) in 1 object(s) allocated from:
#0 0x558812989840 in realloc (/home/sergeyb/sources/cache/lua/loadstring_memleak+0xdb840) (BuildId: bc1d2d6bc5ce0e295bdbeafcd4aba1c3941dde60)
#1 0x558812a338c1 in luaL_alloc /home/sergeyb/sources/cache/lua/lauxlib.c:1056:12
#2 0x558812a30faa in resizebox /home/sergeyb/sources/cache/lua/lauxlib.c:491:18
#3 0x558812a314b2 in prepbuffsize /home/sergeyb/sources/cache/lua/lauxlib.c:580:25
#4 0x558812a315d9 in luaL_prepbuffsize /home/sergeyb/sources/cache/lua/lauxlib.c:593:10
#5 0x558812a5691b in str_gsub /home/sergeyb/sources/cache/lua/lstrlib.c:975:7
#6 0x5588129dbd76 in precallC /home/sergeyb/sources/cache/lua/ldo.c:644:8
#7 0x5588129dc61d in luaD_precall /home/sergeyb/sources/cache/lua/ldo.c:713:7
#8 0x558812a2b502 in luaV_execute /home/sergeyb/sources/cache/lua/lvm.c:1729:22
#9 0x5588129dcc83 in ccall /home/sergeyb/sources/cache/lua/ldo.c:755:5
#10 0x5588129dcd35 in luaD_callnoyield /home/sergeyb/sources/cache/lua/ldo.c:773:3
#11 0x5588129d7748 in luaG_errormsg /home/sergeyb/sources/cache/lua/ldebug.c:847:5
#12 0x5588129d0cc5 in lua_error /home/sergeyb/sources/cache/lua/lapi.c:1261:5
#13 0x558812a45cac in luaB_error /home/sergeyb/sources/cache/lua/lbaselib.c:124:10
#14 0x558812a46c3a in luaB_assert /home/sergeyb/sources/cache/lua/lbaselib.c:443:12
#15 0x5588129dbd76 in precallC /home/sergeyb/sources/cache/lua/ldo.c:644:8
#16 0x5588129dc61d in luaD_precall /home/sergeyb/sources/cache/lua/ldo.c:713:7
#17 0x558812a2b502 in luaV_execute /home/sergeyb/sources/cache/lua/lvm.c:1729:22
#18 0x5588129dcc83 in ccall /home/sergeyb/sources/cache/lua/ldo.c:755:5
#19 0x5588129dcd35 in luaD_callnoyield /home/sergeyb/sources/cache/lua/ldo.c:773:3
#20 0x5588129d7748 in luaG_errormsg /home/sergeyb/sources/cache/lua/ldebug.c:847:5
#21 0x5588129d0cc5 in lua_error /home/sergeyb/sources/cache/lua/lapi.c:1261:5
#22 0x558812a45cac in luaB_error /home/sergeyb/sources/cache/lua/lbaselib.c:124:10
#23 0x558812a46c3a in luaB_assert /home/sergeyb/sources/cache/lua/lbaselib.c:443:12
#24 0x5588129dbd76 in precallC /home/sergeyb/sources/cache/lua/ldo.c:644:8
#25 0x5588129dc61d in luaD_precall /home/sergeyb/sources/cache/lua/ldo.c:713:7
#26 0x558812a2b502 in luaV_execute /home/sergeyb/sources/cache/lua/lvm.c:1729:22
#27 0x5588129dcc83 in ccall /home/sergeyb/sources/cache/lua/ldo.c:755:5
#28 0x5588129dcd35 in luaD_callnoyield /home/sergeyb/sources/cache/lua/ldo.c:773:3
#29 0x5588129d7748 in luaG_errormsg /home/sergeyb/sources/cache/lua/ldebug.c:847:5
SUMMARY: AddressSanitizer: 1536 byte(s) leaked in 1 allocation(s).
Sergey