diff options
author | Yuhong Zhang <34732434+YuhongZhang98@users.noreply.github.com> | 2023-01-05 14:55:42 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-01-09 17:29:01 +0000 |
commit | ef0e913ce74cc8869b7630aa5fc0743c5f1f447c (patch) | |
tree | 5292362b3ccd8db0be23a151cfc064028c83a504 | |
parent | d90c776681c904ded016d62108c17fd3e605f6ae (diff) | |
download | mongo-ef0e913ce74cc8869b7630aa5fc0743c5f1f447c.tar.gz |
SERVER-72495 Return warnings about CodeWScope correctly in checkBSONConformance mode for validater6.2.0-rc5
(cherry picked from commit 58bf88893c373c84226671984b8dbfe21d630cb7)
-rw-r--r-- | jstests/noPassthroughWithMongod/validate_bson_types.js | 33 | ||||
-rw-r--r-- | src/mongo/bson/bson_validate.cpp | 4 |
2 files changed, 36 insertions, 1 deletions
diff --git a/jstests/noPassthroughWithMongod/validate_bson_types.js b/jstests/noPassthroughWithMongod/validate_bson_types.js new file mode 100644 index 00000000000..ffcdd80306d --- /dev/null +++ b/jstests/noPassthroughWithMongod/validate_bson_types.js @@ -0,0 +1,33 @@ +/** + * Tests that the validate checkBSONConformance option works with various BSON types. + */ + +(function() { +const coll = db.validate_bson_types; + +assert.commandWorked(coll.insert({a: 1.0})); // double +assert.commandWorked(coll.insert({b: "abc"})); // string +assert.commandWorked(coll.insert({c: {x: 1}})); // object +assert.commandWorked(coll.insert({d: [1, 2, 3]})); // array +assert.commandWorked(coll.insert({ + e: BinData(2, "KwAAAFRoZSBxdWljayBicm93biBmb3gganVtcHMgb3ZlciB0aGUgbGF6eSBkb2c=") +})); // binData +assert.commandWorked(coll.insert({f: undefined})); // undefined +assert.commandWorked(coll.insert({g: ObjectId("dbdbdbdbdbdbdbdbdbdbdbdb")})); // objectId +assert.commandWorked(coll.insert({h: true})); // boolean +assert.commandWorked(coll.insert({i: ISODate("2013-12-11T19:38:24.055Z")})); // UTC +assert.commandWorked(coll.insert({j: null})); // null +assert.commandWorked(coll.insert({k: RegExp("a")})); // regex +assert.commandWorked( + coll.insert({l: DBPointer("foo", ObjectId("bbbbbbbbbbbbbbbbbbbbbbbb"))})); // DBPointer +assert.commandWorked(coll.insert({m: Code("noScope")})); // code +assert.commandWorked(coll.insert({n: Code('function(){return 1;}', {})})); // code w/ scope +assert.commandWorked(coll.insert({o: 3})); // int +assert.commandWorked(coll.insert({p: Timestamp(1, 2)})); // timestamp +assert.commandWorked(coll.insert({q: NumberLong(6)})); // 64-bit int +assert.commandWorked(coll.insert({r: NumberDecimal("2.0")})); // decimal 128 +assert.commandWorked(coll.insert({s: MinKey()})); // MinKey +assert.commandWorked(coll.insert({t: MaxKey()})); // MaxKey + +assert.commandWorked(coll.validate({checkBSONConformance: true})); +})();
\ No newline at end of file diff --git a/src/mongo/bson/bson_validate.cpp b/src/mongo/bson/bson_validate.cpp index cb58767049e..7226a2c20f9 100644 --- a/src/mongo/bson/bson_validate.cpp +++ b/src/mongo/bson/bson_validate.cpp @@ -537,12 +537,14 @@ private: while (size_t len = cursor.strlen()) { uint8_t type = *cursor.ptr; _currElem = cursor.ptr; + // In case _currElem is moved (for instance when the type is CodeWScope). + auto elemStart = cursor.ptr; cursor.ptr += len + 1; cursor.ptr = _validateElem(cursor, type); // Check if the data is compliant to other BSON specifications if the element is // structurally correct. - _validator.checkNonConformantElem(_currElem, len + 1, type); + _validator.checkNonConformantElem(elemStart, len + 1, type); if constexpr (precise) { // See if the _id field was just validated. If so, set the global scope element. |