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.
This commit is contained in:
Jeremy Mikola
2016-03-02 14:26:21 -05:00
parent 90c9c50a4b
commit fbd29313a4
2 changed files with 30 additions and 0 deletions
+2
View File
@@ -1587,6 +1587,8 @@ PHP_FUNCTION(toJSON)
bson_free(str);
} else {
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
bson_reader_destroy(reader);
return;
}
if (bson_reader_read(reader, &eof) || !eof) {
+28
View File
@@ -0,0 +1,28 @@
--TEST--
BSON\toJSON(): BSON decoding exceptions for malformed documents
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
$tests = array(
pack('Vx', 4), // Empty document with invalid length (too small)
pack('Vx', 6), // Empty document with invalid length (too large)
);
foreach ($tests as $bson) {
echo throws(function() use ($bson) {
toJSON($bson);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
}
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Could not read document from BSON reader
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Could not read document from BSON reader
===DONE===