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 /ldblib.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 'ldblib.c')
-rw-r--r-- | ldblib.c | 18 |
1 files changed, 8 insertions, 10 deletions
@@ -21,10 +21,10 @@ | |||
21 | 21 | ||
22 | 22 | ||
23 | /* | 23 | /* |
24 | ** The hook table at registry[&HOOKKEY] maps threads to their current | 24 | ** The hook table at registry[HOOKKEY] maps threads to their current |
25 | ** hook function. (We only need the unique address of 'HOOKKEY'.) | 25 | ** hook function. |
26 | */ | 26 | */ |
27 | static const int HOOKKEY = 0; | 27 | static const char* HOOKKEY = "_HOOKKEY"; |
28 | 28 | ||
29 | 29 | ||
30 | /* | 30 | /* |
@@ -314,7 +314,7 @@ static int db_upvaluejoin (lua_State *L) { | |||
314 | static void hookf (lua_State *L, lua_Debug *ar) { | 314 | static void hookf (lua_State *L, lua_Debug *ar) { |
315 | static const char *const hooknames[] = | 315 | static const char *const hooknames[] = |
316 | {"call", "return", "line", "count", "tail call"}; | 316 | {"call", "return", "line", "count", "tail call"}; |
317 | lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); | 317 | lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); |
318 | lua_pushthread(L); | 318 | lua_pushthread(L); |
319 | if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ | 319 | if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ |
320 | lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ | 320 | lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ |
@@ -367,14 +367,12 @@ static int db_sethook (lua_State *L) { | |||
367 | count = (int)luaL_optinteger(L, arg + 3, 0); | 367 | count = (int)luaL_optinteger(L, arg + 3, 0); |
368 | func = hookf; mask = makemask(smask, count); | 368 | func = hookf; mask = makemask(smask, count); |
369 | } | 369 | } |
370 | if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { | 370 | if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) { |
371 | lua_createtable(L, 0, 2); /* create a hook table */ | 371 | /* table just created; initialize it */ |
372 | lua_pushvalue(L, -1); | ||
373 | lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ | ||
374 | lua_pushstring(L, "k"); | 372 | lua_pushstring(L, "k"); |
375 | lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ | 373 | lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ |
376 | lua_pushvalue(L, -1); | 374 | lua_pushvalue(L, -1); |
377 | lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ | 375 | lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */ |
378 | } | 376 | } |
379 | checkstack(L, L1, 1); | 377 | checkstack(L, L1, 1); |
380 | lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ | 378 | lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ |
@@ -396,7 +394,7 @@ static int db_gethook (lua_State *L) { | |||
396 | else if (hook != hookf) /* external hook? */ | 394 | else if (hook != hookf) /* external hook? */ |
397 | lua_pushliteral(L, "external hook"); | 395 | lua_pushliteral(L, "external hook"); |
398 | else { /* hook table must exist */ | 396 | else { /* hook table must exist */ |
399 | lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); | 397 | lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); |
400 | checkstack(L, L1, 1); | 398 | checkstack(L, L1, 1); |
401 | lua_pushthread(L1); lua_xmove(L1, L, 1); | 399 | lua_pushthread(L1); lua_xmove(L1, L, 1); |
402 | lua_rawget(L, -2); /* 1st result = hooktable[L1] */ | 400 | lua_rawget(L, -2); /* 1st result = hooktable[L1] */ |