diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-19 13:14:06 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-19 13:14:06 -0300 |
commit | 3c0d3c6fbeea18f257102c62a01b036c7a5c5161 (patch) | |
tree | 138a90dc27593aaf4e873ea8aca5b7d887084e1a /loadlib.c | |
parent | 440a5ee78c8592230780310c9c8d8c2ba1700cb1 (diff) | |
download | lua-3c0d3c6fbeea18f257102c62a01b036c7a5c5161.tar.gz lua-3c0d3c6fbeea18f257102c62a01b036c7a5c5161.tar.bz2 lua-3c0d3c6fbeea18f257102c62a01b036c7a5c5161.zip |
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).
Diffstat (limited to 'loadlib.c')
-rw-r--r-- | loadlib.c | 11 |
1 files changed, 5 insertions, 6 deletions
@@ -56,10 +56,10 @@ | |||
56 | 56 | ||
57 | 57 | ||
58 | /* | 58 | /* |
59 | ** unique key for table in the registry that keeps handles | 59 | ** key for table in the registry that keeps handles |
60 | ** for all loaded C libraries | 60 | ** for all loaded C libraries |
61 | */ | 61 | */ |
62 | static const int CLIBS = 0; | 62 | static const char *CLIBS = "_CLIBS"; |
63 | 63 | ||
64 | #define LIB_FAIL "open" | 64 | #define LIB_FAIL "open" |
65 | 65 | ||
@@ -327,7 +327,7 @@ static void setpath (lua_State *L, const char *fieldname, | |||
327 | */ | 327 | */ |
328 | static void *checkclib (lua_State *L, const char *path) { | 328 | static void *checkclib (lua_State *L, const char *path) { |
329 | void *plib; | 329 | void *plib; |
330 | lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); | 330 | lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); |
331 | lua_getfield(L, -1, path); | 331 | lua_getfield(L, -1, path); |
332 | plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ | 332 | plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ |
333 | lua_pop(L, 2); /* pop CLIBS table and 'plib' */ | 333 | lua_pop(L, 2); /* pop CLIBS table and 'plib' */ |
@@ -340,7 +340,7 @@ static void *checkclib (lua_State *L, const char *path) { | |||
340 | ** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries | 340 | ** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries |
341 | */ | 341 | */ |
342 | static void addtoclib (lua_State *L, const char *path, void *plib) { | 342 | static void addtoclib (lua_State *L, const char *path, void *plib) { |
343 | lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); | 343 | lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); |
344 | lua_pushlightuserdata(L, plib); | 344 | lua_pushlightuserdata(L, plib); |
345 | lua_pushvalue(L, -1); | 345 | lua_pushvalue(L, -1); |
346 | lua_setfield(L, -3, path); /* CLIBS[path] = plib */ | 346 | lua_setfield(L, -3, path); /* CLIBS[path] = plib */ |
@@ -716,12 +716,11 @@ static void createsearcherstable (lua_State *L) { | |||
716 | ** setting a finalizer to close all libraries when closing state. | 716 | ** setting a finalizer to close all libraries when closing state. |
717 | */ | 717 | */ |
718 | static void createclibstable (lua_State *L) { | 718 | static void createclibstable (lua_State *L) { |
719 | lua_newtable(L); /* create CLIBS table */ | 719 | luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ |
720 | lua_createtable(L, 0, 1); /* create metatable for CLIBS */ | 720 | lua_createtable(L, 0, 1); /* create metatable for CLIBS */ |
721 | lua_pushcfunction(L, gctm); | 721 | lua_pushcfunction(L, gctm); |
722 | lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ | 722 | lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ |
723 | lua_setmetatable(L, -2); | 723 | lua_setmetatable(L, -2); |
724 | lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */ | ||
725 | } | 724 | } |
726 | 725 | ||
727 | 726 | ||