Skip to content

Commit 3fba242

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: [ci skip] NEWS Fix GH-13612: Corrupted memory in destructor with weak references
2 parents 88e90c6 + 608ef99 commit 3fba242

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PHP NEWS
55
- Core:
66
. Fixed GH-13569 (GC buffer unnecessarily grows up to GC_MAX_BUF_SIZE when
77
scanning WeakMaps). (Arnaud)
8+
. Fixed bug GH-13612 (Corrupted memory in destructor with weak references).
9+
(nielsdos)
810

911
- Gettext:
1012
. Fixed sigabrt raised with dcgettext/dcngettext calls with gettext 0.22.5

Zend/tests/weakrefs/gh13612.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
GH-13612 (Corrupted memory in destructor with weak references)
3+
--FILE--
4+
<?php
5+
6+
class WeakAnalysingMapRepro
7+
{
8+
public array $destroyed = [];
9+
public array $ownerDestructorHandlers = [];
10+
11+
public function __construct()
12+
{
13+
$handler = new class($this) {
14+
private \WeakReference $weakAnalysingMap;
15+
16+
public function __construct(WeakAnalysingMapRepro $analysingMap)
17+
{
18+
$this->weakAnalysingMap = \WeakReference::create($analysingMap);
19+
}
20+
21+
public function __destruct()
22+
{
23+
var_dump($this->weakAnalysingMap->get());
24+
}
25+
};
26+
27+
$this->destroyed[] = 1;
28+
$this->ownerDestructorHandlers[] = $handler;
29+
}
30+
}
31+
32+
new WeakAnalysingMapRepro();
33+
34+
echo "Done\n";
35+
36+
?>
37+
--EXPECT--
38+
NULL
39+
Done

Zend/zend_objects.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ ZEND_API void zend_object_std_dtor(zend_object *object)
5050
{
5151
zval *p, *end;
5252

53+
if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_WEAKLY_REFERENCED)) {
54+
zend_weakrefs_notify(object);
55+
}
56+
5357
if (object->properties) {
5458
if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
5559
if (EXPECTED(GC_DELREF(object->properties) == 0)
@@ -88,10 +92,6 @@ ZEND_API void zend_object_std_dtor(zend_object *object)
8892
FREE_HASHTABLE(guards);
8993
}
9094
}
91-
92-
if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_WEAKLY_REFERENCED)) {
93-
zend_weakrefs_notify(object);
94-
}
9595
}
9696

9797
ZEND_API void zend_objects_destroy_object(zend_object *object)

0 commit comments

Comments
 (0)