Skip to content

Commit bdf5202

Browse files
committed
Fix "Constant already defined" warning with repeated inclusion of file with __halt_compiler()
Fixes GH-18850
1 parent fd8dfe1 commit bdf5202

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Zend/tests/constants/gh18850.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
var_dump(__COMPILER_HALT_OFFSET__);
4+
5+
__halt_compiler();

Zend/tests/constants/gh18850.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-18850: Repeated inclusion of file with __halt_compiler() triggers "Constant already defined" warning
3+
--FILE--
4+
<?php
5+
6+
require __DIR__ . '/gh18850.inc';
7+
require __DIR__ . '/gh18850.inc';
8+
9+
?>
10+
--EXPECT--
11+
int(62)
12+
int(62)

Zend/zend_compile.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9752,7 +9752,11 @@ static void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */
97529752
name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
97539753
ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
97549754

9755-
zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
9755+
/* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
9756+
* case this file was already included. */
9757+
if (!zend_hash_find(EG(zend_constants), name)) {
9758+
zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
9759+
}
97569760
zend_string_release_ex(name, 0);
97579761
}
97589762
/* }}} */

0 commit comments

Comments
 (0)