Avoid using addresses of static variables as unique keys
The addresses of static variables may be different for different instances of Lua, making these instances incompatible if they use these addresses as unique keys in the registry (or other tables).
This commit is contained in:
11
loadlib.c
11
loadlib.c
@@ -56,10 +56,10 @@
|
||||
|
||||
|
||||
/*
|
||||
** unique key for table in the registry that keeps handles
|
||||
** key for table in the registry that keeps handles
|
||||
** for all loaded C libraries
|
||||
*/
|
||||
static const int CLIBS = 0;
|
||||
static const char *CLIBS = "_CLIBS";
|
||||
|
||||
#define LIB_FAIL "open"
|
||||
|
||||
@@ -327,7 +327,7 @@ static void setpath (lua_State *L, const char *fieldname,
|
||||
*/
|
||||
static void *checkclib (lua_State *L, const char *path) {
|
||||
void *plib;
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_getfield(L, -1, path);
|
||||
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
||||
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
||||
@@ -340,7 +340,7 @@ static void *checkclib (lua_State *L, const char *path) {
|
||||
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
||||
*/
|
||||
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_pushlightuserdata(L, plib);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||
@@ -716,12 +716,11 @@ static void createsearcherstable (lua_State *L) {
|
||||
** setting a finalizer to close all libraries when closing state.
|
||||
*/
|
||||
static void createclibstable (lua_State *L) {
|
||||
lua_newtable(L); /* create CLIBS table */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
||||
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
||||
lua_pushcfunction(L, gctm);
|
||||
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
||||
lua_setmetatable(L, -2);
|
||||
lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user