Files
Jeremy Mikola d6bef26cdd PHPC-2210 and PHPC-2211: Fix direct BSON copy and toJson memory leak (#1415)
* PHPC-2211: Free intermediary result in php_phongo_bson_to_json()

This was responsible for several memory leaks in MongoDB\BSON\Document's JSON output methods.

* PHPC-2210: Fix direct copying of Document and PackedArray BSON

bson_copy_to() cannot be used with a bson_t originally allocated with bson_new(), since there is no way to revert the bson_t to an uninitialized state.

The previous code with bson_destroy() resulted in invalid reads/writes (reported by Valgrind) and could cause "malloc(): unaligned tcache chunk detected" aborts later in the process during a future, unrelated call to bson_copy_to().

With bson_destroy() removed, the malloc() aborts are avoided but Valgrind reports a leak for the originally allocation by bson_new().

The solution applied here borrows logic from bson_copy_to_excluding_noinit() to do a more flexible copy. bson_copy_to_excluding_noinit() could not be used directly because it requires at least one exclusion field name.

* Run clang-format

Co-authored-by: Andreas Braun <git@alcaeus.org>
2023-04-27 09:21:14 -04:00

39 lines
765 B
PHP

--TEST--
MongoDB\Driver\Command::__construct() $document is MongoDB\Driver\Document
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";
$manager = create_test_manager();
$document = MongoDB\BSON\Document::fromJSON('{ "ping": 1 }');
$command = new MongoDB\Driver\Command($document);
var_dump($command);
$cursor = $manager->executeCommand(DATABASE_NAME, $command);
var_dump($cursor->toArray()[0]);
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
object(MongoDB\Driver\Command)#%d (%d) {
["command"]=>
object(stdClass)#%d (%d) {
["ping"]=>
int(1)
}
}
object(stdClass)#%d (%d) {
["ok"]=>
float(1)%A
}
===DONE===