From 9d69ab91ab1401fab9f5167edbe8cedcc8e0fecd Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 5 Sep 2025 18:53:43 +0200 Subject: [PATCH] Fix GH-19720: Assertion failure when error handler throws when accessing a deprecated constant When deprecation causes an exception, we should return NULL instead of continuing. Closes GH-19723. --- NEWS | 2 ++ Zend/zend_API.c | 3 +++ Zend/zend_constants.c | 3 +++ ext/zend_test/tests/gh19720.phpt | 22 ++++++++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 ext/zend_test/tests/gh19720.phpt diff --git a/NEWS b/NEWS index 80d74f373a1..36b2f9b9fc9 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,8 @@ PHP NEWS . Fixed bug GH-19613 (Stale array iterator pointer). (ilutov) . Fixed bug GH-19679 (zend_ssa_range_widening may fail to converge). (Arnaud) . Fixed bug GH-19681 (PHP_EXPAND_PATH broken with bash 5.3.0). (Remi) + . Fixed bug GH-19720 (Assertion failure when error handler throws when + accessing a deprecated constant). (nielsdos) - CLI: . Fixed bug GH-19461 (Improve error message on listening error with IPv6 diff --git a/Zend/zend_API.c b/Zend/zend_API.c index 47de6e78975..79800a61a04 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -1459,6 +1459,9 @@ ZEND_API zend_result zend_update_class_constant(zend_class_constant *c, const ze zval_ptr_dtor(&c->value); ZVAL_COPY_VALUE(&c->value, &tmp); + /* may not return SUCCESS in case of an exception, + * should've returned FAILURE in zval_update_constant_ex! */ + ZEND_ASSERT(!EG(exception)); return SUCCESS; } diff --git a/Zend/zend_constants.c b/Zend/zend_constants.c index 6530c1f063d..f2033475d6e 100644 --- a/Zend/zend_constants.c +++ b/Zend/zend_constants.c @@ -536,6 +536,9 @@ failure: if (!(flags & ZEND_FETCH_CLASS_SILENT) && (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)) { zend_error(E_DEPRECATED, "Constant %s is deprecated", name); + if (UNEXPECTED(EG(exception))) { + return NULL; + } } return &c->value; } diff --git a/ext/zend_test/tests/gh19720.phpt b/ext/zend_test/tests/gh19720.phpt new file mode 100644 index 00000000000..627444c4793 --- /dev/null +++ b/ext/zend_test/tests/gh19720.phpt @@ -0,0 +1,22 @@ +--TEST-- +GH-19720 (Assertion failure when error handler throws when accessing a deprecated constant) +--EXTENSIONS-- +zend_test +--FILE-- + 42]; +} + +set_error_handler(function ($_, $errstr) { + throw new Exception($errstr); +}); + +try { + var_dump(Test::MyConst); +} catch (Exception $e) { + echo $e->getMessage(), "\n"; +} +?> +--EXPECT-- +Constant ZEND_TEST_DEPRECATED is deprecated