Skip to content

Commit 9efd647

Browse files
committed
Merge pull request #249
2 parents 90c9c50 + 9168392 commit 9efd647

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

src/bson.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,10 +1583,19 @@ PHP_FUNCTION(toJSON)
15831583
char *str;
15841584
size_t str_len;
15851585
str = bson_as_json(b, &str_len);
1586+
1587+
if (!str) {
1588+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not convert BSON document to a JSON string");
1589+
bson_reader_destroy(reader);
1590+
return;
1591+
}
1592+
15861593
PHONGO_RETVAL_STRINGL(str, str_len);
15871594
bson_free(str);
15881595
} else {
15891596
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
1597+
bson_reader_destroy(reader);
1598+
return;
15901599
}
15911600

15921601
if (bson_reader_read(reader, &eof) || !eof) {

tests/bson/bson-toJSON_error-001.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ BSON\toJSON(): BSON decoding exceptions
66
<?php
77
require_once __DIR__ . "/../utils/basic.inc";
88

9-
/* We can't really test for bson_iter_init() failure, since bson_reader_read()
10-
* already checks that the buffer is at least 5 bytes.
9+
/* We can't really test for bson_iter_init() failure within bson_as_json(),
10+
* since bson_reader_read() already checks that the buffer is at least 5 bytes.
1111
*/
1212
$invalidBson = array(
1313
'',

tests/bson/bson-toJSON_error-002.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
BSON\toJSON(): BSON decoding exceptions for malformed documents
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
pack('Vx', 4), // Empty document with invalid length (too small)
11+
pack('Vx', 6), // Empty document with invalid length (too large)
12+
);
13+
14+
foreach ($tests as $bson) {
15+
echo throws(function() use ($bson) {
16+
toJSON($bson);
17+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
18+
}
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
25+
Could not read document from BSON reader
26+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
27+
Could not read document from BSON reader
28+
===DONE===

tests/bson/bson-toJSON_error-003.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
BSON\toJSON(): BSON decoding exceptions for bson_as_json() failure
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
// Invalid UTF-8 characters (i.e. 0xFE, 0xFF) in field name
11+
pack('VCCCxVa*xx', 17, 2, 254, 255, 3, 'bar'),
12+
/* Note: we don't use a three-character string in the underflow case, as
13+
* the 4-byte string length and payload (i.e. three characters + null byte)
14+
* coincidentally satisfy the expected size for an 8-byte double. We also
15+
* don't use a four-character string, since its null byte would be
16+
* interpreted as the document terminator. The actual document terminator
17+
* would then remain in the buffer and trigger a "did not exhaust" error.
18+
*/
19+
pack('VCa*xVa*xx', 17, 1, 'foo', 3, 'ab'), // Invalid field type (underflow)
20+
pack('VCa*xVa*xx', 20, 1, 'foo', 6, 'abcde'), // Invalid field type (overflow)
21+
);
22+
23+
foreach ($tests as $bson) {
24+
echo throws(function() use ($bson) {
25+
toJSON($bson);
26+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
27+
}
28+
29+
?>
30+
===DONE===
31+
<?php exit(0); ?>
32+
--EXPECTF--
33+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
34+
Could not convert BSON document to a JSON string
35+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
36+
Could not convert BSON document to a JSON string
37+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
38+
Could not convert BSON document to a JSON string
39+
===DONE===

0 commit comments

Comments
 (0)