From d6eb49e4eb05fd8fcade50ca965aa17011530d2c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 4 Jul 2017 21:32:04 +0800 Subject: [PATCH] bson dictionary's key can't be number --- lualib-src/lua-bson.c | 9 +++------ test/testbson.lua | 12 ++++++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 76daef40..eacecf3c 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -415,6 +415,8 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) append_key(bs, L, BSON_BOOLEAN, key, sz); write_byte(bs, lua_toboolean(L,-1)); break; + case LUA_TNIL: + luaL_error(L, "Bson array has a hole (nil), Use bson.null instead"); default: luaL_error(L, "Invalid value type : %s", lua_typename(L,vt)); } @@ -452,12 +454,7 @@ pack_dict_data(lua_State *L, struct bson *b, int depth, int kt) { size_t sz; switch(kt) { case LUA_TNUMBER: - // copy key, don't change key type - lua_pushvalue(L,-2); - lua_insert(L,-2); - key = lua_tolstring(L,-2,&sz); - append_one(b, L, key, sz, depth); - lua_pop(L,2); + luaL_error(L, "Bson dictionary's key can't be number"); break; case LUA_TSTRING: key = lua_tolstring(L,-2,&sz); diff --git a/test/testbson.lua b/test/testbson.lua index 815ffdef..214d70cd 100644 --- a/test/testbson.lua +++ b/test/testbson.lua @@ -22,9 +22,9 @@ end local obj_a = { __data = { - [1] = 2, - [3] = 4, - [5] = 6, + ["1"] = 2, + ["3"] = 4, + ["5"] = 6, } } @@ -38,9 +38,9 @@ setmetatable( local obj_b = { __data = { - [7] = 8, - [9] = 10, - [11] = obj_a, + ["7"] = 8, + ["9"] = 10, + ["11"] = obj_a, } }