From: Nikita Popov Date: Fri, 10 Jan 2020 11:01:42 +0000 (+0100) Subject: Make constant() error handling consistent with plain const lookup X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7ce531f2c28dcfe4aeed271b55b82de65c3bca8a;p=php Make constant() error handling consistent with plain const lookup This means we get an Error exception and a much better error message indicating the root cause (e.g. accessing a private class constant). --- diff --git a/Zend/tests/018.phpt b/Zend/tests/018.phpt index 97e53b1050..74c5edcb1f 100644 --- a/Zend/tests/018.phpt +++ b/Zend/tests/018.phpt @@ -3,7 +3,11 @@ constant() tests --FILE-- getMessage(), "\n"; +} define("TEST_CONST", 1); var_dump(constant("TEST_CONST")); @@ -13,9 +17,8 @@ var_dump(constant("TEST_CONST2")); echo "Done\n"; ?> ---EXPECTF-- -Warning: constant(): Couldn't find constant in %s on line %d -NULL +--EXPECT-- +Undefined constant '' int(1) string(4) "test" Done diff --git a/Zend/tests/bug51791.phpt b/Zend/tests/bug51791.phpt index 0b92fa8c62..beb50bad90 100644 --- a/Zend/tests/bug51791.phpt +++ b/Zend/tests/bug51791.phpt @@ -6,9 +6,12 @@ Bug #51791 (constant() failed to check undefined constant and php interpreter st class A { const B = 1; } -var_dump(constant('A::B1')); +try { + constant('A::B1'); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} ?> ---EXPECTF-- -Warning: constant(): Couldn't find constant A::B1 in %s on line %d -NULL +--EXPECT-- +Undefined class constant 'A::B1' diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index ef903be1e9..860c94acfb 100755 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1285,19 +1285,16 @@ PHP_FUNCTION(constant) ZEND_PARSE_PARAMETERS_END(); scope = zend_get_executed_scope(); - c = zend_get_constant_ex(const_name, scope, ZEND_FETCH_CLASS_SILENT); - if (c) { - ZVAL_COPY_OR_DUP(return_value, c); - if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) { - if (UNEXPECTED(zval_update_constant_ex(return_value, scope) != SUCCESS)) { - return; - } - } - } else { - if (!EG(exception)) { - php_error_docref(NULL, E_WARNING, "Couldn't find constant %s", ZSTR_VAL(const_name)); + c = zend_get_constant_ex(const_name, scope, 0); + if (!c) { + RETURN_THROWS(); + } + + ZVAL_COPY_OR_DUP(return_value, c); + if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) { + if (UNEXPECTED(zval_update_constant_ex(return_value, scope) != SUCCESS)) { + RETURN_THROWS(); } - RETURN_NULL(); } } /* }}} */ diff --git a/ext/standard/tests/general_functions/bug72920.phpt b/ext/standard/tests/general_functions/bug72920.phpt index 8ba4d26713..d3508044fb 100644 --- a/ext/standard/tests/general_functions/bug72920.phpt +++ b/ext/standard/tests/general_functions/bug72920.phpt @@ -6,7 +6,11 @@ class Foo { private const C1 = "a"; } -var_dump(constant('Foo::C1')); ---EXPECTF-- -Warning: constant(): Couldn't find constant Foo::C1 in %s on line %d -NULL +try { + var_dump(constant('Foo::C1')); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} +?> +--EXPECT-- +Cannot access private const Foo::C1 diff --git a/tests/classes/constants_visibility_002.phpt b/tests/classes/constants_visibility_002.phpt index 4e0ecb1aa2..56c70a99a1 100644 --- a/tests/classes/constants_visibility_002.phpt +++ b/tests/classes/constants_visibility_002.phpt @@ -14,11 +14,14 @@ class A { A::staticConstDump(); (new A())->constDump(); -constant('A::protectedConst'); +try { + constant('A::protectedConst'); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} ?> ---EXPECTF-- +--EXPECT-- string(14) "protectedConst" string(14) "protectedConst" - -Warning: constant(): Couldn't find constant A::protectedConst in %s on line %d +Cannot access protected const A::protectedConst diff --git a/tests/classes/constants_visibility_003.phpt b/tests/classes/constants_visibility_003.phpt index 7c961695ed..fe9bb6f6a1 100644 --- a/tests/classes/constants_visibility_003.phpt +++ b/tests/classes/constants_visibility_003.phpt @@ -14,11 +14,14 @@ class A { A::staticConstDump(); (new A())->constDump(); -constant('A::privateConst'); +try { + constant('A::privateConst'); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} ?> ---EXPECTF-- +--EXPECT-- string(12) "privateConst" string(12) "privateConst" - -Warning: constant(): Couldn't find constant A::privateConst in %s on line %d +Cannot access private const A::privateConst diff --git a/tests/lang/bug44827.phpt b/tests/lang/bug44827.phpt index d871cfd2a8..9cb8c132e7 100644 --- a/tests/lang/bug44827.phpt +++ b/tests/lang/bug44827.phpt @@ -7,10 +7,13 @@ Testfest Munich 2009 --FILE-- getMessage(), "\n"; +} ?> --EXPECTF-- Warning: Class constants cannot be defined or redefined in %s on line %d -Warning: constant(): Couldn't find constant :: in %s on line %d -NULL +Fatal error: Class '' not found in %s on line %d