Skip to content

Commit 9f4bd30

Browse files
committed
Revert "Remove name field from the zend_constant struct (php#10954)"
This reverts commit f42992f. Fix phpGH-11423
1 parent 45a3f17 commit 9f4bd30

15 files changed

+81
-68
lines changed

UPGRADING.INTERNALS

-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ PHP 8.3 INTERNALS UPGRADE NOTES
4242
* The order of members of zend_op_array, zend_ssa_var, zend_ssa_var_info,
4343
zend_executor_globals and php_core_globals have changed to improve
4444
struct packing which reduces their size.
45-
* The name field have been removed from the zend_constant struct. Now,
46-
constant names are only stored as keys of the global constants table.
47-
That's why the `zend_register_constant()` function now expects the
48-
constant name as its first parameter.
4945
* Many calls to zend_assign_to_variable have been replaced with
5046
zend_assign_to_variable_ex which allows delaying the releasing of the old
5147
variable value. This avoids side-effects through destructors between the

Zend/zend_builtin_functions.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ ZEND_FUNCTION(define)
511511
register_constant:
512512
/* non persistent */
513513
ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT);
514-
if (zend_register_constant(name, &c) == SUCCESS) {
514+
c.name = zend_string_copy(name);
515+
if (zend_register_constant(&c) == SUCCESS) {
515516
RETURN_TRUE;
516517
} else {
517518
RETURN_FALSE;
@@ -1479,7 +1480,6 @@ ZEND_FUNCTION(get_defined_constants)
14791480
zend_constant *val;
14801481
int module_number;
14811482
zval *modules, const_val;
1482-
zend_string *const_name;
14831483
char **module_names;
14841484
zend_module_entry *module;
14851485
int i = 1;
@@ -1494,7 +1494,12 @@ ZEND_FUNCTION(get_defined_constants)
14941494
} ZEND_HASH_FOREACH_END();
14951495
module_names[i] = "user";
14961496

1497-
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(zend_constants), const_name, val) {
1497+
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), val) {
1498+
if (!val->name) {
1499+
/* skip special constants */
1500+
continue;
1501+
}
1502+
14981503
if (ZEND_CONSTANT_MODULE_NUMBER(val) == PHP_USER_CONSTANT) {
14991504
module_number = i;
15001505
} else if (ZEND_CONSTANT_MODULE_NUMBER(val) > i) {
@@ -1510,19 +1515,22 @@ ZEND_FUNCTION(get_defined_constants)
15101515
}
15111516

15121517
ZVAL_COPY_OR_DUP(&const_val, &val->value);
1513-
zend_hash_add_new(Z_ARRVAL(modules[module_number]), const_name, &const_val);
1518+
zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
15141519
} ZEND_HASH_FOREACH_END();
15151520

15161521
efree(module_names);
15171522
efree(modules);
15181523
} else {
15191524
zend_constant *constant;
1520-
zend_string *const_name;
15211525
zval const_val;
15221526

1523-
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(zend_constants), const_name, constant) {
1527+
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
1528+
if (!constant->name) {
1529+
/* skip special constants */
1530+
continue;
1531+
}
15241532
ZVAL_COPY_OR_DUP(&const_val, &constant->value);
1525-
zend_hash_add_new(Z_ARRVAL_P(return_value), const_name, &const_val);
1533+
zend_hash_add_new(Z_ARRVAL_P(return_value), constant->name, &const_val);
15261534
} ZEND_HASH_FOREACH_END();
15271535
}
15281536
}

Zend/zend_constants.c

+25-19
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ void free_zend_constant(zval *zv)
4343

4444
if (!(ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)) {
4545
zval_ptr_dtor_nogc(&c->value);
46+
if (c->name) {
47+
zend_string_release_ex(c->name, 0);
48+
}
4649
efree(c);
4750
} else {
4851
zval_internal_ptr_dtor(&c->value);
52+
if (c->name) {
53+
zend_string_release_ex(c->name, 1);
54+
}
4955
free(c);
5056
}
5157
}
@@ -61,6 +67,7 @@ static void copy_zend_constant(zval *zv)
6167
memcpy(Z_PTR_P(zv), c, sizeof(zend_constant));
6268

6369
c = Z_PTR_P(zv);
70+
c->name = zend_string_copy(c->name);
6471
if (Z_TYPE(c->value) == IS_STRING) {
6572
Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1);
6673
}
@@ -122,7 +129,8 @@ ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int
122129

123130
ZVAL_NULL(&c.value);
124131
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
125-
zend_register_internal_constant(name, name_len, &c);
132+
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
133+
zend_register_constant(&c);
126134
}
127135

128136
ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
@@ -131,7 +139,8 @@ ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, boo
131139

132140
ZVAL_BOOL(&c.value, bval);
133141
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
134-
zend_register_internal_constant(name, name_len, &c);
142+
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
143+
zend_register_constant(&c);
135144
}
136145

137146
ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
@@ -140,7 +149,8 @@ ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zen
140149

141150
ZVAL_LONG(&c.value, lval);
142151
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
143-
zend_register_internal_constant(name, name_len, &c);
152+
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
153+
zend_register_constant(&c);
144154
}
145155

146156

@@ -150,7 +160,8 @@ ZEND_API void zend_register_double_constant(const char *name, size_t name_len, d
150160

151161
ZVAL_DOUBLE(&c.value, dval);
152162
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
153-
zend_register_internal_constant(name, name_len, &c);
163+
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
164+
zend_register_constant(&c);
154165
}
155166

156167

@@ -160,7 +171,8 @@ ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len,
160171

161172
ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT));
162173
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
163-
zend_register_internal_constant(name, name_len, &c);
174+
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
175+
zend_register_constant(&c);
164176
}
165177

166178

@@ -533,22 +545,25 @@ static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_consta
533545
return ret;
534546
}
535547

536-
ZEND_API zend_result zend_register_constant(zend_string *name, zend_constant *c)
548+
ZEND_API zend_result zend_register_constant(zend_constant *c)
537549
{
538550
zend_string *lowercase_name = NULL;
551+
zend_string *name;
539552
zend_result ret = SUCCESS;
540553
bool persistent = (ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) != 0;
541554

542555
#if 0
543556
printf("Registering constant for module %d\n", c->module_number);
544557
#endif
545558

546-
const char *slash = strrchr(ZSTR_VAL(name), '\\');
559+
const char *slash = strrchr(ZSTR_VAL(c->name), '\\');
547560
if (slash) {
548-
lowercase_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), persistent);
549-
zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(name));
561+
lowercase_name = zend_string_init(ZSTR_VAL(c->name), ZSTR_LEN(c->name), persistent);
562+
zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(c->name));
550563
lowercase_name = zend_new_interned_string(lowercase_name);
551564
name = lowercase_name;
565+
} else {
566+
name = c->name;
552567
}
553568

554569
/* Check if the user is trying to define any special constant */
@@ -557,6 +572,7 @@ ZEND_API zend_result zend_register_constant(zend_string *name, zend_constant *c)
557572
|| zend_hash_add_constant(EG(zend_constants), name, c) == NULL
558573
) {
559574
zend_error(E_WARNING, "Constant %s already defined", ZSTR_VAL(name));
575+
zend_string_release(c->name);
560576
if (!persistent) {
561577
zval_ptr_dtor_nogc(&c->value);
562578
}
@@ -567,13 +583,3 @@ ZEND_API zend_result zend_register_constant(zend_string *name, zend_constant *c)
567583
}
568584
return ret;
569585
}
570-
571-
ZEND_API zend_result zend_register_internal_constant(const char *name, size_t name_len, zend_constant *c) {
572-
zend_string *name_str = zend_string_init_interned(name, name_len, ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT);
573-
574-
zend_result result = zend_register_constant(name_str, c);
575-
576-
zend_string_release(name_str);
577-
578-
return result;
579-
}

Zend/zend_constants.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
typedef struct _zend_constant {
3434
zval value;
35+
zend_string *name;
3536
} zend_constant;
3637

3738
#define ZEND_CONSTANT_FLAGS(c) \
@@ -83,8 +84,7 @@ ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zen
8384
ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number);
8485
ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number);
8586
ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number);
86-
ZEND_API zend_result zend_register_internal_constant(const char *name, size_t name_len, zend_constant *c);
87-
ZEND_API zend_result zend_register_constant(zend_string *name, zend_constant *c);
87+
ZEND_API zend_result zend_register_constant(zend_constant *c);
8888
#ifdef ZTS
8989
void zend_copy_constants(HashTable *target, HashTable *source);
9090
#endif

Zend/zend_execute.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5008,7 +5008,7 @@ static zend_always_inline zend_result _zend_quick_get_constant(
50085008
if (!check_defined_only) {
50095009
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
50105010
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
5011-
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(Z_STR_P(key)));
5011+
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
50125012
return SUCCESS;
50135013
}
50145014
}

Zend/zend_execute_API.c

+3
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ ZEND_API void zend_shutdown_executor_values(bool fast_shutdown)
294294
break;
295295
}
296296
zval_ptr_dtor_nogc(&c->value);
297+
if (c->name) {
298+
zend_string_release_ex(c->name, 0);
299+
}
297300
efree(c);
298301
zend_string_release_ex(key, 0);
299302
} ZEND_HASH_MAP_FOREACH_END_DEL();

Zend/zend_vm_def.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -8127,8 +8127,9 @@ ZEND_VM_HANDLER(143, ZEND_DECLARE_CONST, CONST, CONST)
81278127
}
81288128
/* non persistent, case sensitive */
81298129
ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT);
8130+
c.name = zend_string_copy(Z_STR_P(name));
81308131

8131-
if (zend_register_constant(Z_STR_P(name), &c) == FAILURE) {
8132+
if (zend_register_constant(&c) == FAILURE) {
81328133
}
81338134

81348135
FREE_OP1();

Zend/zend_vm_execute.h

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/com_dotnet/com_typeinfo.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ PHP_COM_DOTNET_API zend_result php_com_import_typelib(ITypeLib *TL, int mode, in
197197
if (pTKind == TKIND_ENUM) {
198198
ITypeLib_GetTypeInfo(TL, i, &TypeInfo);
199199
for (j = 0; ; j++) {
200-
zend_string *const_name, *name;
200+
zend_string *const_name;
201201

202202
if (FAILED(ITypeInfo_GetVarDesc(TypeInfo, j, &pVarDesc))) {
203203
break;
@@ -228,13 +228,12 @@ PHP_COM_DOTNET_API zend_result php_com_import_typelib(ITypeLib *TL, int mode, in
228228
ZVAL_LONG(&c.value, Z_LVAL(value));
229229
if (mode & CONST_PERSISTENT) {
230230
/* duplicate string in a persistent manner */
231-
name = zend_string_dup(const_name, /* persistent */ true);
231+
c.name = zend_string_dup(const_name, /* persistent */ true);
232232
zend_string_release_ex(const_name, /* persistent */ false);
233233
} else {
234-
name = const_name;
234+
c.name = const_name;
235235
}
236-
zend_register_constant(name, &c);
237-
zend_string_release(name);
236+
zend_register_constant(&c);
238237
}
239238
ITypeInfo_ReleaseVarDesc(TypeInfo, pVarDesc);
240239
}

ext/opcache/ZendAccelerator.c

+3
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,9 @@ static void accel_copy_permanent_strings(zend_new_interned_string_func_t new_int
735735
p->key = new_interned_string(p->key);
736736
}
737737
c = (zend_constant*)Z_PTR(p->val);
738+
if (c->name) {
739+
c->name = new_interned_string(c->name);
740+
}
738741
if (Z_TYPE(c->value) == IS_STRING) {
739742
ZVAL_STR(&c->value, new_interned_string(Z_STR(c->value)));
740743
}

ext/opcache/jit/zend_jit_vm_helpers.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static zend_always_inline zend_constant* _zend_quick_get_constant(
282282

283283
if (!check_defined_only) {
284284
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
285-
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(Z_STR_P(key)));
285+
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
286286
if (EG(exception)) {
287287
return NULL;
288288
}

ext/reflection/php_reflection.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -1045,13 +1045,11 @@ static void _extension_string(smart_str *str, zend_module_entry *module, char *i
10451045
{
10461046
smart_str str_constants = {0};
10471047
zend_constant *constant;
1048-
zend_string *name;
10491048
int num_constants = 0;
10501049

1051-
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(zend_constants), name, constant) {
1052-
1050+
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
10531051
if (ZEND_CONSTANT_MODULE_NUMBER(constant) == module->module_number) {
1054-
_const_string(&str_constants, ZSTR_VAL(name), &constant->value, indent);
1052+
_const_string(&str_constants, ZSTR_VAL(constant->name), &constant->value, indent);
10551053
num_constants++;
10561054
}
10571055
} ZEND_HASH_FOREACH_END();
@@ -6037,19 +6035,18 @@ ZEND_METHOD(ReflectionExtension, getConstants)
60376035
reflection_object *intern;
60386036
zend_module_entry *module;
60396037
zend_constant *constant;
6040-
zend_string *name;
60416038

60426039
if (zend_parse_parameters_none() == FAILURE) {
60436040
RETURN_THROWS();
60446041
}
60456042
GET_REFLECTION_OBJECT_PTR(module);
60466043

60476044
array_init(return_value);
6048-
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(zend_constants), name, constant) {
6045+
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
60496046
if (module->module_number == ZEND_CONSTANT_MODULE_NUMBER(constant)) {
60506047
zval const_val;
60516048
ZVAL_COPY_OR_DUP(&const_val, &constant->value);
6052-
zend_hash_update(Z_ARRVAL_P(return_value), name, &const_val);
6049+
zend_hash_update(Z_ARRVAL_P(return_value), constant->name, &const_val);
60536050
}
60546051
} ZEND_HASH_FOREACH_END();
60556052
}

sapi/cli/php_cli.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,16 @@ static void cli_register_file_handles(void)
558558
php_stream_to_zval(s_err, &ec.value);
559559

560560
Z_CONSTANT_FLAGS(ic.value) = 0;
561-
zend_register_internal_constant("STDIN", sizeof("STDIN")-1, &ic);
561+
ic.name = zend_string_init_interned("STDIN", sizeof("STDIN")-1, 0);
562+
zend_register_constant(&ic);
562563

563564
Z_CONSTANT_FLAGS(oc.value) = 0;
564-
zend_register_internal_constant("STDOUT", sizeof("STDOUT")-1, &oc);
565+
oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT")-1, 0);
566+
zend_register_constant(&oc);
565567

566568
Z_CONSTANT_FLAGS(ec.value) = 0;
567-
zend_register_internal_constant("STDERR", sizeof("STDERR")-1, &ec);
569+
ec.name = zend_string_init_interned("STDERR", sizeof("STDERR")-1, 0);
570+
zend_register_constant(&ec);
568571
}
569572

570573
static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n";

sapi/phpdbg/phpdbg.c

+9-12
Original file line numberDiff line numberDiff line change
@@ -912,24 +912,21 @@ void phpdbg_register_file_handles(void) /* {{{ */
912912

913913
ic.value = zin;
914914
Z_CONSTANT_FLAGS(ic.value) = 0;
915-
zend_string *stdin_name = zend_string_init(ZEND_STRL("STDIN"), 0);
916-
zend_hash_del(EG(zend_constants), stdin_name);
917-
zend_register_constant(stdin_name, &ic);
918-
zend_string_release(stdin_name);
915+
ic.name = zend_string_init(ZEND_STRL("STDIN"), 0);
916+
zend_hash_del(EG(zend_constants), ic.name);
917+
zend_register_constant(&ic);
919918

920919
oc.value = zout;
921920
Z_CONSTANT_FLAGS(oc.value) = 0;
922-
zend_string *stdout_name = zend_string_init(ZEND_STRL("STDOUT"), 0);
923-
zend_hash_del(EG(zend_constants), stdout_name);
924-
zend_register_constant(stdout_name, &oc);
925-
zend_string_release(stdout_name);
921+
oc.name = zend_string_init(ZEND_STRL("STDOUT"), 0);
922+
zend_hash_del(EG(zend_constants), oc.name);
923+
zend_register_constant(&oc);
926924

927925
ec.value = zerr;
928926
Z_CONSTANT_FLAGS(ec.value) = 0;
929-
zend_string *stderr_name = zend_string_init(ZEND_STRL("STDERR"), 0);
930-
zend_hash_del(EG(zend_constants), stderr_name);
931-
zend_register_constant(stderr_name, &ec);
932-
zend_string_release(stderr_name);
927+
ec.name = zend_string_init(ZEND_STRL("STDERR"), 0);
928+
zend_hash_del(EG(zend_constants), ec.name);
929+
zend_register_constant(&ec);
933930
}
934931
/* }}} */
935932

0 commit comments

Comments
 (0)