Skip to content

Commit d432311

Browse files
joshbsiegelkevinAlbs
authored andcommitted
CDRIVER-4678 fix $code and $dbPointer array parsing (#1356)
1 parent e50a625 commit d432311

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
@@ -3602,6 +3602,140 @@ test_decimal128_overflowing_exponent (void)
36023602
}
36033603
}
36043604

3605+
static void
3606+
test_parse_array (void)
3607+
{
3608+
{
3609+
bson_t *b1;
3610+
{
3611+
const char *json = BSON_STR ([ {"$code" : "A"} ]);
3612+
bson_error_t error;
3613+
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3614+
ASSERT_OR_PRINT (b1, error);
3615+
}
3616+
3617+
bson_t *b2;
3618+
{
3619+
const char *json = BSON_STR ({"0" : {"$code" : "A"}});
3620+
bson_error_t error;
3621+
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3622+
ASSERT_OR_PRINT (b2, error);
3623+
}
3624+
3625+
ASSERT (bson_equal (b1, b2));
3626+
bson_destroy (b2);
3627+
bson_destroy (b1);
3628+
}
3629+
3630+
{
3631+
bson_t *b1;
3632+
{
3633+
const char *json = BSON_STR ([ {"$code" : "A"}, {"$code" : "B"} ]);
3634+
bson_error_t error;
3635+
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3636+
ASSERT_OR_PRINT (b1, error);
3637+
}
3638+
3639+
bson_t *b2;
3640+
{
3641+
const char *json =
3642+
BSON_STR ({"0" : {"$code" : "A"}}, {"1" : {"$code" : "B"}});
3643+
bson_error_t error;
3644+
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3645+
ASSERT_OR_PRINT (b2, error);
3646+
}
3647+
3648+
ASSERT (bson_equal (b1, b2));
3649+
bson_destroy (b2);
3650+
bson_destroy (b1);
3651+
}
3652+
3653+
{
3654+
bson_t *b1;
3655+
{
3656+
const char *json = BSON_STR ([ {
3657+
"$dbPointer" :
3658+
{"$ref" : "foo",
3659+
"$id" : {"$oid" : "01234567890abcdef0123456"}}
3660+
} ]);
3661+
bson_error_t error;
3662+
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3663+
ASSERT_OR_PRINT (b1, error);
3664+
}
3665+
3666+
bson_t *b2;
3667+
{
3668+
const char *json = BSON_STR ({
3669+
"0" : {
3670+
"$dbPointer" : {
3671+
"$ref" : "foo",
3672+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3673+
}
3674+
}
3675+
});
3676+
bson_error_t error;
3677+
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3678+
ASSERT_OR_PRINT (b2, error);
3679+
}
3680+
3681+
ASSERT (bson_equal (b1, b2));
3682+
bson_destroy (b2);
3683+
bson_destroy (b1);
3684+
}
3685+
3686+
{
3687+
bson_t *b1;
3688+
{
3689+
const char *json = BSON_STR ([
3690+
{
3691+
"$dbPointer" : {
3692+
"$ref" : "foo",
3693+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3694+
}
3695+
},
3696+
{
3697+
"$dbPointer" : {
3698+
"$ref" : "foo",
3699+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3700+
}
3701+
}
3702+
]);
3703+
bson_error_t error;
3704+
b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3705+
ASSERT_OR_PRINT (b1, error);
3706+
}
3707+
3708+
bson_t *b2;
3709+
{
3710+
const char *json = BSON_STR (
3711+
{
3712+
"0" : {
3713+
"$dbPointer" : {
3714+
"$ref" : "foo",
3715+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3716+
}
3717+
}
3718+
},
3719+
{
3720+
"1" : {
3721+
"$dbPointer" : {
3722+
"$ref" : "foo",
3723+
"$id" : {"$oid" : "01234567890abcdef0123456"}
3724+
}
3725+
}
3726+
});
3727+
3728+
bson_error_t error;
3729+
b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
3730+
ASSERT_OR_PRINT (b2, error);
3731+
}
3732+
3733+
ASSERT (bson_equal (b1, b2));
3734+
bson_destroy (b2);
3735+
bson_destroy (b1);
3736+
}
3737+
}
3738+
36053739
void
36063740
test_json_install (TestSuite *suite)
36073741
{
@@ -3791,6 +3925,7 @@ test_json_install (TestSuite *suite)
37913925
TestSuite_Add (suite,
37923926
"/bson/as_json_with_opts/all_types",
37933927
test_bson_as_json_with_opts_all_types);
3928+
TestSuite_Add (suite, "/bson/parse_array", test_parse_array);
37943929
TestSuite_Add (suite,
37953930
"/bson/decimal128_overflowing_exponent",
37963931
test_decimal128_overflowing_exponent);

0 commit comments

Comments
 (0)