From 6434b5c5b638544420897c8aac3d7129339314ef Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Mon, 24 Feb 2025 10:36:01 -0800 Subject: [PATCH] Fix GH-17916: Final abstract properties should error --- NEWS | 2 ++ .../tests/property_hooks/abstract_prop_final.phpt | 14 ++++++++++++++ Zend/zend_compile.c | 15 +++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 Zend/tests/property_hooks/abstract_prop_final.phpt diff --git a/NEWS b/NEWS index 69c6e954b66b5..74a03c5c3a53f 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,8 @@ PHP NEWS `__callStatic` is allowed). (timwolla) . Fixed bug GH-17713 (ReflectionProperty::getRawValue() and related methods may call hooks of overridden properties). (Arnaud) + . Fixed bug GH-17916 (Final abstract properties should error). + (DanielEScherzer) - DOM: . Fixed bug GH-17609 (Typo in error message: Dom\NO_DEFAULT_NS instead of diff --git a/Zend/tests/property_hooks/abstract_prop_final.phpt b/Zend/tests/property_hooks/abstract_prop_final.phpt new file mode 100644 index 0000000000000..17f2282507f1f --- /dev/null +++ b/Zend/tests/property_hooks/abstract_prop_final.phpt @@ -0,0 +1,14 @@ +--TEST-- +GH-17916: Abstract property cannot be marked as final +--FILE-- + +--EXPECTF-- +Fatal error: Cannot use the final modifier on an abstract property in %s on line %d diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 667257f730658..ef75b45ad0528 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -1036,10 +1036,17 @@ uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifi "Multiple readonly modifiers are not allowed", 0); return 0; } - if (target == ZEND_MODIFIER_TARGET_METHOD && (new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) { - zend_throw_exception(zend_ce_compile_error, - "Cannot use the final modifier on an abstract method", 0); - return 0; + if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) { + if (target == ZEND_MODIFIER_TARGET_METHOD) { + zend_throw_exception(zend_ce_compile_error, + "Cannot use the final modifier on an abstract method", 0); + return 0; + } + if (target == ZEND_MODIFIER_TARGET_PROPERTY) { + zend_throw_exception(zend_ce_compile_error, + "Cannot use the final modifier on an abstract property", 0); + return 0; + } } if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {