mirror of
https://github.com/macintoshplus/mongo-php-driver.git
synced 2026-04-25 17:38:09 +02:00
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:
@@ -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) {
|
||||
|
||||
@@ -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===
|
||||
Reference in New Issue
Block a user