From 284fc61521af43f039ec19cae2ef08d093476c0b Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 8 Nov 2022 16:53:08 +0100 Subject: [PATCH 1/2] Fix GH-9905: constant() behaves inconsistent when class is undefined Directly referring to a constant of an undefined throws an exception; there is not much point in `constant()` raising a fatal error in this case. --- ext/standard/basic_functions.c | 2 +- ext/standard/tests/general_functions/gh9905.phpt | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 ext/standard/tests/general_functions/gh9905.phpt diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 23f736d200905..1e50a37f2c687 100755 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -589,7 +589,7 @@ PHP_FUNCTION(constant) ZEND_PARSE_PARAMETERS_END(); scope = zend_get_executed_scope(); - c = zend_get_constant_ex(const_name, scope, 0); + c = zend_get_constant_ex(const_name, scope, ZEND_FETCH_CLASS_EXCEPTION); if (!c) { RETURN_THROWS(); } diff --git a/ext/standard/tests/general_functions/gh9905.phpt b/ext/standard/tests/general_functions/gh9905.phpt new file mode 100644 index 0000000000000..33d2f4f737026 --- /dev/null +++ b/ext/standard/tests/general_functions/gh9905.phpt @@ -0,0 +1,12 @@ +--TEST-- +GH-9905 (constant() behaves inconsistent when class is undefined) +--FILE-- +getMessage()); +} +?> +--EXPECT-- +Class "NonExistantClass" not found From 4c138d96d42b28229a8f04c46c1d4493ae9f7632 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 8 Nov 2022 17:13:35 +0100 Subject: [PATCH 2/2] Update existing test case --- tests/lang/bug44827.phpt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/lang/bug44827.phpt b/tests/lang/bug44827.phpt index 9220439c884bd..997a1a633f853 100644 --- a/tests/lang/bug44827.phpt +++ b/tests/lang/bug44827.phpt @@ -21,5 +21,4 @@ try { ?> --EXPECTF-- define(): Argument #1 ($constant_name) cannot be a class constant - -Fatal error: Class "" not found in %s on line %d +Class "" not found