/src/php-src/Zend/zend_constants.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © Zend Technologies Ltd., a subsidiary company of | |
6 | | | Perforce Software, Inc., and Contributors. | |
7 | | +----------------------------------------------------------------------+ |
8 | | | This source file is subject to the Modified BSD License that is | |
9 | | | bundled with this package in the file LICENSE, and is available | |
10 | | | through the World Wide Web at <https://www.php.net/license/>. | |
11 | | | | |
12 | | | SPDX-License-Identifier: BSD-3-Clause | |
13 | | +----------------------------------------------------------------------+ |
14 | | | Authors: Andi Gutmans <andi@php.net> | |
15 | | | Zeev Suraski <zeev@php.net> | |
16 | | +----------------------------------------------------------------------+ |
17 | | */ |
18 | | |
19 | | #include "zend.h" |
20 | | #include "zend_attributes.h" |
21 | | #include "zend_constants.h" |
22 | | #include "zend_exceptions.h" |
23 | | #include "zend_execute.h" |
24 | | #include "zend_variables.h" |
25 | | #include "zend_operators.h" |
26 | | #include "zend_globals.h" |
27 | | #include "zend_API.h" |
28 | | #include "zend_constants_arginfo.h" |
29 | | |
30 | | /* Protection from recursive self-referencing class constants */ |
31 | 0 | #define IS_CONSTANT_VISITED_MARK 0x80 |
32 | | |
33 | 0 | #define IS_CONSTANT_VISITED(zv) (Z_CONSTANT_FLAGS_P(zv) & IS_CONSTANT_VISITED_MARK) |
34 | 0 | #define MARK_CONSTANT_VISITED(zv) Z_CONSTANT_FLAGS_P(zv) |= IS_CONSTANT_VISITED_MARK |
35 | 0 | #define RESET_CONSTANT_VISITED(zv) Z_CONSTANT_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK |
36 | | |
37 | | /* Use for special null/true/false constants. */ |
38 | | static zend_constant *null_const, *true_const, *false_const; |
39 | | |
40 | | void free_zend_constant(zval *zv) |
41 | 0 | { |
42 | 0 | zend_constant *c = Z_PTR_P(zv); |
43 | |
|
44 | 0 | if (!(ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)) { |
45 | 0 | zval_ptr_dtor_nogc(&c->value); |
46 | 0 | if (c->name) { |
47 | 0 | zend_string_release_ex(c->name, 0); |
48 | 0 | } |
49 | 0 | if (c->filename) { |
50 | 0 | zend_string_release_ex(c->filename, 0); |
51 | 0 | } |
52 | 0 | if (c->attributes) { |
53 | 0 | zend_hash_release(c->attributes); |
54 | 0 | } |
55 | 0 | efree(c); |
56 | 0 | } else { |
57 | 0 | zval_internal_ptr_dtor(&c->value); |
58 | 0 | if (c->name) { |
59 | 0 | zend_string_release_ex(c->name, 1); |
60 | 0 | } |
61 | 0 | if (c->filename) { |
62 | 0 | zend_string_release_ex(c->filename, 1); |
63 | 0 | } |
64 | 0 | if (c->attributes) { |
65 | 0 | zend_hash_release(c->attributes); |
66 | 0 | } |
67 | 0 | free(c); |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | |
72 | | #ifdef ZTS |
73 | | static void copy_zend_constant(zval *zv) |
74 | | { |
75 | | zend_constant *c = Z_PTR_P(zv); |
76 | | |
77 | | ZEND_ASSERT(ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT); |
78 | | Z_PTR_P(zv) = pemalloc(sizeof(zend_constant), 1); |
79 | | memcpy(Z_PTR_P(zv), c, sizeof(zend_constant)); |
80 | | |
81 | | c = Z_PTR_P(zv); |
82 | | c->name = zend_string_copy(c->name); |
83 | | if (c->filename != NULL) { |
84 | | c->filename = zend_string_copy(c->filename); |
85 | | } |
86 | | if (c->attributes != NULL) { |
87 | | // Use the same attributes table |
88 | | GC_ADDREF(c->attributes); |
89 | | } |
90 | | if (Z_TYPE(c->value) == IS_STRING) { |
91 | | Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1); |
92 | | } |
93 | | } |
94 | | |
95 | | |
96 | | void zend_copy_constants(HashTable *target, HashTable *source) |
97 | | { |
98 | | zend_hash_copy(target, source, copy_zend_constant); |
99 | | } |
100 | | #endif |
101 | | |
102 | | |
103 | | static int clean_module_constant(zval *el, void *arg) |
104 | 0 | { |
105 | 0 | zend_constant *c = (zend_constant *)Z_PTR_P(el); |
106 | 0 | int module_number = *(int *)arg; |
107 | |
|
108 | 0 | if (ZEND_CONSTANT_MODULE_NUMBER(c) == module_number) { |
109 | 0 | return ZEND_HASH_APPLY_REMOVE; |
110 | 0 | } else { |
111 | 0 | return ZEND_HASH_APPLY_KEEP; |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | | |
116 | | void clean_module_constants(int module_number) |
117 | 0 | { |
118 | 0 | zend_hash_apply_with_argument(EG(zend_constants), clean_module_constant, (void *) &module_number); |
119 | 0 | } |
120 | | |
121 | | void zend_startup_constants(void) |
122 | 0 | { |
123 | 0 | EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable)); |
124 | 0 | zend_hash_init(EG(zend_constants), 128, NULL, ZEND_CONSTANT_DTOR, 1); |
125 | 0 | } |
126 | | |
127 | | |
128 | | |
129 | | void zend_register_standard_constants(void) |
130 | 2 | { |
131 | 2 | register_zend_constants_symbols(0); |
132 | | |
133 | 2 | true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1); |
134 | 2 | false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1); |
135 | 2 | null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1); |
136 | 2 | } |
137 | | |
138 | | ZEND_API zend_constant *zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number) |
139 | 2 | { |
140 | 2 | zend_constant c; |
141 | | |
142 | 2 | ZVAL_NULL(&c.value); |
143 | 2 | ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); |
144 | 2 | c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); |
145 | 2 | return zend_register_constant(&c); |
146 | 2 | } |
147 | | |
148 | | ZEND_API zend_constant *zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number) |
149 | 14 | { |
150 | 14 | zend_constant c; |
151 | | |
152 | 14 | ZVAL_BOOL(&c.value, bval); |
153 | 14 | ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); |
154 | 14 | c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); |
155 | 14 | return zend_register_constant(&c); |
156 | 14 | } |
157 | | |
158 | | ZEND_API zend_constant *zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number) |
159 | 961 | { |
160 | 961 | zend_constant c; |
161 | | |
162 | 961 | ZVAL_LONG(&c.value, lval); |
163 | 961 | ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); |
164 | 961 | c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); |
165 | 961 | return zend_register_constant(&c); |
166 | 961 | } |
167 | | |
168 | | |
169 | | ZEND_API zend_constant *zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number) |
170 | 44 | { |
171 | 44 | zend_constant c; |
172 | | |
173 | 44 | ZVAL_DOUBLE(&c.value, dval); |
174 | 44 | ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); |
175 | 44 | c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); |
176 | 44 | return zend_register_constant(&c); |
177 | 44 | } |
178 | | |
179 | | |
180 | | ZEND_API zend_constant *zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number) |
181 | 86 | { |
182 | 86 | zend_constant c; |
183 | | |
184 | 86 | ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT)); |
185 | 86 | ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number); |
186 | 86 | c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT); |
187 | 86 | return zend_register_constant(&c); |
188 | 86 | } |
189 | | |
190 | | |
191 | | ZEND_API zend_constant *zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number) |
192 | 86 | { |
193 | 86 | return zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number); |
194 | 86 | } |
195 | | |
196 | | static zend_constant *zend_get_halt_offset_constant(const char *name, size_t name_len) |
197 | 0 | { |
198 | 0 | zend_constant *c; |
199 | 0 | static const char haltoff[] = "__COMPILER_HALT_OFFSET__"; |
200 | |
|
201 | 0 | if (!EG(current_execute_data)) { |
202 | 0 | return NULL; |
203 | 0 | } else if (name_len == sizeof("__COMPILER_HALT_OFFSET__")-1 && |
204 | 0 | !memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) { |
205 | 0 | const char *cfilename; |
206 | 0 | zend_string *haltname; |
207 | 0 | size_t clen; |
208 | |
|
209 | 0 | cfilename = zend_get_executed_filename(); |
210 | 0 | clen = strlen(cfilename); |
211 | | /* check for __COMPILER_HALT_OFFSET__ */ |
212 | 0 | haltname = zend_mangle_property_name(haltoff, |
213 | 0 | sizeof("__COMPILER_HALT_OFFSET__") - 1, cfilename, clen, 0); |
214 | 0 | c = zend_hash_find_ptr(EG(zend_constants), haltname); |
215 | 0 | zend_string_efree(haltname); |
216 | 0 | return c; |
217 | 0 | } else { |
218 | 0 | return NULL; |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | | ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */ |
223 | 2.17M | { |
224 | 2.17M | if (len == 4) { |
225 | 79.0k | if ((name[0] == 'n' || name[0] == 'N') && |
226 | 20.2k | (name[1] == 'u' || name[1] == 'U') && |
227 | 12.2k | (name[2] == 'l' || name[2] == 'L') && |
228 | 11.7k | (name[3] == 'l' || name[3] == 'L') |
229 | 79.0k | ) { |
230 | 11.3k | return null_const; |
231 | 11.3k | } |
232 | 67.6k | if ((name[0] == 't' || name[0] == 'T') && |
233 | 14.7k | (name[1] == 'r' || name[1] == 'R') && |
234 | 13.8k | (name[2] == 'u' || name[2] == 'U') && |
235 | 13.2k | (name[3] == 'e' || name[3] == 'E') |
236 | 67.6k | ) { |
237 | 2.05k | return true_const; |
238 | 2.05k | } |
239 | 2.09M | } else { |
240 | 2.09M | if ((name[0] == 'f' || name[0] == 'F') && |
241 | 11.7k | (name[1] == 'a' || name[1] == 'A') && |
242 | 2.75k | (name[2] == 'l' || name[2] == 'L') && |
243 | 1.22k | (name[3] == 's' || name[3] == 'S') && |
244 | 1.04k | (name[4] == 'e' || name[4] == 'E') |
245 | 2.09M | ) { |
246 | 822 | return false_const; |
247 | 822 | } |
248 | 2.09M | } |
249 | 2.15M | return NULL; |
250 | 2.17M | } |
251 | | /* }}} */ |
252 | | |
253 | | ZEND_API bool zend_verify_const_access(const zend_class_constant *c, const zend_class_entry *scope) /* {{{ */ |
254 | 0 | { |
255 | 0 | if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) { |
256 | 0 | return 1; |
257 | 0 | } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) { |
258 | 0 | return (c->ce == scope); |
259 | 0 | } else { |
260 | 0 | ZEND_ASSERT(ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PROTECTED); |
261 | 0 | return zend_check_protected(c->ce, scope); |
262 | 0 | } |
263 | 0 | } |
264 | | /* }}} */ |
265 | | |
266 | | static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len) |
267 | 0 | { |
268 | 0 | zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len); |
269 | 0 | if (c) { |
270 | 0 | return c; |
271 | 0 | } |
272 | | |
273 | 0 | c = zend_get_halt_offset_constant(name, name_len); |
274 | 0 | if (c) { |
275 | 0 | return c; |
276 | 0 | } |
277 | | |
278 | 0 | return zend_get_special_const(name, name_len); |
279 | 0 | } |
280 | | |
281 | | ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len) |
282 | 0 | { |
283 | 0 | zend_constant *c = zend_get_constant_str_impl(name, name_len); |
284 | 0 | if (c) { |
285 | 0 | return &c->value; |
286 | 0 | } |
287 | 0 | return NULL; |
288 | 0 | } |
289 | | |
290 | | ZEND_API zend_constant *zend_get_constant_ptr(zend_string *name) |
291 | 0 | { |
292 | 0 | zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name); |
293 | 0 | if (c) { |
294 | 0 | return c; |
295 | 0 | } |
296 | | |
297 | 0 | c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name)); |
298 | 0 | if (c) { |
299 | 0 | return c; |
300 | 0 | } |
301 | | |
302 | 0 | return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name)); |
303 | 0 | } |
304 | | |
305 | | ZEND_API zval *zend_get_constant(zend_string *name) |
306 | 0 | { |
307 | 0 | zend_constant *c = zend_get_constant_ptr(name); |
308 | 0 | if (c) { |
309 | 0 | return &c->value; |
310 | 0 | } |
311 | 0 | return NULL; |
312 | 0 | } |
313 | | |
314 | | ZEND_API zval *zend_get_class_constant_ex(zend_string *class_name, zend_string *constant_name, const zend_class_entry *scope, uint32_t flags) |
315 | 0 | { |
316 | 0 | const zend_class_entry *ce = NULL; |
317 | 0 | zend_class_constant *c = NULL; |
318 | 0 | zval *ret_constant = NULL; |
319 | |
|
320 | 0 | if (ZSTR_HAS_CE_CACHE(class_name)) { |
321 | 0 | ce = ZSTR_GET_CE_CACHE(class_name); |
322 | 0 | if (!ce) { |
323 | 0 | ce = zend_fetch_class(class_name, flags); |
324 | 0 | } |
325 | 0 | } else if (zend_string_equals_ci(class_name, ZSTR_KNOWN(ZEND_STR_SELF))) { |
326 | 0 | if (UNEXPECTED(!scope)) { |
327 | 0 | zend_throw_error(NULL, "Cannot access \"self\" when no class scope is active"); |
328 | 0 | goto failure; |
329 | 0 | } |
330 | 0 | ce = scope; |
331 | 0 | } else if (zend_string_equals_ci(class_name, ZSTR_KNOWN(ZEND_STR_PARENT))) { |
332 | 0 | if (UNEXPECTED(!scope)) { |
333 | 0 | zend_throw_error(NULL, "Cannot access \"parent\" when no class scope is active"); |
334 | 0 | goto failure; |
335 | 0 | } else if (UNEXPECTED(!scope->parent)) { |
336 | 0 | zend_throw_error(NULL, "Cannot access \"parent\" when current class scope has no parent"); |
337 | 0 | goto failure; |
338 | 0 | } else { |
339 | 0 | ce = scope->parent; |
340 | 0 | } |
341 | 0 | } else if (zend_string_equals_ci(class_name, ZSTR_KNOWN(ZEND_STR_STATIC))) { |
342 | 0 | ce = zend_get_called_scope(EG(current_execute_data)); |
343 | 0 | if (UNEXPECTED(!ce)) { |
344 | 0 | zend_throw_error(NULL, "Cannot access \"static\" when no class scope is active"); |
345 | 0 | goto failure; |
346 | 0 | } |
347 | 0 | } else { |
348 | 0 | ce = zend_fetch_class(class_name, flags); |
349 | 0 | } |
350 | 0 | if (ce) { |
351 | 0 | c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), constant_name); |
352 | 0 | if (c == NULL) { |
353 | 0 | if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) { |
354 | 0 | zend_throw_error(NULL, "Undefined constant %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(constant_name)); |
355 | 0 | goto failure; |
356 | 0 | } |
357 | 0 | ret_constant = NULL; |
358 | 0 | } else { |
359 | 0 | if (!zend_verify_const_access(c, scope)) { |
360 | 0 | if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) { |
361 | 0 | zend_throw_error(NULL, "Cannot access %s constant %s::%s", zend_visibility_string(ZEND_CLASS_CONST_FLAGS(c)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name)); |
362 | 0 | } |
363 | 0 | goto failure; |
364 | 0 | } |
365 | | |
366 | 0 | if (UNEXPECTED(ce->ce_flags & ZEND_ACC_TRAIT)) { |
367 | | /** Prevent accessing trait constants directly on cases like \defined() or \constant(), etc. */ |
368 | 0 | if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) { |
369 | 0 | zend_throw_error(NULL, "Cannot access trait constant %s::%s directly", ZSTR_VAL(class_name), ZSTR_VAL(constant_name)); |
370 | 0 | } |
371 | 0 | goto failure; |
372 | 0 | } |
373 | | |
374 | 0 | if (UNEXPECTED(ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED)) { |
375 | 0 | if ((flags & ZEND_FETCH_CLASS_SILENT) == 0 && !CONST_IS_RECURSIVE(c)) { |
376 | 0 | if (c->ce->type == ZEND_USER_CLASS) { |
377 | | /* Recursion protection only applied to user constants, GH-18463 */ |
378 | 0 | CONST_PROTECT_RECURSION(c); |
379 | 0 | } |
380 | 0 | zend_deprecated_class_constant(c, constant_name); |
381 | 0 | if (c->ce->type == ZEND_USER_CLASS) { |
382 | 0 | CONST_UNPROTECT_RECURSION(c); |
383 | 0 | } |
384 | 0 | if (EG(exception)) { |
385 | 0 | goto failure; |
386 | 0 | } |
387 | 0 | } |
388 | 0 | } |
389 | 0 | ret_constant = &c->value; |
390 | 0 | } |
391 | 0 | } |
392 | | |
393 | 0 | if (ret_constant && Z_TYPE_P(ret_constant) == IS_CONSTANT_AST) { |
394 | 0 | zend_result ret; |
395 | |
|
396 | 0 | if (IS_CONSTANT_VISITED(ret_constant)) { |
397 | 0 | zend_throw_error(NULL, "Cannot declare self-referencing constant %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(constant_name)); |
398 | 0 | ret_constant = NULL; |
399 | 0 | goto failure; |
400 | 0 | } |
401 | | |
402 | 0 | MARK_CONSTANT_VISITED(ret_constant); |
403 | 0 | ret = zend_update_class_constant(c, constant_name, c->ce); |
404 | 0 | RESET_CONSTANT_VISITED(ret_constant); |
405 | |
|
406 | 0 | if (UNEXPECTED(ret != SUCCESS)) { |
407 | 0 | ret_constant = NULL; |
408 | 0 | goto failure; |
409 | 0 | } |
410 | 0 | } |
411 | 0 | failure: |
412 | 0 | return ret_constant; |
413 | 0 | } |
414 | | |
415 | | ZEND_API zval *zend_get_constant_ex(zend_string *cname, const zend_class_entry *scope, uint32_t flags) |
416 | 0 | { |
417 | 0 | zend_constant *c; |
418 | 0 | const char *colon; |
419 | 0 | const char *name = ZSTR_VAL(cname); |
420 | 0 | size_t name_len = ZSTR_LEN(cname); |
421 | | |
422 | | /* Skip leading \\ */ |
423 | 0 | if (name[0] == '\\') { |
424 | 0 | name += 1; |
425 | 0 | name_len -= 1; |
426 | 0 | cname = NULL; |
427 | 0 | } |
428 | |
|
429 | 0 | if ((colon = zend_memrchr(name, ':', name_len)) && |
430 | 0 | colon > name && (*(colon - 1) == ':')) { |
431 | 0 | int class_name_len = colon - name - 1; |
432 | 0 | size_t const_name_len = name_len - class_name_len - 2; |
433 | 0 | zend_string *constant_name = zend_string_init(colon + 1, const_name_len, 0); |
434 | 0 | zend_string *class_name = zend_string_init_interned(name, class_name_len, 0); |
435 | 0 | zval *ret_constant = zend_get_class_constant_ex(class_name, constant_name, scope, flags); |
436 | |
|
437 | 0 | zend_string_release_ex(class_name, 0); |
438 | 0 | zend_string_efree(constant_name); |
439 | 0 | return ret_constant; |
440 | 0 | } |
441 | | |
442 | | /* non-class constant */ |
443 | 0 | if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) { |
444 | | /* compound constant name */ |
445 | 0 | int prefix_len = colon - name; |
446 | 0 | size_t const_name_len = name_len - prefix_len - 1; |
447 | 0 | const char *constant_name = colon + 1; |
448 | 0 | char *lcname; |
449 | 0 | size_t lcname_len; |
450 | 0 | ALLOCA_FLAG(use_heap) |
451 | | |
452 | | /* Lowercase the namespace portion */ |
453 | 0 | lcname_len = prefix_len + 1 + const_name_len; |
454 | 0 | lcname = do_alloca(lcname_len + 1, use_heap); |
455 | 0 | zend_str_tolower_copy(lcname, name, prefix_len); |
456 | |
|
457 | 0 | lcname[prefix_len] = '\\'; |
458 | 0 | memcpy(lcname + prefix_len + 1, constant_name, const_name_len + 1); |
459 | |
|
460 | 0 | c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len); |
461 | 0 | free_alloca(lcname, use_heap); |
462 | |
|
463 | 0 | if (!c) { |
464 | 0 | if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) { |
465 | | /* name requires runtime resolution, need to check non-namespaced name */ |
466 | 0 | c = zend_get_constant_str_impl(constant_name, const_name_len); |
467 | 0 | } |
468 | 0 | } |
469 | 0 | } else { |
470 | 0 | if (cname) { |
471 | 0 | c = zend_get_constant_ptr(cname); |
472 | 0 | } else { |
473 | 0 | c = zend_get_constant_str_impl(name, name_len); |
474 | 0 | } |
475 | 0 | } |
476 | |
|
477 | 0 | if (!c) { |
478 | 0 | if (!(flags & ZEND_FETCH_CLASS_SILENT)) { |
479 | 0 | zend_throw_error(NULL, "Undefined constant \"%s\"", name); |
480 | 0 | } |
481 | 0 | return NULL; |
482 | 0 | } |
483 | | |
484 | 0 | if (!(flags & ZEND_FETCH_CLASS_SILENT) && (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)) { |
485 | 0 | if (!CONST_IS_RECURSIVE(c)) { |
486 | 0 | CONST_PROTECT_RECURSION(c); |
487 | 0 | zend_deprecated_constant(c, c->name); |
488 | 0 | CONST_UNPROTECT_RECURSION(c); |
489 | 0 | if (UNEXPECTED(EG(exception))) { |
490 | 0 | return NULL; |
491 | 0 | } |
492 | 0 | } |
493 | 0 | } |
494 | 0 | return &c->value; |
495 | 0 | } |
496 | | |
497 | | static void* zend_hash_add_constant(HashTable *ht, zend_string *key, const zend_constant *c) |
498 | 1.10k | { |
499 | 1.10k | void *ret; |
500 | 1.10k | zend_constant *copy = pemalloc(sizeof(zend_constant), ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT); |
501 | | |
502 | 1.10k | memcpy(copy, c, sizeof(zend_constant)); |
503 | 1.10k | ret = zend_hash_add_ptr(ht, key, copy); |
504 | 1.10k | if (!ret) { |
505 | 0 | pefree(copy, ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT); |
506 | 0 | } |
507 | 1.10k | return ret; |
508 | 1.10k | } |
509 | | |
510 | | ZEND_API zend_constant *zend_register_constant(zend_constant *c) |
511 | 1.10k | { |
512 | 1.10k | zend_string *lowercase_name = NULL; |
513 | 1.10k | zend_string *name; |
514 | 1.10k | zend_constant *ret = NULL; |
515 | 1.10k | bool persistent = (ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) != 0; |
516 | | |
517 | | #if 0 |
518 | | printf("Registering constant for module %d\n", c->module_number); |
519 | | #endif |
520 | | |
521 | 1.10k | const char *slash = strrchr(ZSTR_VAL(c->name), '\\'); |
522 | 1.10k | if (slash) { |
523 | 0 | lowercase_name = zend_string_init(ZSTR_VAL(c->name), ZSTR_LEN(c->name), persistent); |
524 | 0 | zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(c->name)); |
525 | 0 | lowercase_name = zend_new_interned_string(lowercase_name); |
526 | 0 | name = lowercase_name; |
527 | 1.10k | } else { |
528 | 1.10k | name = c->name; |
529 | 1.10k | } |
530 | | |
531 | 1.10k | c->filename = NULL; |
532 | 1.10k | if (ZEND_CONSTANT_MODULE_NUMBER(c) == PHP_USER_CONSTANT) { |
533 | 0 | zend_string *filename = zend_get_executed_filename_ex(); |
534 | 0 | if (filename) { |
535 | 0 | c->filename = zend_string_copy(filename); |
536 | 0 | } |
537 | 0 | } |
538 | | |
539 | 1.10k | c->attributes = NULL; |
540 | | |
541 | | /* Check if the user is trying to define any special constant */ |
542 | 1.10k | if (zend_string_equals_literal(name, "__COMPILER_HALT_OFFSET__") |
543 | 1.10k | || (!persistent && zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name))) |
544 | 1.10k | || (ret = zend_hash_add_constant(EG(zend_constants), name, c)) == NULL |
545 | 1.10k | ) { |
546 | 0 | zend_error(E_WARNING, "Constant %s already defined, this will be an error in PHP 9", ZSTR_VAL(name)); |
547 | 0 | zend_string_release(c->name); |
548 | 0 | if (c->filename) { |
549 | 0 | zend_string_release(c->filename); |
550 | 0 | c->filename = NULL; |
551 | 0 | } |
552 | 0 | if (!persistent) { |
553 | 0 | zval_ptr_dtor_nogc(&c->value); |
554 | 0 | } |
555 | 0 | } |
556 | 1.10k | if (lowercase_name) { |
557 | 0 | zend_string_release(lowercase_name); |
558 | 0 | } |
559 | 1.10k | return ret; |
560 | 1.10k | } |
561 | | |
562 | 0 | void zend_constant_add_attributes(zend_constant *c, HashTable *attributes) { |
563 | 0 | GC_TRY_ADDREF(attributes); |
564 | 0 | c->attributes = attributes; |
565 | |
|
566 | 0 | zend_attribute *deprecated_attribute = zend_get_attribute_str( |
567 | 0 | c->attributes, |
568 | 0 | "deprecated", |
569 | 0 | strlen("deprecated") |
570 | 0 | ); |
571 | |
|
572 | 0 | if (deprecated_attribute) { |
573 | 0 | ZEND_CONSTANT_SET_FLAGS( |
574 | 0 | c, |
575 | 0 | ZEND_CONSTANT_FLAGS(c) | CONST_DEPRECATED, |
576 | 0 | ZEND_CONSTANT_MODULE_NUMBER(c) |
577 | 0 | ); |
578 | 0 | } |
579 | 0 | } |