Skip to content

Adjust filename/lineno for constant expressions #8124

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
2 changes: 1 addition & 1 deletion Zend/tests/bug41633_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ echo Foo::A."\n";
Fatal error: Uncaught Error: Undefined constant self::B in %s:%d
Stack trace:
#0 {main}
thrown in %sbug41633_2.php on line 5
thrown in %sbug41633_2.php on line 3
15 changes: 15 additions & 0 deletions Zend/tests/gh7771_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
GH-7771 (Incorrect file/line for class constant expression exceptions)
--FILE--
<?php

include __DIR__ . '/gh7771_1_definition.inc';

new Foo();

?>
--EXPECTF--
Fatal error: Uncaught Error: Class "NonExistent" not found in %sgh7771_1_definition.inc:4
Stack trace:
#0 {main}
thrown in %sgh7771_1_definition.inc on line 4
5 changes: 5 additions & 0 deletions Zend/tests/gh7771_1_definition.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class Foo {
public const BAR = NonExistent::CLASS_CONSTANT;
}
15 changes: 15 additions & 0 deletions Zend/tests/gh7771_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
GH-7771 (Incorrect file/line for class constant expression exceptions)
--FILE--
<?php

include __DIR__ . '/gh7771_2_definition.inc';

new Foo();

?>
--EXPECTF--
Fatal error: Uncaught Error: Class "NonExistent" not found in %sgh7771_2_definition.inc:6
Stack trace:
#0 {main}
thrown in %sgh7771_2_definition.inc on line 6
8 changes: 8 additions & 0 deletions Zend/tests/gh7771_2_definition.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class Foo {
public const BAR =
self::BAZ
+ NonExistent::CLASS_CONSTANT;
public const BAZ = 42;
}
14 changes: 14 additions & 0 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,20 @@ ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast
{
zend_string *class_name = zend_ast_get_str(ast->child[0]);
zend_string *const_name = zend_ast_get_str(ast->child[1]);

zend_string *previous_filename;
zend_long previous_lineno;
if (scope) {
previous_filename = EG(filename_override);
previous_lineno = EG(lineno_override);
EG(filename_override) = scope->info.user.filename;
EG(lineno_override) = zend_ast_get_lineno(ast);
}
zval *zv = zend_get_class_constant_ex(class_name, const_name, scope, ast->attr);
if (scope) {
EG(filename_override) = previous_filename;
EG(lineno_override) = previous_lineno;
}

if (UNEXPECTED(zv == NULL)) {
ZVAL_UNDEF(result);
Expand Down Expand Up @@ -951,6 +964,7 @@ static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
zend_ast *new = (zend_ast*)buf;
new->kind = ast->kind;
new->attr = ast->attr;
new->lineno = ast->lineno;
buf = (void*)((char*)buf + zend_ast_size(children));
for (i = 0; i < children; i++) {
if (ast->child[i]) {
Expand Down
27 changes: 17 additions & 10 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ void init_executor(void) /* {{{ */
EG(num_errors) = 0;
EG(errors) = NULL;

EG(filename_override) = NULL;
EG(lineno_override) = -1;

zend_fiber_init();
zend_weakrefs_init();

Expand Down Expand Up @@ -450,6 +453,8 @@ void shutdown_executor(void) /* {{{ */
if (EG(ht_iterators) != EG(ht_iterators_slots)) {
efree(EG(ht_iterators));
}

ZEND_ASSERT(EG(filename_override) == NULL);
}

#if ZEND_DEBUG
Expand Down Expand Up @@ -579,21 +584,18 @@ ZEND_API const char *get_function_arg_name(const zend_function *func, uint32_t a

ZEND_API const char *zend_get_executed_filename(void) /* {{{ */
{
zend_execute_data *ex = EG(current_execute_data);

while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
ex = ex->prev_execute_data;
}
if (ex) {
return ZSTR_VAL(ex->func->op_array.filename);
} else {
return "[no active file]";
}
zend_string *filename = zend_get_executed_filename_ex();
return filename != NULL ? ZSTR_VAL(filename) : "[no active file]";
}
/* }}} */

ZEND_API zend_string *zend_get_executed_filename_ex(void) /* {{{ */
{
zend_string *filename_override = EG(filename_override);
if (filename_override != NULL) {
return filename_override;
}

zend_execute_data *ex = EG(current_execute_data);

while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
Expand All @@ -609,6 +611,11 @@ ZEND_API zend_string *zend_get_executed_filename_ex(void) /* {{{ */

ZEND_API uint32_t zend_get_executed_lineno(void) /* {{{ */
{
zend_long lineno_override = EG(lineno_override);
if (lineno_override != -1) {
return lineno_override;
}

zend_execute_data *ex = EG(current_execute_data);

while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ struct _zend_executor_globals {
uint32_t num_errors;
zend_error_info **errors;

/* Override filename or line number of thrown errors and exceptions */
zend_string *filename_override;
zend_long lineno_override;

void *reserved[ZEND_MAX_RESERVED_RESOURCES];
};

Expand Down