mirror of
https://github.com/macintoshplus/mongo-php-driver.git
synced 2026-03-25 17:32:28 +01:00
The ReadConcern, ReadPreference, WriteConcern, and BSON classes have no properties beyond pointers to libmongoc/libbson structs; however, all of these classes use get_properties to report fields for var_export (PHPC-850, PHPC-460). The standard get_gc handler defers to get_properties to collect other zvals for GC inspection. This is problematic for the aforementioned get_properties handlers, since a HashTable of zvals intended for debugging will be returned and those properties will already be freed by our free_object handler (via FREE_HASHTABLE). Having each class define its own get_gc handler is the first step to fixing this issue. The BSON classes already defined their own get_gc handlers, but erroneously returned the internally cached properties directly. The second step to fixing this issue is ensuring that get_gc delegates to zend_std_get_properties, as is done in various PHP core extensions. Doing so ensures that we do not leak other zvals that may be assigned as public properties by the application (covered by the second regression test), since zend_std_get_properties will return those for GC inspection.
29 lines
593 B
PHP
29 lines
593 B
PHP
--TEST--
|
|
PHPC-1598: WriteConcern get_gc should not invoke get_properties
|
|
--FILE--
|
|
<?php
|
|
|
|
/* Note: the segfault only manifests if the get_properties handler reports an
|
|
* allocated value (e.g. string instead of integer for "w" field). */
|
|
$wc = new MongoDB\Driver\WriteConcern('string');
|
|
|
|
$a = (object) ['wc' => $wc];
|
|
$b = (object) ['wc' => $wc];
|
|
|
|
$a->b = $b;
|
|
$b->a = $a;
|
|
|
|
printf("Collected cycles: %d\n", gc_collect_cycles());
|
|
|
|
unset($a, $b);
|
|
|
|
printf("Collected cycles: %d\n", gc_collect_cycles());
|
|
|
|
?>
|
|
===DONE===
|
|
<?php exit(0); ?>
|
|
--EXPECT--
|
|
Collected cycles: 0
|
|
Collected cycles: 2
|
|
===DONE===
|