Skip to content

Commit 9dffec1

Browse files
author
Paweł Andruszkiewicz
committed
BUG#37243264 Negative BSON values cause util.importJson() to fail
When importing JSON with `convertBsonTypes` option set to true, negative $numberInt/$numberLong values (or positive value prefixed with the `+` character) caused the import to fail. The parser did not skip the `-` and `+` characters when validating the integer values. Change-Id: I902774d8781e805db6e6b7c236503a5ff278d67d
1 parent 0f43e99 commit 9dffec1

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

mysqlshdk/libs/utils/document_parser.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ void Json_document_parser::get_bson_data(const std::vector<Bson_token> &tokens,
606606
// Integers must have pure decimal digits
607607
if (token.type == 'I' || token.type == 'i') {
608608
size_t digit_index = 0;
609-
if ((*target)[0] == '+' || (*target)[0] == '-') digit_index++;
609+
if ((*target)[value_start] == '+' || (*target)[value_start] == '-') {
610+
digit_index++;
611+
}
610612

611613
for (size_t index = digit_index; index < value_size; index++) {
612614
if (!isdigit((*target)[value_start + index]))
@@ -717,7 +719,14 @@ void Json_document_parser::parse_bson_integer(Bson_type type) {
717719

718720
get_bson_data({{':'}, {'I', "", &value, nullptr}, {'}'}}, context);
719721

720-
std::string number = value.substr(1, value.size() - 2);
722+
// handle a signed integer starting with a plus, trim the plus character
723+
std::size_t start = 1;
724+
725+
if ('+' == value[start]) {
726+
++start;
727+
}
728+
729+
std::string number = value.substr(start, value.size() - 1 - start);
721730

722731
m_document->append(number);
723732
}

unittest/scripts/auto/js_shell/scripts/json_import_bson_norecord.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ EXPECT_STDOUT_CONTAINS("ERROR: Unexpected data, expected to find '}' processing
223223
import_doc({_id:{$oid:"5bfe3d5ff6d2f36d067c6fea"},int:{$numberInt:""}});
224224
EXPECT_STDOUT_CONTAINS("ERROR: Unexpected data, expected to find an integer string processing extended JSON for $numberInt at offset 64");
225225

226+
//@<> BUG#37243264 $numberInt with just +
227+
import_doc({_id:{$oid:"5bfe3d5ff6d2f36d067c6fea"},int:{$numberInt:"+"}});
228+
EXPECT_STDOUT_CONTAINS("ERROR: Unexpected data, expected to find an integer string processing extended JSON for $numberInt at offset 64");
229+
226230

227231
//@<> $numberDecimal with non string value
228232
import_doc({_id:{$oid:"5bfe3d5ff6d2f36d067c6fea"},decimal:{$numberDecimal:15.4}});
@@ -311,3 +315,15 @@ import_doc(document, ["--convertBsonTypes", "--convertBsonOid=false"])
311315
print_doc();
312316
delete_doc();
313317
EXPECT_STDOUT_CONTAINS('{"_id": "01234567890123456789", "objectId": {"$oid": "5c014aa8087579bdb2e6afef"}, "negativeInteger": -450}');
318+
319+
//@<> BUG#37243264 $numberInt with negative integer
320+
import_doc({_id:{$oid:"5bfe3d5ff6d2f36d067c6fea"},int:{$numberInt:"-1234"}});
321+
print_doc();
322+
delete_doc();
323+
EXPECT_STDOUT_CONTAINS('{"_id": "5bfe3d5ff6d2f36d067c6fea", "int": -1234}');
324+
325+
//@<> BUG#37243264 $numberInt with positive integer
326+
import_doc({_id:{$oid:"5bfe3d5ff6d2f36d067c6fea"},int:{$numberInt:"+1234"}});
327+
print_doc();
328+
delete_doc();
329+
EXPECT_STDOUT_CONTAINS('{"_id": "5bfe3d5ff6d2f36d067c6fea", "int": 1234}');

0 commit comments

Comments
 (0)