diff options
author | Mathias Stearn <mathias@10gen.com> | 2013-07-24 16:43:32 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2013-07-24 16:49:27 -0400 |
commit | 52ab34807bc0887e62bc3df2ed959cd7dd4f6d08 (patch) | |
tree | bb7e641cc30f18b2006478331beac9fb4685a5be /src | |
parent | 9b4367bfc32e2e937da154e87db6e97fb2509b46 (diff) | |
download | mongo-52ab34807bc0887e62bc3df2ed959cd7dd4f6d08.tar.gz |
Don't truncate strings at NUL byte in BSONElement::String()
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/bson/bsonelement.h | 2 | ||||
-rw-r--r-- | src/mongo/dbtests/jsobjtests.cpp | 14 |
2 files changed, 15 insertions, 1 deletions
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h index 11600d01331..753a89d2704 100644 --- a/src/mongo/bson/bsonelement.h +++ b/src/mongo/bson/bsonelement.h @@ -65,7 +65,7 @@ namespace mongo { std::string foo = obj["foo"].String(); // std::exception if not a std::string type or DNE */ - std::string String() const { return chk(mongo::String).valuestr(); } + std::string String() const { return chk(mongo::String).str(); } Date_t Date() const { return chk(mongo::Date).date(); } double Number() const { return chk(isNumber()).number(); } double Double() const { return chk(NumberDouble)._numberDouble(); } diff --git a/src/mongo/dbtests/jsobjtests.cpp b/src/mongo/dbtests/jsobjtests.cpp index ded17334641..d655c771c9d 100644 --- a/src/mongo/dbtests/jsobjtests.cpp +++ b/src/mongo/dbtests/jsobjtests.cpp @@ -911,6 +911,19 @@ namespace JsobjTests { } }; + class StringWithNull { + public: + void run() { + const string input = string("a") + '\0' + 'b'; + ASSERT_EQUALS(input.size(), 3U); + + BSONObj obj = BSON("str" << input); + const string output = obj.firstElement().String(); + ASSERT_EQUALS(escape(output), escape(input)); // for better failure output + ASSERT_EQUALS(output, input); + } + }; + namespace Validation { class Base { @@ -2183,6 +2196,7 @@ namespace JsobjTests { add< BSONObjTests::ArrayAppendAs >(); add< BSONObjTests::GetField >(); add< BSONObjTests::ToStringRecursionDepth >(); + add< BSONObjTests::StringWithNull >(); add< BSONObjTests::Validation::BadType >(); add< BSONObjTests::Validation::EooBeforeEnd >(); |