Skip to content
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

Wrong line & file on error trace #18105

Open
ElliseProd opened this issue Mar 18, 2025 · 3 comments
Open

Wrong line & file on error trace #18105

ElliseProd opened this issue Mar 18, 2025 · 3 comments

Comments

@ElliseProd
Copy link

ElliseProd commented Mar 18, 2025

Description

definitions.php

<?php

class ThrowableParam {
	
   public function __construct() {
		
       // this will be line #7 for definitions.php:
      throw new Exception(sprintf('I expect to indicate file %s on line %s but it throws', basename(__FILE__), __LINE__));
		
   }
	
}

trait TraitInASeparateFile {

    // but this will be line #16 for definitions.php:		
    public function execute(ThrowableParam $param=new ThrowableParam): void {}
	
}

?>

main.php

<?php

include './definitions.php';

class Foo  {

    use TraitInASeparateFile;

}

$bar=new Foo;
$bar->execute(); // line #11 for main.php

// So, there is no line #16 for main.php

?>

RESULT :

Fatal error: Uncaught Exception: I expect to indicate file definitions.php on line 7 but it throws in ./main.php:16

  Stack trace:
  #0 ./definitions.php(7): constant expression
  #1 ./definitions.php(16): ThrowableParam->__construct()
  #2 ./main/php(11): Foo->execute()
  #3 {main}
  thrown in /var/www/html/nbcloud/_tests/bugPHP.php on line 16

BUT EXPECT :

Fatal error: Uncaught Exception: I expect to indicate file definitions.php on line 7 but it throws in ./definitions.php:7

  Stack trace:
  #0 ./definitions.php(7): constant expression
  #1 ./definitions.php(16): ThrowableParam->__construct()
  #2 ./main/php(11): Foo->execute()
  #3 {main}
  thrown in /var/www/html/nbcloud/_tests/bugPHP.php on line 16

PHP Version

8.4.5

Operating System

Debian 11

@ElliseProd ElliseProd changed the title wrong line & file on error trace Wrong line & file on error trace Mar 18, 2025
@nielsdos
Copy link
Member

Also on 8.3

@nielsdos
Copy link
Member

nielsdos commented Mar 18, 2025

The problem is that here the filename is set "incorrectly":

ZVAL_STRING(&tmp, zend_get_executed_filename());
zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
zval_ptr_dtor(&tmp);
ZVAL_LONG(&tmp, zend_get_executed_lineno());
zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);

The line number comes from the trait as that is stored by the compiler in the AST.
The filename comes from the zend_default_exception_new function that sets the file property. The TraitInASeparateFile::execute op_array when ZEND_RECV_INIT evalutes the constant expression, which has the filename definitions.php. zend_ast_evaluate_inner causes a VM re-entry and so the top stack frame now is the ThrowableParam::__construct and now somehow EG(filename_override) is set??

@nielsdos
Copy link
Member

Okay I see. zval_update_constant_with_ctx is called with scope set to EX(func)->op_array.scope which is the scope for the trait clone in main.php. Then later on zend_ast_evaluate_ex overrides the filename and line number. Then when the exception is created the overridden filename (which is now main.php due to the passed scope) gets used...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants