Skip to content

Commit ec13a89

Browse files
committedNov 11, 2013
bugfix: lua-bson , don't dec index when pack dict
1 parent ffd4863 commit ec13a89

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed
 

‎lualib-src/lua-bson.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ write_double(struct bson *b, lua_Number d) {
247247
}
248248
}
249249

250-
static void pack_dict(lua_State *L, struct bson *b);
250+
static void pack_dict(lua_State *L, struct bson *b, bool array);
251251

252252
static inline void
253253
append_key(struct bson *bs, int type, const char *key, size_t sz) {
@@ -291,7 +291,7 @@ append_table(struct bson *bs, lua_State *L, const char *key, size_t sz) {
291291
} else {
292292
append_key(bs, BSON_DOCUMENT, key, sz);
293293
}
294-
pack_dict(L, bs);
294+
pack_dict(L, bs, isarray);
295295
}
296296

297297
static void
@@ -414,7 +414,8 @@ bson_numstr( char *str, int i ) {
414414
}
415415

416416
static void
417-
pack_dict(lua_State *L, struct bson *b) {
417+
pack_dict(lua_State *L, struct bson *b, bool isarray) {
418+
int arraydec = isarray ? 1 : 0;
418419
int length = reserve_length(b);
419420
lua_pushnil(L);
420421
while(lua_next(L,-2) != 0) {
@@ -424,7 +425,7 @@ pack_dict(lua_State *L, struct bson *b) {
424425
size_t sz;
425426
switch(kt) {
426427
case LUA_TNUMBER:
427-
sz = bson_numstr(numberkey, lua_tointeger(L,-2)-1);
428+
sz = bson_numstr(numberkey, lua_tointeger(L,-2)-arraydec);
428429
key = numberkey;
429430
break;
430431
case LUA_TSTRING:
@@ -794,7 +795,7 @@ lencode(lua_State *L) {
794795
bson_create(&b);
795796
lua_settop(L,1);
796797
luaL_checktype(L, 1, LUA_TTABLE);
797-
pack_dict(L,&b);
798+
pack_dict(L, &b, false);
798799
void * ud = lua_newuserdata(L, b.size);
799800
memcpy(ud, b.ptr, b.size);
800801
bson_destroy(&b);
@@ -1132,4 +1133,3 @@ luaopen_bson(lua_State *L) {
11321133

11331134
return 1;
11341135
}
1135-

0 commit comments

Comments
 (0)