lua-users home
lua-l archive

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


Hi,

Following program is crashing at lua_getglobal().

[Aman_freebsd114:/var/views/lua/lua-5.4.3/src]# cat test_global.c
#include <lua.h>                                /* Always include this when calling Lua */
#include <lauxlib.h>                            /* Always include this when calling Lua */
#include <lualib.h>                             /* Prototype for luaL_openlibs(), */
#include <lstate.h>
#include <assert.h>
/*   always include
 *    *   this when calling
 *     *   Lua */

#include <stdlib.h>                             /* For function exit() */
#include <stdio.h>                              /* For input/output */

void bail(lua_State *L, char *msg){
        fprintf(stderr, "\nFATAL ERROR:\n  %s: %s\n\n",
                        msg, lua_tostring(L, -1));
        exit(1);
}

#define LUA_LIB_ABCD "abcd"
#define LUA_REG_UTYPE_DYN_DEF "abcd.dyn.def"


static const struct luaL_Reg lua_lib_abcd [] = {
         {NULL, NULL} // sentinel
};

typedef struct handler_conf {
        int a;
        int b;
        int c;
        int d;
} handler_conf_t;

#define LUA_LIB_DEF         "def"
handler_conf_t *handlers;
int main(void)
{
        lua_State *L;

        L = luaL_newstate();                        /* Create Lua state variable */
        luaL_openlibs(L);                           /* Load Lua libraries */

        luaL_newlib(L, lua_lib_abcd);
        lua_pushvalue(L, -1);
        lua_setglobal (L, LUA_LIB_ABCD);

        void ***handler_init = lua_newuserdata(L,  sizeof(void **));
        *handler_init = (void *)&handlers;
        luaL_setmetatable(L, LUA_REG_UTYPE_DYN_DEF);
        lua_setglobal(L, LUA_LIB_DEF);
        if (luaL_loadfile(L, "read_only_global_env.lua")) {
                bail(L, "luaL_loadfile() failed");
        }

        if (lua_pcall(L, 0, 2, 0)) {
                bail(L, "lua_pcall() failed");
        }


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


        lua_getglobal(L, LUA_LIB_DEF);

        lua_close(L);                               /* Clean up, free the Lua state var */
        return 0;
}


[Aman_freebsd114:/var/views/lua/lua-5.4.3/src]# cc -o test_global test_global.c -Wall -I /var/views/lua/lua-5.4.3/src/ -L/var/views/lua/lua-5.4.3/src/ -lm -llua
[Aman_freebsd114:/var/views/lua/lua-5.4.3/src]# ./test_global
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
Abort trap (core dumped)
[Aman_freebsd114:/var/views/lua/lua-5.4.3/src]# gdb test_global test_global.core
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "amd64-marcel-freebsd"...(no debugging symbols found)...
Core was generated by `./test_global'.
Program terminated with signal 6, Aborted.
Reading symbols from /lib/libm.so.5...(no debugging symbols found)...done.
Loaded symbols for /lib/libm.so.5
Reading symbols from /lib/libc.so.7...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.7
Reading symbols from /libexec/ld-elf.so.1...(no debugging symbols found)...done.
Loaded symbols for /libexec/ld-elf.so.1
#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);


I also included "assert.h" file in lua.h.
--
Best Regards
Aman Agrawal

Attachment: read_only_global_env.lua
Description: Binary data