Skip to content

PHPC-341: Ensure bson_t is freed after decoding JSON #63

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 1 commit into from
Jul 24, 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
1 change: 1 addition & 0 deletions src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,7 @@ PHP_FUNCTION(fromJSON)

if (bson_init_from_json(&b, (const char *)data, data_len, &error)) {
RETVAL_STRINGL((const char *) bson_get_data(&b), b.len, 1);
bson_destroy(&b);
} else {
RETURN_NULL();
}
Expand Down
50 changes: 50 additions & 0 deletions tests/bson/bug0341.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
PHPC-341: fromJSON() leaks when JSON contains array or object fields
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$tests = array(
'{ "foo": "yes", "bar" : false }',
'{ "foo": "no", "array" : [ 5, 6 ] }',
'{ "foo": "no", "obj" : { "embedded" : 3.14 } }',
);

foreach ($tests as $test) {
$bson = fromJSON($test);
var_dump(toPHP($bson));
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
object(stdClass)#%d (2) {
["foo"]=>
string(3) "yes"
["bar"]=>
bool(false)
}
object(stdClass)#%d (2) {
["foo"]=>
string(2) "no"
["array"]=>
array(2) {
[0]=>
int(5)
[1]=>
int(6)
}
}
object(stdClass)#%d (2) {
["foo"]=>
string(2) "no"
["obj"]=>
object(stdClass)#%d (1) {
["embedded"]=>
float(3.14)
}
}
===DONE===