From 8286e032e60974b779d7aa795e9424dc5cd03151 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Wed, 11 Sep 2024 18:43:47 -0700 Subject: [PATCH 1/7] GH-15723: add `ReflectionConstant::getFileName()` Allow determining the name of the file that defined a constant, when the constant was defined in userland code via `define()`. For constants defined by PHP core or extensions, `false` is returned, matching the existing `getFileName()` methods on other reflection classes. --- Zend/zend_builtin_functions.c | 7 ++++++ Zend/zend_constants.c | 18 +++++++++++++++ Zend/zend_constants.h | 1 + Zend/zend_execute_API.c | 3 +++ ext/reflection/php_reflection.c | 14 ++++++++++++ ext/reflection/php_reflection.stub.php | 2 ++ ext/reflection/php_reflection_arginfo.h | 7 +++++- .../tests/ReflectionConstant_getFileName.phpt | 22 +++++++++++++++++++ ext/reflection/tests/included5.inc | 3 +++ 9 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/ReflectionConstant_getFileName.phpt create mode 100644 ext/reflection/tests/included5.inc diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index bb8bb28bf6e4..e9051cdffff4 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -549,6 +549,13 @@ ZEND_FUNCTION(define) /* non persistent */ ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT); c.name = zend_string_copy(name); + + zend_string *filename = zend_get_executed_filename_ex(); + if (filename == NULL) { + c.filename = NULL; + } else { + c.filename = zend_string_copy(filename); + } if (zend_register_constant(&c) == SUCCESS) { RETURN_TRUE; } else { diff --git a/Zend/zend_constants.c b/Zend/zend_constants.c index d453b8bb7371..8a6ca896d1fe 100644 --- a/Zend/zend_constants.c +++ b/Zend/zend_constants.c @@ -46,12 +46,18 @@ void free_zend_constant(zval *zv) if (c->name) { zend_string_release_ex(c->name, 0); } + if (c->filename) { + zend_string_release_ex(c->filename, 0); + } efree(c); } else { zval_internal_ptr_dtor(&c->value); if (c->name) { zend_string_release_ex(c->name, 1); } + if (c->filename) { + zend_string_release_ex(c->filename, 1); + } free(c); } } @@ -68,6 +74,9 @@ static void copy_zend_constant(zval *zv) c = Z_PTR_P(zv); c->name = zend_string_copy(c->name); + if (c->filename != NULL) { + c->filename = zend_string_copy(c->filename); + } if (Z_TYPE(c->value) == IS_STRING) { Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1); } @@ -123,6 +132,7 @@ ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int ZVAL_NULL(&c.value); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); + c.filename = NULL; zend_register_constant(&c); } @@ -133,6 +143,7 @@ ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, boo ZVAL_BOOL(&c.value, bval); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); + c.filename = NULL; zend_register_constant(&c); } @@ -143,6 +154,7 @@ ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zen ZVAL_LONG(&c.value, lval); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); + c.filename = NULL; zend_register_constant(&c); } @@ -154,6 +166,7 @@ ZEND_API void zend_register_double_constant(const char *name, size_t name_len, d ZVAL_DOUBLE(&c.value, dval); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); + c.filename = NULL; zend_register_constant(&c); } @@ -165,6 +178,7 @@ ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT)); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); + c.filename = NULL; zend_register_constant(&c); } @@ -502,6 +516,10 @@ ZEND_API zend_result zend_register_constant(zend_constant *c) ) { zend_error(E_WARNING, "Constant %s already defined", ZSTR_VAL(name)); zend_string_release(c->name); + if (c->filename) { + zend_string_release(c->filename); + c->filename = NULL; + } if (!persistent) { zval_ptr_dtor_nogc(&c->value); } diff --git a/Zend/zend_constants.h b/Zend/zend_constants.h index 6f0710c0ce63..fbefe0011ae1 100644 --- a/Zend/zend_constants.h +++ b/Zend/zend_constants.h @@ -33,6 +33,7 @@ typedef struct _zend_constant { zval value; zend_string *name; + zend_string *filename; } zend_constant; #define ZEND_CONSTANT_FLAGS(c) \ diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index f42479cf745d..9ebc15f3a43f 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -299,6 +299,9 @@ ZEND_API void zend_shutdown_executor_values(bool fast_shutdown) if (c->name) { zend_string_release_ex(c->name, 0); } + if (c->filename) { + zend_string_release_ex(c->filename, 0); + } efree(c); zend_string_release_ex(key, 0); } ZEND_HASH_MAP_FOREACH_END_DEL(); diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index a5562f165f76..fd3b883c3051 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -7835,6 +7835,20 @@ ZEND_METHOD(ReflectionConstant, isDeprecated) RETURN_BOOL(ZEND_CONSTANT_FLAGS(const_) & CONST_DEPRECATED); } +ZEND_METHOD(ReflectionConstant, getFileName) +{ + reflection_object *intern; + zend_constant *const_; + + ZEND_PARSE_PARAMETERS_NONE(); + + GET_REFLECTION_OBJECT_PTR(const_); + if (const_->filename != NULL) { + RETURN_STR_COPY(const_->filename); + } + RETURN_FALSE; +} + ZEND_METHOD(ReflectionConstant, __toString) { reflection_object *intern; diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php index d9acc08d2242..399166d5e53f 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -914,5 +914,7 @@ public function getValue(): mixed {} public function isDeprecated(): bool {} + public function getFileName(): string|false {} + public function __toString(): string {} } diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h index 92d80de5a60c..3a6c3b6ad835 100644 --- a/ext/reflection/php_reflection_arginfo.h +++ b/ext/reflection/php_reflection_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 1cdf310b94e2297a4e426bd4c0c1ab4d5995936d */ + * Stub hash: 1412eb6e3b1093bbd4144985cca0796d30668dbb */ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 1, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0) @@ -703,6 +703,9 @@ ZEND_END_ARG_INFO() #define arginfo_class_ReflectionConstant_isDeprecated arginfo_class_ReflectionFunctionAbstract_hasTentativeReturnType +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_ReflectionConstant_getFileName, 0, 0, MAY_BE_STRING|MAY_BE_FALSE) +ZEND_END_ARG_INFO() + #define arginfo_class_ReflectionConstant___toString arginfo_class_ReflectionFunction___toString ZEND_METHOD(Reflection, getModifierNames); @@ -968,6 +971,7 @@ ZEND_METHOD(ReflectionConstant, getNamespaceName); ZEND_METHOD(ReflectionConstant, getShortName); ZEND_METHOD(ReflectionConstant, getValue); ZEND_METHOD(ReflectionConstant, isDeprecated); +ZEND_METHOD(ReflectionConstant, getFileName); ZEND_METHOD(ReflectionConstant, __toString); static const zend_function_entry class_Reflection_methods[] = { @@ -1332,6 +1336,7 @@ static const zend_function_entry class_ReflectionConstant_methods[] = { ZEND_ME(ReflectionConstant, getShortName, arginfo_class_ReflectionConstant_getShortName, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionConstant, getValue, arginfo_class_ReflectionConstant_getValue, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionConstant, isDeprecated, arginfo_class_ReflectionConstant_isDeprecated, ZEND_ACC_PUBLIC) + ZEND_ME(ReflectionConstant, getFileName, arginfo_class_ReflectionConstant_getFileName, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionConstant, __toString, arginfo_class_ReflectionConstant___toString, ZEND_ACC_PUBLIC) ZEND_FE_END }; diff --git a/ext/reflection/tests/ReflectionConstant_getFileName.phpt b/ext/reflection/tests/ReflectionConstant_getFileName.phpt new file mode 100644 index 000000000000..cf700e5672d3 --- /dev/null +++ b/ext/reflection/tests/ReflectionConstant_getFileName.phpt @@ -0,0 +1,22 @@ +--TEST-- +ReflectionConstant::getFileName() +--FILE-- +getFileName()); + +define('IN_CURRENT_FILE', 42); +$reflectionConstant = new ReflectionConstant('IN_CURRENT_FILE'); +var_dump($reflectionConstant->getFileName()); + +$reflectionConstant = new ReflectionConstant('INCLUDED_CONSTANT'); +var_dump($reflectionConstant->getFileName()); + +?> +--EXPECTF-- +bool(false) +string(%d) "%sReflectionConstant_getFileName.php" +string(%d) "%sincluded5.inc" diff --git a/ext/reflection/tests/included5.inc b/ext/reflection/tests/included5.inc new file mode 100644 index 000000000000..47db60104e70 --- /dev/null +++ b/ext/reflection/tests/included5.inc @@ -0,0 +1,3 @@ + From 2fa74873b47781abfb22c25ec2a37219b7ea7918 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Wed, 11 Sep 2024 19:36:27 -0700 Subject: [PATCH 2/7] Handle constants declared with `const` --- Zend/zend_vm_def.h | 7 +++++++ Zend/zend_vm_execute.h | 7 +++++++ ext/reflection/tests/ReflectionConstant_getFileName.phpt | 6 +++++- ext/reflection/tests/included5.inc | 4 +++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index e19c50337931..a0c86ed06f8a 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -8232,6 +8232,13 @@ ZEND_VM_HANDLER(143, ZEND_DECLARE_CONST, CONST, CONST) ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT); c.name = zend_string_copy(Z_STR_P(name)); + zend_string *filename = zend_get_executed_filename_ex(); + if (filename == NULL) { + c.filename = NULL; + } else { + c.filename = zend_string_copy(filename); + } + if (zend_register_constant(&c) == FAILURE) { } diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index af9ea669b6b4..523f1ea17256 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -7981,6 +7981,13 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_DECLARE_CONST_SPEC_CONST_CONST ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT); c.name = zend_string_copy(Z_STR_P(name)); + zend_string *filename = zend_get_executed_filename_ex(); + if (filename == NULL) { + c.filename = NULL; + } else { + c.filename = zend_string_copy(filename); + } + if (zend_register_constant(&c) == FAILURE) { } diff --git a/ext/reflection/tests/ReflectionConstant_getFileName.phpt b/ext/reflection/tests/ReflectionConstant_getFileName.phpt index cf700e5672d3..abdccefd682c 100644 --- a/ext/reflection/tests/ReflectionConstant_getFileName.phpt +++ b/ext/reflection/tests/ReflectionConstant_getFileName.phpt @@ -12,7 +12,10 @@ define('IN_CURRENT_FILE', 42); $reflectionConstant = new ReflectionConstant('IN_CURRENT_FILE'); var_dump($reflectionConstant->getFileName()); -$reflectionConstant = new ReflectionConstant('INCLUDED_CONSTANT'); +$reflectionConstant = new ReflectionConstant('INCLUDED_CONSTANT_DEFINED'); +var_dump($reflectionConstant->getFileName()); + +$reflectionConstant = new ReflectionConstant('INCLUDED_CONSTANT_AST'); var_dump($reflectionConstant->getFileName()); ?> @@ -20,3 +23,4 @@ var_dump($reflectionConstant->getFileName()); bool(false) string(%d) "%sReflectionConstant_getFileName.php" string(%d) "%sincluded5.inc" +string(%d) "%sincluded5.inc" diff --git a/ext/reflection/tests/included5.inc b/ext/reflection/tests/included5.inc index 47db60104e70..d406ce0f0f6a 100644 --- a/ext/reflection/tests/included5.inc +++ b/ext/reflection/tests/included5.inc @@ -1,3 +1,5 @@ From 986db8a7927743b214a49f16549f1a76538b1d3d Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Wed, 11 Sep 2024 20:24:45 -0700 Subject: [PATCH 3/7] Fix some manually registered constants --- ext/com_dotnet/com_typeinfo.c | 6 ++++++ sapi/cli/php_cli.c | 3 +++ sapi/phpdbg/phpdbg.c | 3 +++ 3 files changed, 12 insertions(+) diff --git a/ext/com_dotnet/com_typeinfo.c b/ext/com_dotnet/com_typeinfo.c index 4f6a3309b8cd..8f424fa3159e 100644 --- a/ext/com_dotnet/com_typeinfo.c +++ b/ext/com_dotnet/com_typeinfo.c @@ -233,6 +233,12 @@ PHP_COM_DOTNET_API zend_result php_com_import_typelib(ITypeLib *TL, int mode, in } else { c.name = const_name; } + zend_string *filename = zend_get_executed_filename_ex(); + if (filename == NULL) { + c.filename = NULL; + } else { + c.filename = zend_string_copy(filename); + } zend_register_constant(&c); } ITypeInfo_ReleaseVarDesc(TypeInfo, pVarDesc); diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 2731a1f93738..0eb9f4dca3f2 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -546,14 +546,17 @@ static void cli_register_file_handles(void) Z_CONSTANT_FLAGS(ic.value) = 0; ic.name = zend_string_init_interned("STDIN", sizeof("STDIN")-1, 0); + ic.filename = NULL; zend_register_constant(&ic); Z_CONSTANT_FLAGS(oc.value) = 0; oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT")-1, 0); + oc.filename = NULL; zend_register_constant(&oc); Z_CONSTANT_FLAGS(ec.value) = 0; ec.name = zend_string_init_interned("STDERR", sizeof("STDERR")-1, 0); + ec.filename = NULL; zend_register_constant(&ec); } diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index f298a8029fae..fc0dfb200e38 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -914,18 +914,21 @@ void phpdbg_register_file_handles(void) /* {{{ */ ic.value = zin; Z_CONSTANT_FLAGS(ic.value) = 0; ic.name = zend_string_init(ZEND_STRL("STDIN"), 0); + ic.filename = NULL; zend_hash_del(EG(zend_constants), ic.name); zend_register_constant(&ic); oc.value = zout; Z_CONSTANT_FLAGS(oc.value) = 0; oc.name = zend_string_init(ZEND_STRL("STDOUT"), 0); + oc.filename = NULL; zend_hash_del(EG(zend_constants), oc.name); zend_register_constant(&oc); ec.value = zerr; Z_CONSTANT_FLAGS(ec.value) = 0; ec.name = zend_string_init(ZEND_STRL("STDERR"), 0); + ec.filename = NULL; zend_hash_del(EG(zend_constants), ec.name); zend_register_constant(&ec); } From a6084158b7ff7164711dde11305245c2d047cf94 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Wed, 18 Sep 2024 19:06:00 -0700 Subject: [PATCH 4/7] Expand `ReflectionConstant::getFileName()` tests --- .../tests/ReflectionConstant_getFileName.phpt | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/ext/reflection/tests/ReflectionConstant_getFileName.phpt b/ext/reflection/tests/ReflectionConstant_getFileName.phpt index abdccefd682c..05190f95737b 100644 --- a/ext/reflection/tests/ReflectionConstant_getFileName.phpt +++ b/ext/reflection/tests/ReflectionConstant_getFileName.phpt @@ -5,22 +5,40 @@ ReflectionConstant::getFileName() include "included5.inc"; -$reflectionConstant = new ReflectionConstant('PHP_VERSION'); -var_dump($reflectionConstant->getFileName()); +function testConstant( string $name ): void { + $ref = new ReflectionConstant( $name ); + echo "$name: "; + var_dump( $ref->getFileName() ); +} -define('IN_CURRENT_FILE', 42); -$reflectionConstant = new ReflectionConstant('IN_CURRENT_FILE'); -var_dump($reflectionConstant->getFileName()); +define('IN_CURRENT_FILE_DEFINED', 42); +const IN_CURRENT_FILE_AST = 123; -$reflectionConstant = new ReflectionConstant('INCLUDED_CONSTANT_DEFINED'); -var_dump($reflectionConstant->getFileName()); +echo "From PHP:\n"; +testConstant( 'PHP_VERSION' ); +testConstant( 'STDIN' ); +testConstant( 'STDOUT' ); +testConstant( 'STDERR' ); -$reflectionConstant = new ReflectionConstant('INCLUDED_CONSTANT_AST'); -var_dump($reflectionConstant->getFileName()); +echo "\nFrom the current file:\n"; +testConstant( 'IN_CURRENT_FILE_DEFINED' ); +testConstant( 'IN_CURRENT_FILE_AST' ); +echo "\nFrom an included file:\n"; +testConstant( 'INCLUDED_CONSTANT_DEFINED' ); +testConstant( 'INCLUDED_CONSTANT_AST' ); ?> --EXPECTF-- -bool(false) -string(%d) "%sReflectionConstant_getFileName.php" -string(%d) "%sincluded5.inc" -string(%d) "%sincluded5.inc" +From PHP: +PHP_VERSION: bool(false) +STDIN: bool(false) +STDOUT: bool(false) +STDERR: bool(false) + +From the current file: +IN_CURRENT_FILE_DEFINED: string(%d) "%sReflectionConstant_getFileName.php" +IN_CURRENT_FILE_AST: string(%d) "%sReflectionConstant_getFileName.php" + +From an included file: +INCLUDED_CONSTANT_DEFINED: string(%d) "%sincluded5.inc" +INCLUDED_CONSTANT_AST: string(%d) "%sincluded5.inc" \ No newline at end of file From 3dc98524033e3a7f3c3220c670b8ac320ed42b0a Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Tue, 8 Oct 2024 01:50:32 -0700 Subject: [PATCH 5/7] UPGRADING for `ReflectionConstant::getFileName()` --- UPGRADING | 3 +++ 1 file changed, 3 insertions(+) diff --git a/UPGRADING b/UPGRADING index 6a7a54f71745..acb2a163cdb2 100644 --- a/UPGRADING +++ b/UPGRADING @@ -88,6 +88,9 @@ PHP 8.5 UPGRADE NOTES statement from the DEALLOCATE sql command in that we can reuse its name afterwards. +- Reflection: + . ReflectionConstant::getFileName() was introduced. + ======================================== 7. New Classes and Interfaces ======================================== From afb7feeb2f94b850aadaceed8893bda84321cb70 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Tue, 8 Oct 2024 18:23:52 -0700 Subject: [PATCH 6/7] ext/reflection/tests/ReflectionConstant_getFileName.phpt: whitespace fixes --- .../tests/ReflectionConstant_getFileName.phpt | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ext/reflection/tests/ReflectionConstant_getFileName.phpt b/ext/reflection/tests/ReflectionConstant_getFileName.phpt index 05190f95737b..b627acceec4b 100644 --- a/ext/reflection/tests/ReflectionConstant_getFileName.phpt +++ b/ext/reflection/tests/ReflectionConstant_getFileName.phpt @@ -5,28 +5,28 @@ ReflectionConstant::getFileName() include "included5.inc"; -function testConstant( string $name ): void { - $ref = new ReflectionConstant( $name ); +function testConstant(string $name): void { + $ref = new ReflectionConstant($name); echo "$name: "; - var_dump( $ref->getFileName() ); + var_dump($ref->getFileName()); } define('IN_CURRENT_FILE_DEFINED', 42); const IN_CURRENT_FILE_AST = 123; echo "From PHP:\n"; -testConstant( 'PHP_VERSION' ); -testConstant( 'STDIN' ); -testConstant( 'STDOUT' ); -testConstant( 'STDERR' ); +testConstant('PHP_VERSION'); +testConstant('STDIN'); +testConstant('STDOUT'); +testConstant('STDERR'); echo "\nFrom the current file:\n"; -testConstant( 'IN_CURRENT_FILE_DEFINED' ); -testConstant( 'IN_CURRENT_FILE_AST' ); +testConstant('IN_CURRENT_FILE_DEFINED'); +testConstant('IN_CURRENT_FILE_AST'); echo "\nFrom an included file:\n"; -testConstant( 'INCLUDED_CONSTANT_DEFINED' ); -testConstant( 'INCLUDED_CONSTANT_AST' ); +testConstant('INCLUDED_CONSTANT_DEFINED'); +testConstant('INCLUDED_CONSTANT_AST'); ?> --EXPECTF-- From PHP: @@ -41,4 +41,4 @@ IN_CURRENT_FILE_AST: string(%d) "%sReflectionConstant_getFileName.php" From an included file: INCLUDED_CONSTANT_DEFINED: string(%d) "%sincluded5.inc" -INCLUDED_CONSTANT_AST: string(%d) "%sincluded5.inc" \ No newline at end of file +INCLUDED_CONSTANT_AST: string(%d) "%sincluded5.inc" From 701b5e50c7c2aa2576ada80c0969ae6980f1c2f3 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Fri, 25 Oct 2024 06:20:08 -0700 Subject: [PATCH 7/7] Deduplicate zend_constant filename setting in `zend_register_constant()` --- Zend/zend_builtin_functions.c | 7 ------- Zend/zend_constants.c | 12 +++++++----- Zend/zend_vm_def.h | 7 ------- Zend/zend_vm_execute.h | 7 ------- ext/com_dotnet/com_typeinfo.c | 6 ------ sapi/cli/php_cli.c | 3 --- sapi/phpdbg/phpdbg.c | 3 --- 7 files changed, 7 insertions(+), 38 deletions(-) diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index e9051cdffff4..bb8bb28bf6e4 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -549,13 +549,6 @@ ZEND_FUNCTION(define) /* non persistent */ ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT); c.name = zend_string_copy(name); - - zend_string *filename = zend_get_executed_filename_ex(); - if (filename == NULL) { - c.filename = NULL; - } else { - c.filename = zend_string_copy(filename); - } if (zend_register_constant(&c) == SUCCESS) { RETURN_TRUE; } else { diff --git a/Zend/zend_constants.c b/Zend/zend_constants.c index 8a6ca896d1fe..9206eb8e1638 100644 --- a/Zend/zend_constants.c +++ b/Zend/zend_constants.c @@ -132,7 +132,6 @@ ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int ZVAL_NULL(&c.value); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); - c.filename = NULL; zend_register_constant(&c); } @@ -143,7 +142,6 @@ ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, boo ZVAL_BOOL(&c.value, bval); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); - c.filename = NULL; zend_register_constant(&c); } @@ -154,7 +152,6 @@ ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zen ZVAL_LONG(&c.value, lval); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); - c.filename = NULL; zend_register_constant(&c); } @@ -166,7 +163,6 @@ ZEND_API void zend_register_double_constant(const char *name, size_t name_len, d ZVAL_DOUBLE(&c.value, dval); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); - c.filename = NULL; zend_register_constant(&c); } @@ -178,7 +174,6 @@ ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT)); ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); - c.filename = NULL; zend_register_constant(&c); } @@ -509,6 +504,13 @@ ZEND_API zend_result zend_register_constant(zend_constant *c) name = c->name; } + zend_string *filename = zend_get_executed_filename_ex(); + if (filename == NULL) { + c->filename = NULL; + } else { + c->filename = zend_string_copy(filename); + } + /* Check if the user is trying to define any special constant */ if (zend_string_equals_literal(name, "__COMPILER_HALT_OFFSET__") || (!persistent && zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name))) diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index a0c86ed06f8a..e19c50337931 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -8232,13 +8232,6 @@ ZEND_VM_HANDLER(143, ZEND_DECLARE_CONST, CONST, CONST) ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT); c.name = zend_string_copy(Z_STR_P(name)); - zend_string *filename = zend_get_executed_filename_ex(); - if (filename == NULL) { - c.filename = NULL; - } else { - c.filename = zend_string_copy(filename); - } - if (zend_register_constant(&c) == FAILURE) { } diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 523f1ea17256..af9ea669b6b4 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -7981,13 +7981,6 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_DECLARE_CONST_SPEC_CONST_CONST ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT); c.name = zend_string_copy(Z_STR_P(name)); - zend_string *filename = zend_get_executed_filename_ex(); - if (filename == NULL) { - c.filename = NULL; - } else { - c.filename = zend_string_copy(filename); - } - if (zend_register_constant(&c) == FAILURE) { } diff --git a/ext/com_dotnet/com_typeinfo.c b/ext/com_dotnet/com_typeinfo.c index 8f424fa3159e..4f6a3309b8cd 100644 --- a/ext/com_dotnet/com_typeinfo.c +++ b/ext/com_dotnet/com_typeinfo.c @@ -233,12 +233,6 @@ PHP_COM_DOTNET_API zend_result php_com_import_typelib(ITypeLib *TL, int mode, in } else { c.name = const_name; } - zend_string *filename = zend_get_executed_filename_ex(); - if (filename == NULL) { - c.filename = NULL; - } else { - c.filename = zend_string_copy(filename); - } zend_register_constant(&c); } ITypeInfo_ReleaseVarDesc(TypeInfo, pVarDesc); diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 0eb9f4dca3f2..2731a1f93738 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -546,17 +546,14 @@ static void cli_register_file_handles(void) Z_CONSTANT_FLAGS(ic.value) = 0; ic.name = zend_string_init_interned("STDIN", sizeof("STDIN")-1, 0); - ic.filename = NULL; zend_register_constant(&ic); Z_CONSTANT_FLAGS(oc.value) = 0; oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT")-1, 0); - oc.filename = NULL; zend_register_constant(&oc); Z_CONSTANT_FLAGS(ec.value) = 0; ec.name = zend_string_init_interned("STDERR", sizeof("STDERR")-1, 0); - ec.filename = NULL; zend_register_constant(&ec); } diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index fc0dfb200e38..f298a8029fae 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -914,21 +914,18 @@ void phpdbg_register_file_handles(void) /* {{{ */ ic.value = zin; Z_CONSTANT_FLAGS(ic.value) = 0; ic.name = zend_string_init(ZEND_STRL("STDIN"), 0); - ic.filename = NULL; zend_hash_del(EG(zend_constants), ic.name); zend_register_constant(&ic); oc.value = zout; Z_CONSTANT_FLAGS(oc.value) = 0; oc.name = zend_string_init(ZEND_STRL("STDOUT"), 0); - oc.filename = NULL; zend_hash_del(EG(zend_constants), oc.name); zend_register_constant(&oc); ec.value = zerr; Z_CONSTANT_FLAGS(ec.value) = 0; ec.name = zend_string_init(ZEND_STRL("STDERR"), 0); - ec.filename = NULL; zend_hash_del(EG(zend_constants), ec.name); zend_register_constant(&ec); }