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