Skip to content

Commit de7ef3f

Browse files
committed
Fix lineno in function redeclaration error
We were previously using the lineno of the first instruction, rather than the start of the function itself. Fixes GH-16509 Closes GH-16531
1 parent 69bcbdc commit de7ef3f

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ PHP NEWS
1515
. Fixed bug GH-16371 (Assertion failure in Zend/zend_weakrefs.c:646). (Arnaud)
1616
. Fixed bug GH-16515 (Incorrect propagation of ZEND_ACC_RETURN_REFERENCE for
1717
call trampoline). (ilutov)
18+
. Fixed bug GH-16509 (Incorrect line number in function redeclaration error).
19+
(ilutov)
1820

1921
- Curl:
2022
. Fixed bug GH-16302 (CurlMultiHandle holds a reference to CurlHandle if

Zend/tests/gh16509.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
function test() {
4+
5+
6+
echo 'foo';
7+
}

Zend/tests/gh16509.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
GH-16509: Incorrect lineno reported for function redeclaration
3+
--FILE--
4+
<?php
5+
6+
include __DIR__ . '/gh16509.inc';
7+
include __DIR__ . '/gh16509.inc';
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Cannot redeclare test() (previously declared in %sgh16509.inc:3) in %sgh16509.inc on line 3

Zend/zend_compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(zen
10721072
zend_error_noreturn(error_level, "Cannot redeclare %s() (previously declared in %s:%d)",
10731073
op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
10741074
ZSTR_VAL(old_function->op_array.filename),
1075-
old_function->op_array.opcodes[0].lineno);
1075+
old_function->op_array.line_start);
10761076
} else {
10771077
zend_error_noreturn(error_level, "Cannot redeclare %s()",
10781078
op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
@@ -7516,6 +7516,7 @@ static void zend_compile_func_decl(znode *result, zend_ast *ast, bool toplevel)
75167516
} else if (toplevel) {
75177517
/* Only register the function after a successful compile */
75187518
if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
7519+
CG(zend_lineno) = decl->start_lineno;
75197520
do_bind_function_error(lcname, op_array, true);
75207521
}
75217522
}

ext/opcache/zend_accelerator_util_funcs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ static zend_always_inline void _zend_accel_function_hash_copy(HashTable *target,
170170
function2 = Z_PTR_P(t);
171171
CG(in_compilation) = 1;
172172
zend_set_compiled_filename(function1->op_array.filename);
173-
CG(zend_lineno) = function1->op_array.opcodes[0].lineno;
173+
CG(zend_lineno) = function1->op_array.line_start;
174174
if (function2->type == ZEND_USER_FUNCTION
175175
&& function2->op_array.last > 0) {
176176
zend_error(E_ERROR, "Cannot redeclare %s() (previously declared in %s:%d)",
177177
ZSTR_VAL(function1->common.function_name),
178178
ZSTR_VAL(function2->op_array.filename),
179-
(int)function2->op_array.opcodes[0].lineno);
179+
(int)function2->op_array.line_start);
180180
} else {
181181
zend_error(E_ERROR, "Cannot redeclare %s()", ZSTR_VAL(function1->common.function_name));
182182
}

0 commit comments

Comments
 (0)