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_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
29 /* Protection from recursive self-referencing class constants */
30 #define IS_CONSTANT_VISITED_MARK 0x80
31
32 #define IS_CONSTANT_VISITED(zv) (Z_CONSTANT_FLAGS_P(zv) & IS_CONSTANT_VISITED_MARK)
33 #define MARK_CONSTANT_VISITED(zv) Z_CONSTANT_FLAGS_P(zv) |= IS_CONSTANT_VISITED_MARK
34 #define RESET_CONSTANT_VISITED(zv) Z_CONSTANT_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK
35
36 /* Use for special null/true/false constants. */
37 static zend_constant *null_const, *true_const, *false_const;
38
free_zend_constant(zval * zv)39 void free_zend_constant(zval *zv)
40 {
41 zend_constant *c = Z_PTR_P(zv);
42
43 if (!(ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)) {
44 zval_ptr_dtor_nogc(&c->value);
45 if (c->name) {
46 zend_string_release_ex(c->name, 0);
47 }
48 efree(c);
49 } else {
50 zval_internal_ptr_dtor(&c->value);
51 if (c->name) {
52 zend_string_release_ex(c->name, 1);
53 }
54 free(c);
55 }
56 }
57
58
59 #ifdef ZTS
copy_zend_constant(zval * zv)60 static void copy_zend_constant(zval *zv)
61 {
62 zend_constant *c = Z_PTR_P(zv);
63
64 ZEND_ASSERT(ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT);
65 Z_PTR_P(zv) = pemalloc(sizeof(zend_constant), 1);
66 memcpy(Z_PTR_P(zv), c, sizeof(zend_constant));
67
68 c = Z_PTR_P(zv);
69 c->name = zend_string_copy(c->name);
70 if (Z_TYPE(c->value) == IS_STRING) {
71 Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1);
72 }
73 }
74
75
zend_copy_constants(HashTable * target,HashTable * source)76 void zend_copy_constants(HashTable *target, HashTable *source)
77 {
78 zend_hash_copy(target, source, copy_zend_constant);
79 }
80 #endif
81
82
clean_module_constant(zval * el,void * arg)83 static int clean_module_constant(zval *el, void *arg)
84 {
85 zend_constant *c = (zend_constant *)Z_PTR_P(el);
86 int module_number = *(int *)arg;
87
88 if (ZEND_CONSTANT_MODULE_NUMBER(c) == module_number) {
89 return ZEND_HASH_APPLY_REMOVE;
90 } else {
91 return ZEND_HASH_APPLY_KEEP;
92 }
93 }
94
95
clean_module_constants(int module_number)96 void clean_module_constants(int module_number)
97 {
98 zend_hash_apply_with_argument(EG(zend_constants), clean_module_constant, (void *) &module_number);
99 }
100
zend_startup_constants(void)101 void zend_startup_constants(void)
102 {
103 EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable));
104 zend_hash_init(EG(zend_constants), 128, NULL, ZEND_CONSTANT_DTOR, 1);
105 }
106
107
108
zend_register_standard_constants(void)109 void zend_register_standard_constants(void)
110 {
111 REGISTER_MAIN_LONG_CONSTANT("E_ERROR", E_ERROR, CONST_PERSISTENT | CONST_CS);
112 REGISTER_MAIN_LONG_CONSTANT("E_RECOVERABLE_ERROR", E_RECOVERABLE_ERROR, CONST_PERSISTENT | CONST_CS);
113 REGISTER_MAIN_LONG_CONSTANT("E_WARNING", E_WARNING, CONST_PERSISTENT | CONST_CS);
114 REGISTER_MAIN_LONG_CONSTANT("E_PARSE", E_PARSE, CONST_PERSISTENT | CONST_CS);
115 REGISTER_MAIN_LONG_CONSTANT("E_NOTICE", E_NOTICE, CONST_PERSISTENT | CONST_CS);
116 REGISTER_MAIN_LONG_CONSTANT("E_STRICT", E_STRICT, CONST_PERSISTENT | CONST_CS);
117 REGISTER_MAIN_LONG_CONSTANT("E_DEPRECATED", E_DEPRECATED, CONST_PERSISTENT | CONST_CS);
118 REGISTER_MAIN_LONG_CONSTANT("E_CORE_ERROR", E_CORE_ERROR, CONST_PERSISTENT | CONST_CS);
119 REGISTER_MAIN_LONG_CONSTANT("E_CORE_WARNING", E_CORE_WARNING, CONST_PERSISTENT | CONST_CS);
120 REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_ERROR", E_COMPILE_ERROR, CONST_PERSISTENT | CONST_CS);
121 REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_WARNING", E_COMPILE_WARNING, CONST_PERSISTENT | CONST_CS);
122 REGISTER_MAIN_LONG_CONSTANT("E_USER_ERROR", E_USER_ERROR, CONST_PERSISTENT | CONST_CS);
123 REGISTER_MAIN_LONG_CONSTANT("E_USER_WARNING", E_USER_WARNING, CONST_PERSISTENT | CONST_CS);
124 REGISTER_MAIN_LONG_CONSTANT("E_USER_NOTICE", E_USER_NOTICE, CONST_PERSISTENT | CONST_CS);
125 REGISTER_MAIN_LONG_CONSTANT("E_USER_DEPRECATED", E_USER_DEPRECATED, CONST_PERSISTENT | CONST_CS);
126
127 REGISTER_MAIN_LONG_CONSTANT("E_ALL", E_ALL, CONST_PERSISTENT | CONST_CS);
128
129 REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_PROVIDE_OBJECT", DEBUG_BACKTRACE_PROVIDE_OBJECT, CONST_PERSISTENT | CONST_CS);
130 REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_IGNORE_ARGS", DEBUG_BACKTRACE_IGNORE_ARGS, CONST_PERSISTENT | CONST_CS);
131 REGISTER_MAIN_BOOL_CONSTANT("ZEND_THREAD_SAFE", ZTS_V, CONST_PERSISTENT | CONST_CS);
132 REGISTER_MAIN_BOOL_CONSTANT("ZEND_DEBUG_BUILD", ZEND_DEBUG, CONST_PERSISTENT | CONST_CS);
133
134 /* Special constants true/false/null. */
135 REGISTER_MAIN_BOOL_CONSTANT("TRUE", 1, CONST_PERSISTENT);
136 REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
137 REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);
138
139 true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
140 false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
141 null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
142 }
143
144
zend_shutdown_constants(void)145 void zend_shutdown_constants(void)
146 {
147 zend_hash_destroy(EG(zend_constants));
148 free(EG(zend_constants));
149 }
150
zend_register_null_constant(const char * name,size_t name_len,int flags,int module_number)151 ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
152 {
153 zend_constant c;
154
155 ZVAL_NULL(&c.value);
156 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
157 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
158 zend_register_constant(&c);
159 }
160
zend_register_bool_constant(const char * name,size_t name_len,bool bval,int flags,int module_number)161 ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
162 {
163 zend_constant c;
164
165 ZVAL_BOOL(&c.value, bval);
166 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
167 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
168 zend_register_constant(&c);
169 }
170
zend_register_long_constant(const char * name,size_t name_len,zend_long lval,int flags,int module_number)171 ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
172 {
173 zend_constant c;
174
175 ZVAL_LONG(&c.value, lval);
176 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
177 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
178 zend_register_constant(&c);
179 }
180
181
zend_register_double_constant(const char * name,size_t name_len,double dval,int flags,int module_number)182 ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
183 {
184 zend_constant c;
185
186 ZVAL_DOUBLE(&c.value, dval);
187 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
188 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
189 zend_register_constant(&c);
190 }
191
192
zend_register_stringl_constant(const char * name,size_t name_len,const char * strval,size_t strlen,int flags,int module_number)193 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)
194 {
195 zend_constant c;
196
197 ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT));
198 ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
199 c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
200 zend_register_constant(&c);
201 }
202
203
zend_register_string_constant(const char * name,size_t name_len,const char * strval,int flags,int module_number)204 ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number)
205 {
206 zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
207 }
208
zend_get_halt_offset_constant(const char * name,size_t name_len)209 static zend_constant *zend_get_halt_offset_constant(const char *name, size_t name_len)
210 {
211 zend_constant *c;
212 static const char haltoff[] = "__COMPILER_HALT_OFFSET__";
213
214 if (!EG(current_execute_data)) {
215 return NULL;
216 } else if (name_len == sizeof("__COMPILER_HALT_OFFSET__")-1 &&
217 !memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
218 const char *cfilename;
219 zend_string *haltname;
220 size_t clen;
221
222 cfilename = zend_get_executed_filename();
223 clen = strlen(cfilename);
224 /* check for __COMPILER_HALT_OFFSET__ */
225 haltname = zend_mangle_property_name(haltoff,
226 sizeof("__COMPILER_HALT_OFFSET__") - 1, cfilename, clen, 0);
227 c = zend_hash_find_ptr(EG(zend_constants), haltname);
228 zend_string_efree(haltname);
229 return c;
230 } else {
231 return NULL;
232 }
233 }
234
_zend_get_special_const(const char * name,size_t len)235 ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
236 {
237 if (len == 4) {
238 if ((name[0] == 'n' || name[0] == 'N') &&
239 (name[1] == 'u' || name[1] == 'U') &&
240 (name[2] == 'l' || name[2] == 'L') &&
241 (name[3] == 'l' || name[3] == 'L')
242 ) {
243 return null_const;
244 }
245 if ((name[0] == 't' || name[0] == 'T') &&
246 (name[1] == 'r' || name[1] == 'R') &&
247 (name[2] == 'u' || name[2] == 'U') &&
248 (name[3] == 'e' || name[3] == 'E')
249 ) {
250 return true_const;
251 }
252 } else {
253 if ((name[0] == 'f' || name[0] == 'F') &&
254 (name[1] == 'a' || name[1] == 'A') &&
255 (name[2] == 'l' || name[2] == 'L') &&
256 (name[3] == 's' || name[3] == 'S') &&
257 (name[4] == 'e' || name[4] == 'E')
258 ) {
259 return false_const;
260 }
261 }
262 return NULL;
263 }
264 /* }}} */
265
zend_verify_const_access(zend_class_constant * c,zend_class_entry * scope)266 ZEND_API bool zend_verify_const_access(zend_class_constant *c, zend_class_entry *scope) /* {{{ */
267 {
268 if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
269 return 1;
270 } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
271 return (c->ce == scope);
272 } else {
273 ZEND_ASSERT(ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PROTECTED);
274 return zend_check_protected(c->ce, scope);
275 }
276 }
277 /* }}} */
278
zend_get_constant_str_impl(const char * name,size_t name_len)279 static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
280 {
281 zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
282 if (c) {
283 return c;
284 }
285
286 c = zend_get_halt_offset_constant(name, name_len);
287 if (c) {
288 return c;
289 }
290
291 return zend_get_special_const(name, name_len);
292 }
293
zend_get_constant_str(const char * name,size_t name_len)294 ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
295 {
296 zend_constant *c = zend_get_constant_str_impl(name, name_len);
297 if (c) {
298 return &c->value;
299 }
300 return NULL;
301 }
302
zend_get_constant_impl(zend_string * name)303 static zend_constant *zend_get_constant_impl(zend_string *name)
304 {
305 zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
306 if (c) {
307 return c;
308 }
309
310 c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
311 if (c) {
312 return c;
313 }
314
315 return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
316 }
317
zend_get_constant(zend_string * name)318 ZEND_API zval *zend_get_constant(zend_string *name)
319 {
320 zend_constant *c = zend_get_constant_impl(name);
321 if (c) {
322 return &c->value;
323 }
324 return NULL;
325 }
326
zend_get_class_constant_ex(zend_string * class_name,zend_string * constant_name,zend_class_entry * scope,uint32_t flags)327 ZEND_API zval *zend_get_class_constant_ex(zend_string *class_name, zend_string *constant_name, zend_class_entry *scope, uint32_t flags)
328 {
329 zend_class_entry *ce = NULL;
330 zend_class_constant *c = NULL;
331 zval *ret_constant = NULL;
332
333 if (ZSTR_HAS_CE_CACHE(class_name)) {
334 ce = ZSTR_GET_CE_CACHE(class_name);
335 if (!ce) {
336 ce = zend_fetch_class(class_name, flags);
337 }
338 } else if (zend_string_equals_literal_ci(class_name, "self")) {
339 if (UNEXPECTED(!scope)) {
340 zend_throw_error(NULL, "Cannot access \"self\" when no class scope is active");
341 goto failure;
342 }
343 ce = scope;
344 } else if (zend_string_equals_literal_ci(class_name, "parent")) {
345 if (UNEXPECTED(!scope)) {
346 zend_throw_error(NULL, "Cannot access \"parent\" when no class scope is active");
347 goto failure;
348 } else if (UNEXPECTED(!scope->parent)) {
349 zend_throw_error(NULL, "Cannot access \"parent\" when current class scope has no parent");
350 goto failure;
351 } else {
352 ce = scope->parent;
353 }
354 } else if (zend_string_equals_literal_ci(class_name, "static")) {
355 ce = zend_get_called_scope(EG(current_execute_data));
356 if (UNEXPECTED(!ce)) {
357 zend_throw_error(NULL, "Cannot access \"static\" when no class scope is active");
358 goto failure;
359 }
360 } else {
361 ce = zend_fetch_class(class_name, flags);
362 }
363 if (ce) {
364 c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), constant_name);
365 if (c == NULL) {
366 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
367 zend_throw_error(NULL, "Undefined constant %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
368 goto failure;
369 }
370 ret_constant = NULL;
371 } else {
372 if (!zend_verify_const_access(c, scope)) {
373 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
374 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));
375 }
376 goto failure;
377 }
378 ret_constant = &c->value;
379 }
380 }
381
382 if (ret_constant && Z_TYPE_P(ret_constant) == IS_CONSTANT_AST) {
383 zend_result ret;
384
385 if (IS_CONSTANT_VISITED(ret_constant)) {
386 zend_throw_error(NULL, "Cannot declare self-referencing constant %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
387 ret_constant = NULL;
388 goto failure;
389 }
390
391 MARK_CONSTANT_VISITED(ret_constant);
392 ret = zval_update_constant_ex(ret_constant, c->ce);
393 RESET_CONSTANT_VISITED(ret_constant);
394
395 if (UNEXPECTED(ret != SUCCESS)) {
396 ret_constant = NULL;
397 goto failure;
398 }
399 }
400 failure:
401 return ret_constant;
402 }
403
zend_get_constant_ex(zend_string * cname,zend_class_entry * scope,uint32_t flags)404 ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
405 {
406 zend_constant *c;
407 const char *colon;
408 const char *name = ZSTR_VAL(cname);
409 size_t name_len = ZSTR_LEN(cname);
410
411 /* Skip leading \\ */
412 if (name[0] == '\\') {
413 name += 1;
414 name_len -= 1;
415 cname = NULL;
416 }
417
418 if ((colon = zend_memrchr(name, ':', name_len)) &&
419 colon > name && (*(colon - 1) == ':')) {
420 int class_name_len = colon - name - 1;
421 size_t const_name_len = name_len - class_name_len - 2;
422 zend_string *constant_name = zend_string_init(colon + 1, const_name_len, 0);
423 zend_string *class_name = zend_string_init_interned(name, class_name_len, 0);
424 zval *ret_constant = zend_get_class_constant_ex(class_name, constant_name, scope, flags);
425
426 zend_string_release_ex(class_name, 0);
427 zend_string_efree(constant_name);
428 return ret_constant;
429 /*
430 zend_class_entry *ce = NULL;
431 zend_class_constant *c = NULL;
432 zval *ret_constant = NULL;
433
434 if (zend_string_equals_literal_ci(class_name, "self")) {
435 if (UNEXPECTED(!scope)) {
436 zend_throw_error(NULL, "Cannot access \"self\" when no class scope is active");
437 goto failure;
438 }
439 ce = scope;
440 } else if (zend_string_equals_literal_ci(class_name, "parent")) {
441 if (UNEXPECTED(!scope)) {
442 zend_throw_error(NULL, "Cannot access \"parent\" when no class scope is active");
443 goto failure;
444 } else if (UNEXPECTED(!scope->parent)) {
445 zend_throw_error(NULL, "Cannot access \"parent\" when current class scope has no parent");
446 goto failure;
447 } else {
448 ce = scope->parent;
449 }
450 } else if (zend_string_equals_literal_ci(class_name, "static")) {
451 ce = zend_get_called_scope(EG(current_execute_data));
452 if (UNEXPECTED(!ce)) {
453 zend_throw_error(NULL, "Cannot access \"static\" when no class scope is active");
454 goto failure;
455 }
456 } else {
457 ce = zend_fetch_class(class_name, flags);
458 }
459 if (ce) {
460 c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), constant_name);
461 if (c == NULL) {
462 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
463 zend_throw_error(NULL, "Undefined constant %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
464 goto failure;
465 }
466 ret_constant = NULL;
467 } else {
468 if (!zend_verify_const_access(c, scope)) {
469 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
470 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));
471 }
472 goto failure;
473 }
474 ret_constant = &c->value;
475 }
476 }
477
478 if (ret_constant && Z_TYPE_P(ret_constant) == IS_CONSTANT_AST) {
479 zend_result ret;
480
481 if (IS_CONSTANT_VISITED(ret_constant)) {
482 zend_throw_error(NULL, "Cannot declare self-referencing constant %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
483 ret_constant = NULL;
484 goto failure;
485 }
486
487 MARK_CONSTANT_VISITED(ret_constant);
488 ret = zval_update_constant_ex(ret_constant, c->ce);
489 RESET_CONSTANT_VISITED(ret_constant);
490
491 if (UNEXPECTED(ret != SUCCESS)) {
492 ret_constant = NULL;
493 goto failure;
494 }
495 }
496 failure:
497 zend_string_release_ex(class_name, 0);
498 zend_string_efree(constant_name);
499 return ret_constant;
500 */
501 }
502
503 /* non-class constant */
504 if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
505 /* compound constant name */
506 int prefix_len = colon - name;
507 size_t const_name_len = name_len - prefix_len - 1;
508 const char *constant_name = colon + 1;
509 char *lcname;
510 size_t lcname_len;
511 ALLOCA_FLAG(use_heap)
512
513 /* Lowercase the namespace portion */
514 lcname_len = prefix_len + 1 + const_name_len;
515 lcname = do_alloca(lcname_len + 1, use_heap);
516 zend_str_tolower_copy(lcname, name, prefix_len);
517
518 lcname[prefix_len] = '\\';
519 memcpy(lcname + prefix_len + 1, constant_name, const_name_len + 1);
520
521 c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
522 free_alloca(lcname, use_heap);
523
524 if (!c) {
525 if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
526 /* name requires runtime resolution, need to check non-namespaced name */
527 c = zend_get_constant_str_impl(constant_name, const_name_len);
528 }
529 }
530 } else {
531 if (cname) {
532 c = zend_get_constant_impl(cname);
533 } else {
534 c = zend_get_constant_str_impl(name, name_len);
535 }
536 }
537
538 if (!c) {
539 if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
540 zend_throw_error(NULL, "Undefined constant \"%s\"", name);
541 }
542 return NULL;
543 }
544
545 if (!(flags & ZEND_FETCH_CLASS_SILENT) && (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)) {
546 zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
547 }
548 return &c->value;
549 }
550
zend_hash_add_constant(HashTable * ht,zend_string * key,zend_constant * c)551 static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
552 {
553 void *ret;
554 zend_constant *copy = pemalloc(sizeof(zend_constant), ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT);
555
556 memcpy(copy, c, sizeof(zend_constant));
557 ret = zend_hash_add_ptr(ht, key, copy);
558 if (!ret) {
559 pefree(copy, ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT);
560 }
561 return ret;
562 }
563
zend_register_constant(zend_constant * c)564 ZEND_API zend_result zend_register_constant(zend_constant *c)
565 {
566 zend_string *lowercase_name = NULL;
567 zend_string *name;
568 zend_result ret = SUCCESS;
569 bool persistent = (ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) != 0;
570
571 #if 0
572 printf("Registering constant for module %d\n", c->module_number);
573 #endif
574
575 const char *slash = strrchr(ZSTR_VAL(c->name), '\\');
576 if (slash) {
577 lowercase_name = zend_string_init(ZSTR_VAL(c->name), ZSTR_LEN(c->name), persistent);
578 zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(c->name));
579 lowercase_name = zend_new_interned_string(lowercase_name);
580 name = lowercase_name;
581 } else {
582 name = c->name;
583 }
584
585 /* Check if the user is trying to define any special constant */
586 if (zend_string_equals_literal(name, "__COMPILER_HALT_OFFSET__")
587 || (!persistent && zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name)))
588 || zend_hash_add_constant(EG(zend_constants), name, c) == NULL
589 ) {
590 zend_error(E_WARNING, "Constant %s already defined", ZSTR_VAL(name));
591 zend_string_release(c->name);
592 if (!persistent) {
593 zval_ptr_dtor_nogc(&c->value);
594 }
595 ret = FAILURE;
596 }
597 if (lowercase_name) {
598 zend_string_release(lowercase_name);
599 }
600 return ret;
601 }
602