From 910f579f14f06dcfc4e31d21522ae08f6f78e287 Mon Sep 17 00:00:00 2001 From: Florian Sowade Date: Mon, 18 Sep 2023 14:28:39 +0200 Subject: [PATCH] Fix GH-12207 memory leak of doc blocks of static properties When declaring the same static property with a doc block in a class and in a trait, the doc block of the property in the class is leaked. While at it, possibly fix doc comment for internal classes. Close GH-12238 --- NEWS | 4 ++++ ...idding-static-property-with-doc-block.phpt | 21 +++++++++++++++++++ Zend/zend_API.c | 6 ++++++ 3 files changed, 31 insertions(+) create mode 100644 Zend/tests/traits/bugs/overridding-static-property-with-doc-block.phpt diff --git a/NEWS b/NEWS index 5b999380c14..6f0c68ad73c 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? ????, PHP 8.1.25 +- Core: + . Fixed bug GH-12207 (memory leak when class using trait with doc block). + (rioderelfte) + - Filter: . Fix explicit FILTER_REQUIRE_SCALAR with FILTER_CALLBACK (ilutov) diff --git a/Zend/tests/traits/bugs/overridding-static-property-with-doc-block.phpt b/Zend/tests/traits/bugs/overridding-static-property-with-doc-block.phpt new file mode 100644 index 00000000000..51c733d6625 --- /dev/null +++ b/Zend/tests/traits/bugs/overridding-static-property-with-doc-block.phpt @@ -0,0 +1,21 @@ +--TEST-- +Overriding a static property where both declarations have a doc block does not leak memory +--FILE-- + +--EXPECT-- diff --git a/Zend/zend_API.c b/Zend/zend_API.c index f59035be3f1..11781330466 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -4120,6 +4120,9 @@ ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, z (property_info_ptr->flags & ZEND_ACC_STATIC) != 0) { property_info->offset = property_info_ptr->offset; zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]); + if (property_info_ptr->doc_comment) { + zend_string_release(property_info_ptr->doc_comment); + } zend_hash_del(&ce->properties_info, name); } else { property_info->offset = ce->default_static_members_count++; @@ -4142,6 +4145,9 @@ ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, z (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) { property_info->offset = property_info_ptr->offset; zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]); + if (property_info_ptr->doc_comment) { + zend_string_release_ex(property_info_ptr->doc_comment, 1); + } zend_hash_del(&ce->properties_info, name); ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);