From de7ef3fa66b3904b23e70fb1245ee9afb38173ec Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Mon, 21 Oct 2024 16:27:45 +0200 Subject: [PATCH] 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 --- NEWS | 2 ++ Zend/tests/gh16509.inc | 7 +++++++ Zend/tests/gh16509.phpt | 11 +++++++++++ Zend/zend_compile.c | 3 ++- ext/opcache/zend_accelerator_util_funcs.c | 4 ++-- 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 Zend/tests/gh16509.inc create mode 100644 Zend/tests/gh16509.phpt diff --git a/NEWS b/NEWS index 38957adc4cb..15bb87a607a 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,8 @@ PHP NEWS . Fixed bug GH-16371 (Assertion failure in Zend/zend_weakrefs.c:646). (Arnaud) . Fixed bug GH-16515 (Incorrect propagation of ZEND_ACC_RETURN_REFERENCE for call trampoline). (ilutov) + . Fixed bug GH-16509 (Incorrect line number in function redeclaration error). + (ilutov) - Curl: . Fixed bug GH-16302 (CurlMultiHandle holds a reference to CurlHandle if diff --git a/Zend/tests/gh16509.inc b/Zend/tests/gh16509.inc new file mode 100644 index 00000000000..297699fb671 --- /dev/null +++ b/Zend/tests/gh16509.inc @@ -0,0 +1,7 @@ + +--EXPECTF-- +Fatal error: Cannot redeclare test() (previously declared in %sgh16509.inc:3) in %sgh16509.inc on line 3 diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 8662e188aaa..5e79152f257 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -1072,7 +1072,7 @@ static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(zen zend_error_noreturn(error_level, "Cannot redeclare %s() (previously declared in %s:%d)", op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name), ZSTR_VAL(old_function->op_array.filename), - old_function->op_array.opcodes[0].lineno); + old_function->op_array.line_start); } else { zend_error_noreturn(error_level, "Cannot redeclare %s()", 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) } else if (toplevel) { /* Only register the function after a successful compile */ if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) { + CG(zend_lineno) = decl->start_lineno; do_bind_function_error(lcname, op_array, true); } } diff --git a/ext/opcache/zend_accelerator_util_funcs.c b/ext/opcache/zend_accelerator_util_funcs.c index 2ee60d4bbc4..e504b0203a6 100644 --- a/ext/opcache/zend_accelerator_util_funcs.c +++ b/ext/opcache/zend_accelerator_util_funcs.c @@ -170,13 +170,13 @@ failure: function2 = Z_PTR_P(t); CG(in_compilation) = 1; zend_set_compiled_filename(function1->op_array.filename); - CG(zend_lineno) = function1->op_array.opcodes[0].lineno; + CG(zend_lineno) = function1->op_array.line_start; if (function2->type == ZEND_USER_FUNCTION && function2->op_array.last > 0) { zend_error(E_ERROR, "Cannot redeclare %s() (previously declared in %s:%d)", ZSTR_VAL(function1->common.function_name), ZSTR_VAL(function2->op_array.filename), - (int)function2->op_array.opcodes[0].lineno); + (int)function2->op_array.line_start); } else { zend_error(E_ERROR, "Cannot redeclare %s()", ZSTR_VAL(function1->common.function_name)); }