Permalink
Browse files

+ Add cyclic reference checks in BSON building

+ Add support for BSON.BinData
+ Consistency & security checks:
  * Avoid long jumps from luaL_error which caused memory leaks
  * Check string pointers before use
  • Loading branch information...
1 parent 2891030 commit 3f3c78fd8d3501b249f6731e3373b34ee6723126 @neoxic neoxic committed Jan 19, 2012
Showing with 125 additions and 132 deletions.
  1. +41 −55 mongo_bsontypes.cpp
  2. +1 −1 mongo_gridfile.cpp
  3. +83 −76 utils.cpp
View
@@ -29,7 +29,8 @@ static int bson_type_Date(lua_State *L) {
static int bson_type_Timestamp(lua_State*L) {
push_bsontype_table(L, mongo::Timestamp);
- // no arg
+ lua_pushvalue(L, 1);
+ lua_rawseti(L, -2, 1); // t[1] = function arg #1
return 1;
}
@@ -63,6 +64,13 @@ static int bson_type_Symbol(lua_State *L) {
return 1;
}
+static int bson_type_BinData(lua_State *L) {
+ push_bsontype_table(L, mongo::BinData);
+ lua_pushvalue(L, 1);
+ lua_rawseti(L, -2, 1); // t[1] = function arg #1
+ return 1;
+}
+
static int bson_type_ObjectID(lua_State *L) {
if(lua_gettop(L) == 0)
lua_pushstring(L, mongo::OID::gen().toString().data());
@@ -79,88 +87,63 @@ static int bson_type_NULL(lua_State *L) {
}
static int integer_value(lua_State *L) {
- int n = lua_gettop(L);
- int returncount = 1;
-
- lua_rawgeti(L, 1, 1);
-
- if (n > 1) {
- lua_pushinteger(L, luaL_checkint(L, 2));
+ if (lua_gettop(L) > 1) {
+ luaL_checkint(L, 2);
+ lua_pushvalue(L, 2);
lua_rawseti(L, 1, 1);
- returncount = 0;
- } else {
- lua_pushinteger(L, luaL_checkint(L, -1));
+ return 0;
}
-
- return returncount;
+ lua_rawgeti(L, 1, 1);
+ return 1;
}
static int number_value(lua_State *L) {
- int n = lua_gettop(L);
- int returncount = 1;
-
- lua_rawgeti(L, 1, 1);
-
- if (n > 1) {
- lua_pushnumber(L, luaL_checknumber(L, 2));
+ if (lua_gettop(L) > 1) {
+ luaL_checknumber(L, 2);
+ lua_pushvalue(L, 2);
lua_rawseti(L, 1, 1);
- returncount = 0;
- } else {
- lua_pushnumber(L, luaL_checknumber(L, -1));
+ return 0;
}
-
- return returncount;
+ lua_rawgeti(L, 1, 1);
+ return 1;
}
static int string_value(lua_State *L) {
- int n = lua_gettop(L);
- int returncount = 1;
-
- lua_rawgeti(L, 1, 1);
-
- if (n > 1) {
- lua_pushstring(L, luaL_checkstring(L, 2));
+ if (lua_gettop(L) > 1) {
+ luaL_checkstring(L, 2);
+ lua_pushvalue(L, 2);
lua_rawseti(L, 1, 1);
- returncount = 0;
- } else {
- lua_pushstring(L, luaL_checkstring(L, -1));
+ return 0;
}
-
- return returncount;
+ lua_rawgeti(L, 1, 1);
+ return 1;
}
static int null_value(lua_State *L) {
lua_pushnil(L);
-
return 1;
}
static int stringpair_value(lua_State *L) {
- int n = lua_gettop(L);
- int returncount = 2;
-
- lua_rawgeti(L, 1, 1);
- lua_rawgeti(L, 1, 2);
-
- if (n > 1) {
- lua_pushstring(L, luaL_checkstring(L, 2));
+ if (lua_gettop(L) > 1) {
+ luaL_checkstring(L, 2);
+ luaL_checkstring(L, 3);
+ lua_pushvalue(L, 2);
lua_rawseti(L, 1, 1);
- lua_pushstring(L, luaL_checkstring(L, 3));
+ lua_pushvalue(L, 3);
lua_rawseti(L, 1, 2);
- returncount = 0;
- } else {
- lua_pushstring(L, luaL_checkstring(L, -2));
- lua_pushstring(L, luaL_checkstring(L, -2));
+ return 0;
}
-
- return returncount;
+ lua_rawgeti(L, 1, 1);
+ lua_rawgeti(L, 1, 2);
+ return 2;
}
static int generic_tostring(lua_State *L) {
lua_rawgeti(L, 1, 1);
- lua_pushstring(L, luaL_optstring(L, -1, "nil"));
+ if (!lua_isstring(L, -1)) lua_pushstring(L, "nil");
return 1;
}
@@ -240,6 +223,7 @@ void push_bsontype_table(lua_State* L, mongo::BSONType bsontype) {
lua_pushcfunction(L, number_value);
break;
case mongo::Symbol:
+ case mongo::BinData:
case mongo::jstOID:
lua_pushcfunction(L, string_value);
break;
@@ -256,10 +240,11 @@ void push_bsontype_table(lua_State* L, mongo::BSONType bsontype) {
switch(bsontype) {
case mongo::NumberInt:
case mongo::Symbol:
+ case mongo::BinData:
case mongo::jstOID:
+ case mongo::Timestamp:
lua_pushcfunction(L, generic_tostring);
break;
- case mongo::Timestamp:
case mongo::NumberLong:
lua_pushcfunction(L, longlong_tostring);
break;
@@ -384,6 +369,7 @@ int mongo_bsontypes_register(lua_State *L) {
{"NumberInt", bson_type_NumberInt},
{"NumberLong", bson_type_NumberLong},
{"Symbol", bson_type_Symbol},
+ {"BinData", bson_type_BinData},
{"ObjectId", bson_type_ObjectID},
{"NULL", bson_type_NULL},
View
@@ -184,7 +184,7 @@ static int gridfile_write(lua_State *L) {
const char *where = luaL_checkstring(L, 2);
try {
- gridfile->write(lua_tostring(L, 2));
+ gridfile->write(where);
} catch (std::exception &e) {
lua_pushboolean(L, 0);
lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFILE, "write", e.what());
Oops, something went wrong.

0 comments on commit 3f3c78f

Please sign in to comment.