Skip to content

Commit fbd2931

Browse files
committed
PHPC-615: Return after throwing for bson_reader_read() error
This adds additional error tests for toJSON(), which demonstrate the problem when both exceptions might have been thrown. Additionally, this ensures that the bson_reader_t is freed before returning. Previously, the code relied on the free after the second exception was thrown.
1 parent 90c9c50 commit fbd2931

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/bson.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,8 @@ PHP_FUNCTION(toJSON)
15871587
bson_free(str);
15881588
} else {
15891589
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
1590+
bson_reader_destroy(reader);
1591+
return;
15901592
}
15911593

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

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===

0 commit comments

Comments
 (0)