Skip to content

Commit 0244d19

Browse files
committed
PHPC-923: Use zend_string_release() to free class names
1 parent 1d8a3e8 commit 0244d19

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/bson.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ bool php_phongo_bson_visit_binary(const bson_iter_t *iter ARG_UNUSED, const char
243243
#if PHP_VERSION_ID >= 70000
244244
zend_string *zs_classname = zend_string_init((const char *)v_binary, v_binary_len, 0);
245245
zend_class_entry *found_ce = zend_fetch_class(zs_classname, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
246-
zend_string_free(zs_classname);
246+
zend_string_release(zs_classname);
247247
#else
248248
zend_class_entry *found_ce = zend_fetch_class((const char *)v_binary, v_binary_len, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
249249
#endif
@@ -1653,7 +1653,7 @@ static void apply_classname_to_state(const char *classname, int classname_len, p
16531653
#if PHP_VERSION_ID >= 70000
16541654
zend_string* zs_classname = zend_string_init(classname, classname_len, 0);
16551655
zend_class_entry *found_ce = zend_fetch_class(zs_classname, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
1656-
zend_string_free(zs_classname);
1656+
zend_string_release(zs_classname);
16571657
#else
16581658
zend_class_entry *found_ce = zend_fetch_class(classname, classname_len, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
16591659
#endif

tests/bson/bug0923-001.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
PHPC-923: Use zend_string_release() to free class names (type map)
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
/* Register an autoloader that does nothing more than append the class name to
9+
* an array. This adds a reference to the zend_string in PHP 7, which exposes an
10+
* assert failure if the value is then freed with zend_string_free() instead of
11+
* zend_string_release(). */
12+
$classes = [];
13+
14+
spl_autoload_register(function ($class) use (&$classes) {
15+
$classes[] = $class;
16+
});
17+
18+
echo throws(function() {
19+
var_dump(toPHP(fromJSON('{"x":1}'), ['root' => 'MissingClass']));
20+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
21+
22+
var_dump($classes);
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECT--
28+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29+
Class MissingClass does not exist
30+
array(1) {
31+
[0]=>
32+
string(12) "MissingClass"
33+
}
34+
===DONE===

tests/bson/bug0923-002.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
PHPC-923: Use zend_string_release() to free class names (__pclass)
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
/* Register an autoloader that does nothing more than append the class name to
9+
* an array. This adds a reference to the zend_string in PHP 7, which exposes an
10+
* assert failure if the value is then freed with zend_string_free() instead of
11+
* zend_string_release(). */
12+
$classes = [];
13+
14+
spl_autoload_register(function ($class) use (&$classes) {
15+
$classes[] = $class;
16+
});
17+
18+
/* Note: An exception is not thrown if the __pclass field fails to denote a
19+
* valid class. Instead, it is left as-is and the general type map applies. */
20+
var_dump(toPHP(fromJSON('{"x":{"__pclass":{"$binary":"TWlzc2luZ0NsYXNz","$type":"80"}}}')));
21+
22+
var_dump($classes);
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECTF--
28+
object(stdClass)#%d (%d) {
29+
["x"]=>
30+
object(stdClass)#%d (%d) {
31+
["__pclass"]=>
32+
object(MongoDB\BSON\Binary)#%d (%d) {
33+
["data"]=>
34+
string(12) "MissingClass"
35+
["type"]=>
36+
int(128)
37+
}
38+
}
39+
}
40+
array(1) {
41+
[0]=>
42+
string(12) "MissingClass"
43+
}
44+
===DONE===

0 commit comments

Comments
 (0)