Skip to content

Fix "Constant already defined" warning with repeated inclusion of file with __halt_compiler() #19471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Zend/tests/constants/gh18850.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

var_dump(__COMPILER_HALT_OFFSET__);

__halt_compiler();
12 changes: 12 additions & 0 deletions Zend/tests/constants/gh18850.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
GH-18850: Repeated inclusion of file with __halt_compiler() triggers "Constant already defined" warning
--FILE--
<?php

require __DIR__ . '/gh18850.inc';
require __DIR__ . '/gh18850.inc';

?>
--EXPECT--
int(62)
int(62)
6 changes: 5 additions & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -9752,7 +9752,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);
}
/* }}} */
Expand Down
2 changes: 0 additions & 2 deletions ext/phar/tests/bug77432.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---

Expand Down