Skip to content

Commit e8283ee

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
2 parents 79f0e48 + 241bd3f commit e8283ee

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

Zend/tests/weakrefs/weakrefs_006.phpt

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
--TEST--
2+
WeakReference overwriting existing value
3+
--FILE--
4+
<?php
5+
6+
class HasDtor {
7+
public function __destruct() {
8+
echo "In destruct\n";
9+
global $w, $all;
10+
for ($i = 0; $i < 10; $i++) {
11+
$v = new stdClass();
12+
$all[] = $v;
13+
$w[$v] = $i;
14+
}
15+
}
16+
}
17+
$all = [];
18+
$w = new WeakMap();
19+
$o = new stdClass();
20+
21+
$w[$o] = new HasDtor();
22+
$w[$o] = 123;
23+
var_dump($w);
24+
?>
25+
--EXPECT--
26+
In destruct
27+
object(WeakMap)#1 (11) {
28+
[0]=>
29+
array(2) {
30+
["key"]=>
31+
object(stdClass)#2 (0) {
32+
}
33+
["value"]=>
34+
int(123)
35+
}
36+
[1]=>
37+
array(2) {
38+
["key"]=>
39+
object(stdClass)#4 (0) {
40+
}
41+
["value"]=>
42+
int(0)
43+
}
44+
[2]=>
45+
array(2) {
46+
["key"]=>
47+
object(stdClass)#5 (0) {
48+
}
49+
["value"]=>
50+
int(1)
51+
}
52+
[3]=>
53+
array(2) {
54+
["key"]=>
55+
object(stdClass)#6 (0) {
56+
}
57+
["value"]=>
58+
int(2)
59+
}
60+
[4]=>
61+
array(2) {
62+
["key"]=>
63+
object(stdClass)#7 (0) {
64+
}
65+
["value"]=>
66+
int(3)
67+
}
68+
[5]=>
69+
array(2) {
70+
["key"]=>
71+
object(stdClass)#8 (0) {
72+
}
73+
["value"]=>
74+
int(4)
75+
}
76+
[6]=>
77+
array(2) {
78+
["key"]=>
79+
object(stdClass)#9 (0) {
80+
}
81+
["value"]=>
82+
int(5)
83+
}
84+
[7]=>
85+
array(2) {
86+
["key"]=>
87+
object(stdClass)#10 (0) {
88+
}
89+
["value"]=>
90+
int(6)
91+
}
92+
[8]=>
93+
array(2) {
94+
["key"]=>
95+
object(stdClass)#11 (0) {
96+
}
97+
["value"]=>
98+
int(7)
99+
}
100+
[9]=>
101+
array(2) {
102+
["key"]=>
103+
object(stdClass)#12 (0) {
104+
}
105+
["value"]=>
106+
int(8)
107+
}
108+
[10]=>
109+
array(2) {
110+
["key"]=>
111+
object(stdClass)#13 (0) {
112+
}
113+
["value"]=>
114+
int(9)
115+
}
116+
}

Zend/zend_weakrefs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,12 @@ static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval
351351

352352
zval *zv = zend_hash_index_find(&wm->ht, (zend_ulong) obj_key);
353353
if (zv) {
354-
zval_ptr_dtor(zv);
354+
/* Because the destructors can have side effects such as resizing or rehashing the WeakMap storage,
355+
* free the zval only after overwriting the original value. */
356+
zval zv_orig;
357+
ZVAL_COPY_VALUE(&zv_orig, zv);
355358
ZVAL_COPY_VALUE(zv, value);
359+
zval_ptr_dtor(&zv_orig);
356360
return;
357361
}
358362

0 commit comments

Comments
 (0)