Skip to content

Commit 49ef296

Browse files
arnaud-lbTimWolla
andcommitted
Add tests
Co-authored-by: Tim Düsterhus <[email protected]>
1 parent a796eca commit 49ef296

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

Zend/tests/gh19543-001.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-19543 001: GC treats ZEND_WEAKREF_TAG_MAP references as WeakMap references
3+
--FILE--
4+
<?php
5+
6+
$e = new Exception();
7+
$a = new stdClass();
8+
zend_weakmap_attach($e, $a);
9+
unset($a);
10+
gc_collect_cycles();
11+
12+
?>
13+
==DONE==
14+
--EXPECT--
15+
==DONE==

Zend/tests/gh19543-002.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-19543 001: GC treats ZEND_WEAKREF_TAG_MAP references as WeakMap references
3+
--FILE--
4+
<?php
5+
6+
$e = new Exception();
7+
$a = new stdClass();
8+
zend_weakmap_attach($e, $a);
9+
unset($a);
10+
$e2 = $e;
11+
unset($e2); // add to roots
12+
gc_collect_cycles();
13+
14+
?>
15+
==DONE==
16+
--EXPECT--
17+
==DONE==

ext/zend_test/php_test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test)
5050
int observer_fiber_switch;
5151
int observer_fiber_destroy;
5252
int observer_execute_internal;
53-
HashTable global_weakmap;
53+
HashTable *global_weakmap;
5454
int replace_zend_execute_ex;
5555
int register_passes;
5656
bool print_stderr_mshutdown;

ext/zend_test/test.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static ZEND_FUNCTION(zend_weakmap_attach)
340340
Z_PARAM_ZVAL(value)
341341
ZEND_PARSE_PARAMETERS_END();
342342

343-
if (zend_weakrefs_hash_add(&ZT_G(global_weakmap), obj, value)) {
343+
if (zend_weakrefs_hash_add(ZT_G(global_weakmap), obj, value)) {
344344
Z_TRY_ADDREF_P(value);
345345
RETURN_TRUE;
346346
}
@@ -355,13 +355,13 @@ static ZEND_FUNCTION(zend_weakmap_remove)
355355
Z_PARAM_OBJ(obj)
356356
ZEND_PARSE_PARAMETERS_END();
357357

358-
RETURN_BOOL(zend_weakrefs_hash_del(&ZT_G(global_weakmap), obj) == SUCCESS);
358+
RETURN_BOOL(zend_weakrefs_hash_del(ZT_G(global_weakmap), obj) == SUCCESS);
359359
}
360360

361361
static ZEND_FUNCTION(zend_weakmap_dump)
362362
{
363363
ZEND_PARSE_PARAMETERS_NONE();
364-
RETURN_ARR(zend_array_dup(&ZT_G(global_weakmap)));
364+
RETURN_ARR(zend_array_dup(ZT_G(global_weakmap)));
365365
}
366366

367367
static ZEND_FUNCTION(zend_get_current_func_name)
@@ -1311,18 +1311,20 @@ PHP_MSHUTDOWN_FUNCTION(zend_test)
13111311

13121312
PHP_RINIT_FUNCTION(zend_test)
13131313
{
1314-
zend_hash_init(&ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0);
1314+
ZT_G(global_weakmap) = emalloc(sizeof(HashTable));
1315+
zend_hash_init(ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0);
13151316
ZT_G(observer_nesting_depth) = 0;
13161317
return SUCCESS;
13171318
}
13181319

13191320
PHP_RSHUTDOWN_FUNCTION(zend_test)
13201321
{
13211322
zend_ulong obj_key;
1322-
ZEND_HASH_FOREACH_NUM_KEY(&ZT_G(global_weakmap), obj_key) {
1323-
zend_weakrefs_hash_del(&ZT_G(global_weakmap), zend_weakref_key_to_object(obj_key));
1323+
ZEND_HASH_FOREACH_NUM_KEY(ZT_G(global_weakmap), obj_key) {
1324+
zend_weakrefs_hash_del(ZT_G(global_weakmap), zend_weakref_key_to_object(obj_key));
13241325
} ZEND_HASH_FOREACH_END();
1325-
zend_hash_destroy(&ZT_G(global_weakmap));
1326+
zend_hash_destroy(ZT_G(global_weakmap));
1327+
efree(ZT_G(global_weakmap));
13261328

13271329
if (ZT_G(zend_test_heap)) {
13281330
free(ZT_G(zend_test_heap));

0 commit comments

Comments
 (0)