zend_constants.c 19 KB

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