Skip to content

Commit 81631fd

Browse files
authored
CDRIVER-4678 fix $code and $dbPointer array parsing (#1356)
1 parent 2105327 commit 81631fd

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

src/libbson/src/bson/bson-json.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ _bson_json_read_code_or_scope_key (bson_json_reader_bson_t *bson,
12971297
/* save the key, e.g. {"key": {"$code": "return x", "$scope":{"x":1}}},
12981298
* in case it is overwritten while parsing scope sub-object */
12991299
_bson_json_buf_set (
1300-
&bson->code_data.key_buf, bson->key_buf.buf, bson->key_buf.len);
1300+
&bson->code_data.key_buf, bson->key, bson->key_buf.len);
13011301
}
13021302

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

14061406
bson->bson_type = BSON_TYPE_DBPOINTER;
14071407
bson->read_state = BSON_JSON_IN_BSON_TYPE_DBPOINTER_STARTMAP;

src/libbson/tests/test-json.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,6 +3646,140 @@ test_decimal128_overflowing_exponent (void)
36463646
}
36473647
}
36483648

3649+
static void
3650+
test_parse_array (void)
3651+
{
3652+
{
3653+
bson_t *b1;
3654+
{
3655+
const char *json = BSON_STR ([ {"$code" : "A"} ]);
3656+
bson_error_t error;
3657+
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3658+
ASSERT_OR_PRINT (b1, error);
3659+
}
3660+
3661+
bson_t *b2;
3662+
{
3663+
const char *json = BSON_STR ({"0" : {"$code" : "A"}});
3664+
bson_error_t error;
3665+
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3666+
ASSERT_OR_PRINT (b2, error);
3667+
}
3668+
3669+
ASSERT (bson_equal (b1, b2));
3670+
bson_destroy (b2);
3671+
bson_destroy (b1);
3672+
}
3673+
3674+
{
3675+
bson_t *b1;
3676+
{
3677+
const char *json = BSON_STR ([ {"$code" : "A"}, {"$code" : "B"} ]);
3678+
bson_error_t error;
3679+
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3680+
ASSERT_OR_PRINT (b1, error);
3681+
}
3682+
3683+
bson_t *b2;
3684+
{
3685+
const char *json =
3686+
BSON_STR ({"0" : {"$code" : "A"}}, {"1" : {"$code" : "B"}});
3687+
bson_error_t error;
3688+
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3689+
ASSERT_OR_PRINT (b2, error);
3690+
}
3691+
3692+
ASSERT (bson_equal (b1, b2));
3693+
bson_destroy (b2);
3694+
bson_destroy (b1);
3695+
}
3696+
3697+
{
3698+
bson_t *b1;
3699+
{
3700+
const char *json = BSON_STR ([ {
3701+
"$dbPointer" :
3702+
{"$ref" : "foo",
3703+
"$id" : {"$oid" : "01234567890abcdef0123456"}}
3704+
} ]);
3705+
bson_error_t error;
3706+
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3707+
ASSERT_OR_PRINT (b1, error);
3708+
}
3709+
3710+
bson_t *b2;
3711+
{
3712+
const char *json = BSON_STR ({
3713+
"0" : {
3714+
"$dbPointer" : {
3715+
"$ref" : "foo",
3716+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3717+
}
3718+
}
3719+
});
3720+
bson_error_t error;
3721+
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3722+
ASSERT_OR_PRINT (b2, error);
3723+
}
3724+
3725+
ASSERT (bson_equal (b1, b2));
3726+
bson_destroy (b2);
3727+
bson_destroy (b1);
3728+
}
3729+
3730+
{
3731+
bson_t *b1;
3732+
{
3733+
const char *json = BSON_STR ([
3734+
{
3735+
"$dbPointer" : {
3736+
"$ref" : "foo",
3737+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3738+
}
3739+
},
3740+
{
3741+
"$dbPointer" : {
3742+
"$ref" : "foo",
3743+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3744+
}
3745+
}
3746+
]);
3747+
bson_error_t error;
3748+
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3749+
ASSERT_OR_PRINT (b1, error);
3750+
}
3751+
3752+
bson_t *b2;
3753+
{
3754+
const char *json = BSON_STR (
3755+
{
3756+
"0" : {
3757+
"$dbPointer" : {
3758+
"$ref" : "foo",
3759+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3760+
}
3761+
}
3762+
},
3763+
{
3764+
"1" : {
3765+
"$dbPointer" : {
3766+
"$ref" : "foo",
3767+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3768+
}
3769+
}
3770+
});
3771+
3772+
bson_error_t error;
3773+
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3774+
ASSERT_OR_PRINT (b2, error);
3775+
}
3776+
3777+
ASSERT (bson_equal (b1, b2));
3778+
bson_destroy (b2);
3779+
bson_destroy (b1);
3780+
}
3781+
}
3782+
36493783
void
36503784
test_json_install (TestSuite *suite)
36513785
{
@@ -3839,6 +3973,7 @@ test_json_install (TestSuite *suite)
38393973
TestSuite_Add (suite,
38403974
"/bson/as_json_with_opts/all_types",
38413975
test_bson_as_json_with_opts_all_types);
3976+
TestSuite_Add (suite, "/bson/parse_array", test_parse_array);
38423977
TestSuite_Add (suite,
38433978
"/bson/decimal128_overflowing_exponent",
38443979
test_decimal128_overflowing_exponent);

0 commit comments

Comments
 (0)