Skip to content

Fix memory leak and change warnings to exceptions in bson_to_zval() #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.scripts/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ wget -O run-tests.php https://raw.githubusercontent.com/php/php-src/master/run-t

rm -rf tmp-lcov tmp # coveralls may pick it up and lie about our coverage

echo "extension=phongo.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

MONGO=`which mongo`
mongod --version
Expand Down
10 changes: 7 additions & 3 deletions src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,16 +893,20 @@ int bson_to_zval(const unsigned char *data, int data_len, php_phongo_bson_state
bool eof = false;
TSRMLS_FETCH();

/* Ensure that state->zchild has a type, since the calling code may want to
* zval_ptr_dtor() it if we throw an exception. */
ZVAL_NULL(state->zchild);

reader = bson_reader_new_from_data(data, data_len);

if (!(b = bson_reader_read(reader, NULL))) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not read document from reader");
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
bson_reader_destroy(reader);
return 0;
}

if (!bson_iter_init(&iter, b)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize BSON iterator");
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not initialize BSON iterator");
bson_reader_destroy(reader);
return 0;
}
Expand Down Expand Up @@ -941,7 +945,7 @@ int bson_to_zval(const unsigned char *data, int data_len, php_phongo_bson_state
}

if (bson_reader_read(reader, &eof) || !eof) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Reading document did not exhaust input buffer");
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Reading document did not exhaust input buffer");
bson_reader_destroy(reader);
return 0;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/bson/bson-toPHP_error-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
BSON\toPHP(): BSON decoding exceptions
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

/* We can't really test for bson_iter_init() failure, since bson_reader_read()
* already checks that the buffer is at least 5 bytes.
*/
$invalidBson = array(
'',
str_repeat(fromJSON('{"x": "y"}'), 2),
);

foreach ($invalidBson as $bson) {
try {
var_dump(toPHP($bson));
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
echo $e->getMessage(), "\n";
}
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Could not read document from BSON reader
Reading document did not exhaust input buffer
===DONE===
11 changes: 6 additions & 5 deletions tests/bson/bug0325.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ require_once __DIR__ . "/../utils/basic.inc";

$bson1 = fromJSON('{"x": "y"}');
$bson2 = fromJSON('{"a": "b"}');
$value = toPHP($bson1 . $bson2);

var_dump($value);
try {
var_dump(toPHP($bson1 . $bson2));
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
echo $e->getMessage(), "\n";
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Warning:%sReading document did not exhaust input buffer in %s on line %d%a
NULL
Reading document did not exhaust input buffer
===DONE===
20 changes: 20 additions & 0 deletions tests/bson/bug0347.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Test for PHPC-347: Memory leak decoding empty buffer
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

try {
var_dump(toPHP(''));
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
echo $e->getMessage(), "\n";
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Could not read document from BSON reader
===DONE===