Skip to content

CDRIVER-4678 Fix $code and $dbPointer array parsing #1356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libbson/src/bson/bson-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ _bson_json_read_code_or_scope_key (bson_json_reader_bson_t *bson,
/* save the key, e.g. {"key": {"$code": "return x", "$scope":{"x":1}}},
* in case it is overwritten while parsing scope sub-object */
_bson_json_buf_set (
&bson->code_data.key_buf, bson->key_buf.buf, bson->key_buf.len);
&bson->code_data.key_buf, bson->key, bson->key_buf.len);
}

if (is_scope) {
Expand Down Expand Up @@ -1401,7 +1401,7 @@ _bson_json_read_map_key (bson_json_reader_t *reader, /* IN */
{
/* start parsing "key": {"$dbPointer": {...}}, save "key" for later */
_bson_json_buf_set (
&bson->dbpointer_key, bson->key_buf.buf, bson->key_buf.len);
&bson->dbpointer_key, bson->key, bson->key_buf.len);

bson->bson_type = BSON_TYPE_DBPOINTER;
bson->read_state = BSON_JSON_IN_BSON_TYPE_DBPOINTER_STARTMAP;
Expand Down
135 changes: 135 additions & 0 deletions src/libbson/tests/test-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -3646,6 +3646,140 @@ test_decimal128_overflowing_exponent (void)
}
}

static void
test_parse_array (void)
{
{
bson_t *b1;
{
const char *json = BSON_STR ([ {"$code" : "A"} ]);
bson_error_t error;
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
ASSERT_OR_PRINT (b1, error);
}

bson_t *b2;
{
const char *json = BSON_STR ({"0" : {"$code" : "A"}});
bson_error_t error;
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
ASSERT_OR_PRINT (b2, error);
}

ASSERT (bson_equal (b1, b2));
bson_destroy (b2);
bson_destroy (b1);
}

{
bson_t *b1;
{
const char *json = BSON_STR ([ {"$code" : "A"}, {"$code" : "B"} ]);
bson_error_t error;
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
ASSERT_OR_PRINT (b1, error);
}

bson_t *b2;
{
const char *json =
BSON_STR ({"0" : {"$code" : "A"}}, {"1" : {"$code" : "B"}});
bson_error_t error;
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
ASSERT_OR_PRINT (b2, error);
}

ASSERT (bson_equal (b1, b2));
bson_destroy (b2);
bson_destroy (b1);
}

{
bson_t *b1;
{
const char *json = BSON_STR ([ {
"$dbPointer" :
{"$ref" : "foo",
"$id" : {"$oid" : "01234567890abcdef0123456"}}
} ]);
bson_error_t error;
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
ASSERT_OR_PRINT (b1, error);
}

bson_t *b2;
{
const char *json = BSON_STR ({
"0" : {
"$dbPointer" : {
"$ref" : "foo",
"$id" : {"$oid" : "01234567890abcdef0123456"}
}
}
});
bson_error_t error;
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
ASSERT_OR_PRINT (b2, error);
}

ASSERT (bson_equal (b1, b2));
bson_destroy (b2);
bson_destroy (b1);
}

{
bson_t *b1;
{
const char *json = BSON_STR ([
{
"$dbPointer" : {
"$ref" : "foo",
"$id" : {"$oid" : "01234567890abcdef0123456"}
}
},
{
"$dbPointer" : {
"$ref" : "foo",
"$id" : {"$oid" : "01234567890abcdef0123456"}
}
}
]);
bson_error_t error;
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
ASSERT_OR_PRINT (b1, error);
}

bson_t *b2;
{
const char *json = BSON_STR (
{
"0" : {
"$dbPointer" : {
"$ref" : "foo",
"$id" : {"$oid" : "01234567890abcdef0123456"}
}
}
},
{
"1" : {
"$dbPointer" : {
"$ref" : "foo",
"$id" : {"$oid" : "01234567890abcdef0123456"}
}
}
});

bson_error_t error;
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
ASSERT_OR_PRINT (b2, error);
}

ASSERT (bson_equal (b1, b2));
bson_destroy (b2);
bson_destroy (b1);
}
}

void
test_json_install (TestSuite *suite)
{
Expand Down Expand Up @@ -3839,6 +3973,7 @@ test_json_install (TestSuite *suite)
TestSuite_Add (suite,
"/bson/as_json_with_opts/all_types",
test_bson_as_json_with_opts_all_types);
TestSuite_Add (suite, "/bson/parse_array", test_parse_array);
TestSuite_Add (suite,
"/bson/decimal128_overflowing_exponent",
test_decimal128_overflowing_exponent);
Expand Down