Skip to content

Commit 3b83ace

Browse files
committed
Make key comparison logic more strict
1 parent 9b020ac commit 3b83ace

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/BSON/PackedArray.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ static PHP_METHOD(MongoDB_BSON_PackedArray, fromJSON)
129129
// Check if BSON contains only numeric keys
130130
if (!bson_empty(bson)) {
131131
bson_iter_t iter;
132-
uint64_t expected_key = 0;
132+
uint32_t expected_key = 0;
133+
char expected_key_str[11];
134+
const char* key_str;
133135

134136
if (!bson_iter_init(&iter, bson)) {
135137
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Received invalid JSON array");
@@ -138,12 +140,11 @@ static PHP_METHOD(MongoDB_BSON_PackedArray, fromJSON)
138140
}
139141

140142
while (bson_iter_next(&iter)) {
141-
const char* string_key = bson_iter_key(&iter);
142-
char* string_key_end;
143-
uint64_t int_key = strtoll(string_key, &string_key_end, 10);
143+
key_str = bson_iter_key(&iter);
144+
snprintf(expected_key_str, sizeof(expected_key_str), "%" PRIu32, expected_key);
144145

145-
if (*string_key_end != '\0' || int_key != expected_key) {
146-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Received invalid JSON array: expected key %" PRId64 ", but found \"%s\"", expected_key, string_key);
146+
if (strcmp(key_str, expected_key_str)) {
147+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Received invalid JSON array: expected key %" PRIu32 ", but found \"%s\"", expected_key, key_str);
147148
bson_destroy(bson);
148149
return;
149150
}

tests/bson/bson-packedarray-fromJSON_error-001.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ echo throws(function() {
1313
MongoDB\BSON\PackedArray::fromJSON('{ "foo": "bar" }');
1414
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), PHP_EOL;
1515

16+
echo throws(function() {
17+
MongoDB\BSON\PackedArray::fromJSON('{ "00": "bar", "1": "bar" }');
18+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), PHP_EOL;
19+
1620
?>
1721
===DONE===
1822
<?php exit(0); ?>
@@ -21,4 +25,6 @@ OK: Got MongoDB\Driver\Exception\UnexpectedValueException
2125
Got parse error at "o", position 1: "SPECIAL_EXPECTED"
2226
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
2327
Received invalid JSON array: expected key 0, but found "foo"
28+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
29+
Received invalid JSON array: expected key 0, but found "00"
2430
===DONE===

0 commit comments

Comments
 (0)