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