From a3de2ce9ba43244cbbd0bfa5ee978ffaaca8a133 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Wed, 13 Aug 2025 15:23:42 +0200 Subject: [PATCH] Fix "Constant already defined" warning with repeated inclusion of file with __halt_compiler() Fixes GH-18850 Closes GH-19471 --- NEWS | 4 ++++ Zend/tests/constants/gh18850.inc | 5 +++++ Zend/tests/constants/gh18850.phpt | 12 ++++++++++++ Zend/zend_compile.c | 6 +++++- ext/phar/tests/bug77432.phpt | 2 -- 5 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 Zend/tests/constants/gh18850.inc create mode 100644 Zend/tests/constants/gh18850.phpt diff --git a/NEWS b/NEWS index 998ef70e1d4..d23b71bb54a 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? ????, PHP 8.3.26 +- Core: + . Fixed bug GH-18850 (Repeated inclusion of file with __halt_compiler() + triggers "Constant already defined" warning). (ilutov) + - OpenSSL: . Fixed bug GH-19245 (Success error message on TLS stream accept failure). (Jakub Zelenka) diff --git a/Zend/tests/constants/gh18850.inc b/Zend/tests/constants/gh18850.inc new file mode 100644 index 00000000000..08099278ea3 --- /dev/null +++ b/Zend/tests/constants/gh18850.inc @@ -0,0 +1,5 @@ + +--EXPECT-- +int(62) +int(62) diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 6c8577cef58..5d25b396383 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -8607,7 +8607,11 @@ static void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */ name = zend_mangle_property_name(const_name, sizeof(const_name) - 1, ZSTR_VAL(filename), ZSTR_LEN(filename), 0); - zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0); + /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in + * case this file was already included. */ + if (!zend_hash_find(EG(zend_constants), name)) { + zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0); + } zend_string_release_ex(name, 0); } /* }}} */ diff --git a/ext/phar/tests/bug77432.phpt b/ext/phar/tests/bug77432.phpt index b3b927a5439..3e3a9988c7d 100644 --- a/ext/phar/tests/bug77432.phpt +++ b/ext/phar/tests/bug77432.phpt @@ -38,8 +38,6 @@ include("phar://" . $filename); --- Include 1 --- hello world --- Include 2 --- - -Warning: Constant already defined in %s on line %d hello world --- After unlink ---