summaryrefslogtreecommitdiff
path: root/lua_cjson.c
diff options
context:
space:
mode:
authorThibault Charbonnier <thibaultcha@me.com>2016-12-16 18:27:49 -0800
committerYichun Zhang (agentzh) <agentzh@gmail.com>2016-12-17 20:14:47 -0800
commita61d7840ba2d79df8cb73d8c864a99d0eb06d580 (patch)
treed410e0db9567b283e801ef4cb0e1b15517601da1 /lua_cjson.c
parenta18d6999d82ba34493892a609c00937b81c84355 (diff)
downloadlua-cjson-a61d7840ba2d79df8cb73d8c864a99d0eb06d580.tar.gz
lua-cjson-a61d7840ba2d79df8cb73d8c864a99d0eb06d580.tar.bz2
lua-cjson-a61d7840ba2d79df8cb73d8c864a99d0eb06d580.zip
bugfix: preserve 'empty_array_mt' behavior upon multiple loadings of the module.
Prior to this fix, when the module would be loaded several times (by-passing `package.loaded`), the `lua_cjson_new` function would override the `empty_array_mt` table in the registry with a new one. Comparison for equality between those tables would then fail, and the behavior would be broken. This was discovered after loading `cjson` *and* `cjson.safe` in the same application, resulting in two calls to `lua_cjson_new`. Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
Diffstat (limited to 'lua_cjson.c')
-rw-r--r--lua_cjson.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/lua_cjson.c b/lua_cjson.c
index 3a77644..1695979 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -1401,10 +1401,21 @@ static int lua_cjson_new(lua_State *l)
1401 /* Initialise number conversions */ 1401 /* Initialise number conversions */
1402 fpconv_init(); 1402 fpconv_init();
1403 1403
1404 /* Create empty array metatable */ 1404 /* Test if empty array metatable is in registry */
1405 lua_pushlightuserdata(l, &json_empty_array); 1405 lua_pushlightuserdata(l, &json_empty_array);
1406 lua_newtable(l); 1406 lua_rawget(l, LUA_REGISTRYINDEX);
1407 lua_rawset(l, LUA_REGISTRYINDEX); 1407 if (lua_isnil(l, -1)) {
1408 /* Create empty array metatable.
1409 *
1410 * If multiple calls to lua_cjson_new() are made,
1411 * this prevents overriding the table at the given
1412 * registry's index with a new one.
1413 */
1414 lua_pop(l, 1);
1415 lua_pushlightuserdata(l, &json_empty_array);
1416 lua_newtable(l);
1417 lua_rawset(l, LUA_REGISTRYINDEX);
1418 }
1408 1419
1409 /* cjson module table */ 1420 /* cjson module table */
1410 lua_newtable(l); 1421 lua_newtable(l);