From f80e2cd9b19c5478d63f7da4905fc121fe6aa5ea Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 13 Oct 2021 12:57:55 +0800 Subject: [PATCH] bugfix: See #1484 --- lualib-src/lua-bson.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 9d20242a..517fd40e 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -512,16 +512,25 @@ pack_meta_dict(lua_State *L, struct bson *b, int depth) { static bool is_rawarray(lua_State *L) { - size_t len = lua_rawlen(L, -1); - if (len > 0) { - lua_pushinteger(L, len); - if (lua_next(L,-2) == 0) { - return true; - } else { - lua_pop(L,2); - } + // test first key, hash part first in Lua 5.4 + lua_pushnil(L); + if (lua_next(L, -2) == 0) { + // empty table + return false; + } + lua_Integer firstkey = lua_tointeger(L, -2); + lua_pop(L, 2); + if (firstkey != 1) + return false; + + size_t len = lua_rawlen(L, -1); + lua_pushinteger(L, len); + if (lua_next(L,-2) == 0) { + return true; + } else { + lua_pop(L,2); + return false; } - return false; } static void