From 73f87cf5051c0e1fc2c280019a1dd80c950c3146 Mon Sep 17 00:00:00 2001 From: SATO Kentaro Date: Sat, 17 Feb 2024 00:23:12 +0900 Subject: [PATCH] Temporary reset filename and lineno override before autoload Closes GH-10232. --- NEWS | 4 ++++ Zend/tests/gh10232.phpt | 33 +++++++++++++++++++++++++++++ Zend/tests/gh10232/constant_def.inc | 12 +++++++++++ Zend/tests/gh10232/required.inc | 5 +++++ Zend/tests/gh7771_3.phpt | 2 +- Zend/zend_execute_API.c | 6 ++++++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 Zend/tests/gh10232.phpt create mode 100644 Zend/tests/gh10232/constant_def.inc create mode 100644 Zend/tests/gh10232/required.inc diff --git a/NEWS b/NEWS index 86fdebdb9affd..77e3ca5458775 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? ????, PHP 8.2.17 +- Core: + . Fixed bug GH-10232 (if autoloading occurs during constant resolution + filename and lineno are identified incorrectly). (ranvis) + - Curl: . Fix failing tests due to string changes in libcurl 8.6.0. (Ayesh) diff --git a/Zend/tests/gh10232.phpt b/Zend/tests/gh10232.phpt new file mode 100644 index 0000000000000..8e8d7ff86b5c6 --- /dev/null +++ b/Zend/tests/gh10232.phpt @@ -0,0 +1,33 @@ +--TEST-- +GH-10232 (Weird behaviour when a file is autoloaded in assignment of a constant) +--FILE-- +getLine(), "\n"; + require_once __DIR__ . '/gh10232/constant_def.inc'; +}, true); + + +class ConstantRef +{ + public const VALUE = ConstantDef::VALUE; +} + +ConstantRef::VALUE; + +?> +--EXPECTF-- +Notice: 7 in %sgh10232.php on line 7 +Exception on line 8 + +Notice: constant_def.inc in %sconstant_def.inc on line 3 +Exception in constant_def.inc on line 4 + +Notice: required.inc in %srequired.inc on line 3 +Exception in required.inc on line 4 diff --git a/Zend/tests/gh10232/constant_def.inc b/Zend/tests/gh10232/constant_def.inc new file mode 100644 index 0000000000000..22e469af63828 --- /dev/null +++ b/Zend/tests/gh10232/constant_def.inc @@ -0,0 +1,12 @@ +getFile()), ' on line ', $ex->getLine(), "\n"; + +require_once 'required.inc'; // The script of the same directory. + +class ConstantDef +{ + const VALUE = true; +} diff --git a/Zend/tests/gh10232/required.inc b/Zend/tests/gh10232/required.inc new file mode 100644 index 0000000000000..d5ff90da0211c --- /dev/null +++ b/Zend/tests/gh10232/required.inc @@ -0,0 +1,5 @@ +getFile()), ' on line ', $ex->getLine(), "\n"; diff --git a/Zend/tests/gh7771_3.phpt b/Zend/tests/gh7771_3.phpt index f6780814b89b4..e44a01a8aec74 100644 --- a/Zend/tests/gh7771_3.phpt +++ b/Zend/tests/gh7771_3.phpt @@ -12,5 +12,5 @@ spl_autoload_register(function($class) use ($classlist) { var_dump(D::HW); ?> --EXPECTF-- -Fatal error: Constant expression contains invalid operations in %sgh7771_3.php(7) : eval()'d code(1) : eval()'d code on line 1 +Fatal error: Constant expression contains invalid operations in %sgh7771_3.php(7) : eval()'d code on line 1 diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index fa5e13dba2ea8..46ddaade3c209 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -1205,9 +1205,15 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string * autoload_name = zend_string_copy(name); } + zend_string *previous_filename = EG(filename_override); + zend_long previous_lineno = EG(lineno_override); + EG(filename_override) = NULL; + EG(lineno_override) = -1; zend_exception_save(); ce = zend_autoload(autoload_name, lc_name); zend_exception_restore(); + EG(filename_override) = previous_filename; + EG(lineno_override) = previous_lineno; zend_string_release_ex(autoload_name, 0); zend_hash_del(EG(in_autoload), lc_name);