summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2015-02-11 22:12:37 -0500
committerRamon Fernandez <ramon@mongodb.com>2015-02-17 13:39:51 -0500
commit3a7e85ea1f672f702660e5472566234b1d19038e (patch)
treefcb35c1cf158fcd917711be3e7bd004d2d1f306a
parent656f78711632a5dc37221422c99e3c4619bcc58f (diff)
downloadmongo-3a7e85ea1f672f702660e5472566234b1d19038e.tar.gz
SERVER-17264: improve bson validation for utf-8 strings
(cherry picked from commit 394a8569ff14a215c0691aa34440227b2e62a4de) Conflicts: src/mongo/bson/bson_validate_test.cpp
-rw-r--r--src/mongo/bson/bson_validate.cpp5
-rw-r--r--src/mongo/bson/bson_validate_test.cpp14
2 files changed, 19 insertions, 0 deletions
diff --git a/src/mongo/bson/bson_validate.cpp b/src/mongo/bson/bson_validate.cpp
index 9ac0bcbbbfb..a7c95ad1370 100644
--- a/src/mongo/bson/bson_validate.cpp
+++ b/src/mongo/bson/bson_validate.cpp
@@ -62,6 +62,11 @@ namespace mongo {
if ( !readNumber<int>( &sz ) )
return Status( ErrorCodes::InvalidBSON, "invalid bson" );
+ if ( sz <= 0 ) {
+ // must have NULL at the very least
+ return Status( ErrorCodes::InvalidBSON, "invalid bson");
+ }
+
if ( out ) {
*out = StringData( _buffer + _position, sz );
}
diff --git a/src/mongo/bson/bson_validate_test.cpp b/src/mongo/bson/bson_validate_test.cpp
index 61e67f33c46..a36dbc582d6 100644
--- a/src/mongo/bson/bson_validate_test.cpp
+++ b/src/mongo/bson/bson_validate_test.cpp
@@ -213,4 +213,18 @@ namespace {
ASSERT_NOT_OK(validateBSON(x.objdata(), x.objsize() / 2));
}
+ TEST(BSONValidateFast, StringHasSomething) {
+ BufBuilder bb;
+ BSONObjBuilder ob(bb);
+ bb.appendChar(String);
+ bb.appendStr("x", /*withNUL*/true);
+ bb.appendNum(0);
+ const BSONObj x = ob.done();
+ ASSERT_EQUALS(5 // overhead
+ + 1 // type
+ + 2 // name
+ + 4 // size
+ , x.objsize());
+ ASSERT_NOT_OK(validateBSON(x.objdata(), x.objsize()));
+ }
}