Skip to content

Commit bf6501f

Browse files
committed
check mongo reply data stream
1 parent f92fc15 commit bf6501f

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lualib-src/lua-bson.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ struct bson_reader {
6868
int size;
6969
};
7070

71+
static inline int32_t
72+
get_length(const uint8_t * data) {
73+
const uint8_t * b = (const uint8_t *)data;
74+
int32_t len = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
75+
return len;
76+
}
77+
7178
static inline void
7279
bson_destroy(struct bson *b) {
7380
if (b->ptr != b->buffer) {
@@ -644,7 +651,7 @@ static int
644651
lmakeindex(lua_State *L) {
645652
int32_t *bson = luaL_checkudata(L,1,"bson");
646653
const uint8_t * start = (const uint8_t *)bson;
647-
struct bson_reader br = { start+4, *bson - 5 };
654+
struct bson_reader br = { start+4, get_length(start) - 5 };
648655
lua_newtable(L);
649656

650657
for (;;) {
@@ -820,7 +827,9 @@ ldecode(lua_State *L) {
820827
if (data == NULL) {
821828
return 0;
822829
}
823-
struct bson_reader br = { (const uint8_t *)data , *data };
830+
const uint8_t * b = (const uint8_t *)data;
831+
int32_t len = get_length(b);
832+
struct bson_reader br = { b , len };
824833

825834
unpack_dict(L, &br, false);
826835

lualib-src/lua-mongo.c

+7
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ op_reply(lua_State *L) {
260260
lua_pushnil(L);
261261
lua_rawseti(L, 2, i);
262262
}
263+
} else {
264+
if (sz >= 4) {
265+
sz -= get_length((document)doc);
266+
}
267+
}
268+
if (sz != 0) {
269+
return luaL_error(L, "Invalid result bson document");
263270
}
264271
lua_pushboolean(L,1);
265272
lua_pushinteger(L, id);

lualib/mongo.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function mongo.client( conf )
9898
host = obj.host,
9999
port = obj.port,
100100
response = dispatch_reply,
101-
auth = mongo_auth(conf),
101+
auth = mongo_auth(obj),
102102
}
103103
setmetatable(obj, client_meta)
104104
obj.__sock:connect(true) -- try connect only once

0 commit comments

Comments
 (0)