Skip to content

Commit 07387fa

Browse files
committed
PHPC-347: bson_to_zval() leaks when buffer contains no documents
1 parent 8d6c17d commit 07387fa

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/bson.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ int bson_to_zval(const unsigned char *data, int data_len, php_phongo_bson_state
898898

899899
if (!(b = bson_reader_read(reader, NULL))) {
900900
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
901+
bson_reader_destroy(reader);
901902
return 0;
902903
}
903904

tests/bson/bug0347.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Test for PHPC-347: Memory leak decoding empty buffer
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
try {
10+
var_dump(toPHP(''));
11+
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
12+
echo $e->getMessage(), "\n";
13+
}
14+
15+
?>
16+
===DONE===
17+
<?php exit(0); ?>
18+
--EXPECTF--
19+
Could not read document from BSON reader
20+
===DONE===

0 commit comments

Comments
 (0)