Skip to content

Commit af3d2f7

Browse files
committed
Fix double-free of doc_comment when overriding static property via trait
When redeclaring an overridden static property with a trait we're removing the property from the class. However, because the property itself does not belong to the class we must not free its associated data. This issue is exposed by 9a250cc in PHP 8.3+ because duplicate static properties in traits are no longer skipped, but redeclared. Fixes GH-12468
1 parent 07d8159 commit af3d2f7

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.1.26
44

5+
- Core:
6+
. Fixed bug GH-12468 (Double-free of doc_comment when overriding static
7+
property via trait). (ilutov)
8+
59
- DOM:
610
. Fix registerNodeClass with abstract class crashing. (nielsdos)
711

Zend/tests/gh12468_1.phpt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-12468: Double-free of doc_comment when overriding static property via trait
3+
--FILE--
4+
<?php
5+
trait T {
6+
/** some doc */
7+
static protected $a = 0;
8+
}
9+
class A {
10+
use T;
11+
}
12+
class B extends A {
13+
use T;
14+
}
15+
?>
16+
===DONE===
17+
--EXPECT--
18+
===DONE===

Zend/tests/gh12468_2.phpt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-12468: Double-free of doc_comment when overriding static property via trait
3+
--FILE--
4+
<?php
5+
trait T {
6+
/** some doc */
7+
static protected $a = 0;
8+
}
9+
class A {
10+
/** some doc */
11+
static protected $a = 0;
12+
}
13+
class B extends A {
14+
use T;
15+
}
16+
?>
17+
===DONE===
18+
--EXPECT--
19+
===DONE===

Zend/zend_API.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4120,7 +4120,7 @@ ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, z
41204120
(property_info_ptr->flags & ZEND_ACC_STATIC) != 0) {
41214121
property_info->offset = property_info_ptr->offset;
41224122
zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
4123-
if (property_info_ptr->doc_comment) {
4123+
if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
41244124
zend_string_release(property_info_ptr->doc_comment);
41254125
}
41264126
zend_hash_del(&ce->properties_info, name);
@@ -4145,7 +4145,7 @@ ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, z
41454145
(property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
41464146
property_info->offset = property_info_ptr->offset;
41474147
zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
4148-
if (property_info_ptr->doc_comment) {
4148+
if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) {
41494149
zend_string_release_ex(property_info_ptr->doc_comment, 1);
41504150
}
41514151
zend_hash_del(&ce->properties_info, name);

0 commit comments

Comments
 (0)