lua-users home
lua-l archive

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


On Thu, Oct 28, 2021 at 1:26 PM aman agrawal <aman.161089@gmail.com> wrote:
#0  0x0000000800b5b0ba in thr_kill () from /lib/libc.so.7
(gdb) bt
#0  0x0000000800b5b0ba in thr_kill () from /lib/libc.so.7
#1  0x0000000800b5b084 in raise () from /lib/libc.so.7
#2  0x0000000800b5aff9 in abort () from /lib/libc.so.7
#3  0x0000000000407df3 in luaD_throw ()
#4  0x00000000004072c7 in luaG_errormsg ()
#5  0x0000000000406e71 in luaG_runerror ()
#6  0x0000000000406b8f in luaG_typeerror ()
#7  0x0000000000418116 in luaV_finishget ()
#8  0x0000000000403f73 in lua_getglobal ()
#9  0x0000000000402651 in main ()
(gdb)

Can anyone please tell what I'm doing wrong here?

If I removed following lines, the program doesn't crash:

  lua_pushinteger(L, LUA_RIDX_GLOBALS);
         lua_insert(L, -2);
         lua_settable(L, LUA_REGISTRYINDEX);

Lua stores the actual table of global variables that it directly accesses at index LUA_RIDX_GLOBALS in the Lua registry. Here, you are replacing that table with something else, namely the second return value from read_only_global_env.lua. But that file only returns one value, so the second return value that you asked for in lua_pcall is forced to nil. So you are replacing the canonical global table with nil, hence it logically follows that lua_getglobal will throw an error for attempting to index a nil value (because that's exactly what it does).