PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_constants.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 223 274 81.4 %
Date: 2015-07-26 Functions: 17 24 70.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 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@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "zend.h"
      23             : #include "zend_constants.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             : 
      30    51681865 : void free_zend_constant(zval *zv)
      31             : {
      32    51681865 :         zend_constant *c = Z_PTR_P(zv);
      33             : 
      34    51681865 :         if (!(c->flags & CONST_PERSISTENT)) {
      35       65785 :                 zval_dtor(&c->value);
      36             :         } else {
      37    51616080 :                 zval_internal_dtor(&c->value);
      38             :         }
      39    51681865 :         if (c->name) {
      40    51681865 :                 zend_string_release(c->name);
      41             :         }
      42    51681865 :         pefree(c, c->flags & CONST_PERSISTENT);
      43    51681865 : }
      44             : 
      45             : 
      46           0 : static void copy_zend_constant(zval *zv)
      47             : {
      48           0 :         zend_constant *c = Z_PTR_P(zv);
      49             : 
      50           0 :         Z_PTR_P(zv) = pemalloc(sizeof(zend_constant), c->flags & CONST_PERSISTENT);
      51           0 :         memcpy(Z_PTR_P(zv), c, sizeof(zend_constant));
      52             : 
      53           0 :         c = Z_PTR_P(zv);
      54           0 :         c->name = zend_string_copy(c->name);
      55           0 :         if (!(c->flags & CONST_PERSISTENT)) {
      56           0 :                 zval_copy_ctor(&c->value);
      57             :         } else {
      58           0 :                 if (Z_TYPE(c->value) == IS_STRING) {
      59           0 :                         Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1);
      60             :                 }
      61             :         }
      62           0 : }
      63             : 
      64             : 
      65           0 : void zend_copy_constants(HashTable *target, HashTable *source)
      66             : {
      67           0 :         zend_hash_copy(target, source, copy_zend_constant);
      68           0 : }
      69             : 
      70             : 
      71       86829 : static int clean_non_persistent_constant(zval *zv)
      72             : {
      73       86829 :         zend_constant *c = Z_PTR_P(zv);
      74       86829 :         return (c->flags & CONST_PERSISTENT) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
      75             : }
      76             : 
      77             : 
      78           0 : static int clean_non_persistent_constant_full(zval *zv)
      79             : {
      80           0 :         zend_constant *c = Z_PTR_P(zv);
      81           0 :         return (c->flags & CONST_PERSISTENT) ? 0 : 1;
      82             : }
      83             : 
      84             : 
      85           0 : static int clean_module_constant(zval *el, void *arg)
      86             : {
      87           0 :         zend_constant *c = (zend_constant *)Z_PTR_P(el);
      88           0 :         int module_number = *(int *)arg;
      89             : 
      90           0 :         if (c->module_number == module_number) {
      91           0 :                 return 1;
      92             :         } else {
      93           0 :                 return 0;
      94             :         }
      95             : }
      96             : 
      97             : 
      98           0 : void clean_module_constants(int module_number)
      99             : {
     100           0 :         zend_hash_apply_with_argument(EG(zend_constants), clean_module_constant, (void *) &module_number);
     101           0 : }
     102             : 
     103             : 
     104           0 : int zend_startup_constants(void)
     105             : {
     106           0 :         EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable));
     107             : 
     108           0 :         zend_hash_init(EG(zend_constants), 128, NULL, ZEND_CONSTANT_DTOR, 1);
     109           0 :         return SUCCESS;
     110             : }
     111             : 
     112             : 
     113             : 
     114       21049 : void zend_register_standard_constants(void)
     115             : {
     116       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_ERROR", E_ERROR, CONST_PERSISTENT | CONST_CS);
     117       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_RECOVERABLE_ERROR", E_RECOVERABLE_ERROR, CONST_PERSISTENT | CONST_CS);
     118       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_WARNING", E_WARNING, CONST_PERSISTENT | CONST_CS);
     119       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_PARSE", E_PARSE, CONST_PERSISTENT | CONST_CS);
     120       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_NOTICE", E_NOTICE, CONST_PERSISTENT | CONST_CS);
     121       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_STRICT", E_STRICT, CONST_PERSISTENT | CONST_CS);
     122       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_DEPRECATED", E_DEPRECATED, CONST_PERSISTENT | CONST_CS);
     123       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_CORE_ERROR", E_CORE_ERROR, CONST_PERSISTENT | CONST_CS);
     124       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_CORE_WARNING", E_CORE_WARNING, CONST_PERSISTENT | CONST_CS);
     125       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_ERROR", E_COMPILE_ERROR, CONST_PERSISTENT | CONST_CS);
     126       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_WARNING", E_COMPILE_WARNING, CONST_PERSISTENT | CONST_CS);
     127       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_USER_ERROR", E_USER_ERROR, CONST_PERSISTENT | CONST_CS);
     128       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_USER_WARNING", E_USER_WARNING, CONST_PERSISTENT | CONST_CS);
     129       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_USER_NOTICE", E_USER_NOTICE, CONST_PERSISTENT | CONST_CS);
     130       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_USER_DEPRECATED", E_USER_DEPRECATED, CONST_PERSISTENT | CONST_CS);
     131             : 
     132       21049 :         REGISTER_MAIN_LONG_CONSTANT("E_ALL", E_ALL, CONST_PERSISTENT | CONST_CS);
     133             : 
     134       21049 :         REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_PROVIDE_OBJECT", DEBUG_BACKTRACE_PROVIDE_OBJECT, CONST_PERSISTENT | CONST_CS);
     135       21049 :         REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_IGNORE_ARGS", DEBUG_BACKTRACE_IGNORE_ARGS, CONST_PERSISTENT | CONST_CS);
     136             :         /* true/false constants */
     137             :         {
     138       21049 :                 REGISTER_MAIN_BOOL_CONSTANT("TRUE", 1, CONST_PERSISTENT | CONST_CT_SUBST);
     139       21049 :                 REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT | CONST_CT_SUBST);
     140       21049 :                 REGISTER_MAIN_BOOL_CONSTANT("ZEND_THREAD_SAFE", ZTS_V, CONST_PERSISTENT | CONST_CS);
     141       21049 :                 REGISTER_MAIN_BOOL_CONSTANT("ZEND_DEBUG_BUILD", ZEND_DEBUG, CONST_PERSISTENT | CONST_CS);
     142             :         }
     143       21049 :         REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT | CONST_CT_SUBST);
     144       21049 : }
     145             : 
     146             : 
     147           0 : int zend_shutdown_constants(void)
     148             : {
     149           0 :         zend_hash_destroy(EG(zend_constants));
     150           0 :         free(EG(zend_constants));
     151           0 :         return SUCCESS;
     152             : }
     153             : 
     154             : 
     155       21044 : void clean_non_persistent_constants(void)
     156             : {
     157       21044 :         if (EG(full_tables_cleanup)) {
     158           0 :                 zend_hash_apply(EG(zend_constants), clean_non_persistent_constant_full);
     159             :         } else {
     160       21044 :                 zend_hash_reverse_apply(EG(zend_constants), clean_non_persistent_constant);
     161             :         }
     162       21044 : }
     163             : 
     164       21049 : ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
     165             : {
     166             :         zend_constant c;
     167             : 
     168       21049 :         ZVAL_NULL(&c.value);
     169       21049 :         c.flags = flags;
     170       42098 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     171       21049 :         c.module_number = module_number;
     172       21049 :         zend_register_constant(&c);
     173       21049 : }
     174             : 
     175       84196 : ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, zend_bool bval, int flags, int module_number)
     176             : {
     177             :         zend_constant c;
     178             : 
     179       84196 :         ZVAL_BOOL(&c.value, bval);
     180       84196 :         c.flags = flags;
     181      168392 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     182       84196 :         c.module_number = module_number;
     183       84196 :         zend_register_constant(&c);
     184       84196 : }
     185             : 
     186    49044445 : ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
     187             : {
     188             :         zend_constant c;
     189             : 
     190    49044445 :         ZVAL_LONG(&c.value, lval);
     191    49044445 :         c.flags = flags;
     192    98088890 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     193    49044445 :         c.module_number = module_number;
     194    49044445 :         zend_register_constant(&c);
     195    49044445 : }
     196             : 
     197             : 
     198      399931 : ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
     199             : {
     200             :         zend_constant c;
     201             : 
     202      399931 :         ZVAL_DOUBLE(&c.value, dval);
     203      399931 :         c.flags = flags;
     204      799862 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     205      399931 :         c.module_number = module_number;
     206      399931 :         zend_register_constant(&c);
     207      399931 : }
     208             : 
     209             : 
     210     1978792 : ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, char *strval, size_t strlen, int flags, int module_number)
     211             : {
     212             :         zend_constant c;
     213             : 
     214     3957584 :         ZVAL_NEW_STR(&c.value, zend_string_init(strval, strlen, flags & CONST_PERSISTENT));
     215     1978792 :         c.flags = flags;
     216     3957584 :         c.name = zend_string_init(name, name_len, flags & CONST_PERSISTENT);
     217     1978792 :         c.module_number = module_number;
     218     1978792 :         zend_register_constant(&c);
     219     1978792 : }
     220             : 
     221             : 
     222     1557626 : ZEND_API void zend_register_string_constant(const char *name, size_t name_len, char *strval, int flags, int module_number)
     223             : {
     224     1557626 :         zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
     225     1557626 : }
     226             : 
     227        1743 : static zend_constant *zend_get_special_constant(const char *name, size_t name_len)
     228             : {
     229             :         zend_constant *c;
     230             :         static char haltoff[] = "__COMPILER_HALT_OFFSET__";
     231             : 
     232        1743 :         if (!EG(current_execute_data)) {
     233         951 :                 return NULL;
     234        1292 :         } else if (name_len == sizeof("__COMPILER_HALT_OFFSET__")-1 &&
     235         500 :                   !memcmp(name, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
     236             :                 const char *cfilename;
     237             :                 zend_string *haltname;
     238             :                 size_t clen;
     239             : 
     240         500 :                 cfilename = zend_get_executed_filename();
     241         500 :                 clen = strlen(cfilename);
     242             :                 /* check for __COMPILER_HALT_OFFSET__ */
     243         500 :                 haltname = zend_mangle_property_name(haltoff,
     244             :                         sizeof("__COMPILER_HALT_OFFSET__") - 1, cfilename, clen, 0);
     245        1000 :                 c = zend_hash_find_ptr(EG(zend_constants), haltname);
     246             :                 zend_string_free(haltname);
     247         500 :                 return c;
     248             :         } else {
     249         292 :                 return NULL;
     250             :         }
     251             : }
     252             : 
     253             : 
     254        1158 : ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
     255             : {
     256             :         zend_constant *c;
     257             :         ALLOCA_FLAG(use_heap)
     258             : 
     259        2316 :         if ((c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len)) == NULL) {
     260         859 :                 char *lcname = do_alloca(name_len + 1, use_heap);
     261         859 :                 zend_str_tolower_copy(lcname, name, name_len);
     262        1718 :                 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, name_len)) != NULL) {
     263         170 :                         if (c->flags & CONST_CS) {
     264           0 :                                 c = NULL;
     265             :                         }
     266             :                 } else {
     267         689 :                         c = zend_get_special_constant(name, name_len);
     268             :                 }
     269         859 :                 free_alloca(lcname, use_heap);
     270             :         }
     271             : 
     272        1158 :         return c ? &c->value : NULL;
     273             : }
     274             : 
     275        1557 : ZEND_API zval *zend_get_constant(zend_string *name)
     276             : {
     277             :         zend_constant *c;
     278             :         ALLOCA_FLAG(use_heap)
     279             : 
     280        3114 :         if ((c = zend_hash_find_ptr(EG(zend_constants), name)) == NULL) {
     281        1054 :                 char *lcname = do_alloca(name->len + 1, use_heap);
     282        1054 :                 zend_str_tolower_copy(lcname, name->val, name->len);
     283        2108 :                 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, name->len)) != NULL) {
     284           0 :                         if (c->flags & CONST_CS) {
     285           0 :                                 c = NULL;
     286             :                         }
     287             :                 } else {
     288        1054 :                         c = zend_get_special_constant(name->val, name->len);
     289             :                 }
     290        1054 :                 free_alloca(lcname, use_heap);
     291             :         }
     292             : 
     293        1557 :         return c ? &c->value : NULL;
     294             : }
     295             : 
     296         406 : ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, zend_ulong flags)
     297             : {
     298             :         zend_constant *c;
     299             :         const char *colon;
     300         406 :         zend_class_entry *ce = NULL;
     301             :         zend_string *class_name;
     302         406 :         const char *name = cname->val;
     303         406 :         size_t name_len = cname->len;
     304             : 
     305             :         /* Skip leading \\ */
     306         406 :         if (name[0] == '\\') {
     307           8 :                 name += 1;
     308           8 :                 name_len -= 1;
     309           8 :                 cname = NULL;
     310             :         }
     311             : 
     312         545 :         if ((colon = zend_memrchr(name, ':', name_len)) &&
     313         139 :             colon > name && (*(colon - 1) == ':')) {
     314         139 :                 int class_name_len = colon - name - 1;
     315         139 :                 size_t const_name_len = name_len - class_name_len - 2;
     316         278 :                 zend_string *constant_name = zend_string_init(colon + 1, const_name_len, 0);
     317             :                 char *lcname;
     318         139 :                 zval *ret_constant = NULL;
     319             :                 ALLOCA_FLAG(use_heap)
     320             : 
     321         278 :                 class_name = zend_string_init(name, class_name_len, 0);
     322         139 :                 lcname = do_alloca(class_name_len + 1, use_heap);
     323         139 :                 zend_str_tolower_copy(lcname, name, class_name_len);
     324         139 :                 if (!scope) {
     325         130 :                         if (EG(current_execute_data)) {
     326         130 :                                 scope = EG(scope);
     327             :                         } else {
     328           0 :                                 scope = CG(active_class_entry);
     329             :                         }
     330             :                 }
     331             : 
     332         208 :                 if (class_name_len == sizeof("self")-1 &&
     333          36 :                     !memcmp(lcname, "self", sizeof("self")-1)) {
     334          33 :                         if (UNEXPECTED(!scope)) {
     335           0 :                                 zend_error(E_EXCEPTION | E_ERROR, "Cannot access self:: when no class scope is active");
     336           0 :                                 return NULL;
     337             :                         }
     338          33 :                         ce = scope;
     339         139 :                 } else if (class_name_len == sizeof("parent")-1 &&
     340          23 :                            !memcmp(lcname, "parent", sizeof("parent")-1)) {
     341          10 :                         if (UNEXPECTED(!scope)) {
     342           0 :                                 zend_error(E_EXCEPTION | E_ERROR, "Cannot access parent:: when no class scope is active");
     343           0 :                                 return NULL;
     344          10 :                         } else if (UNEXPECTED(!scope->parent)) {
     345           0 :                                 zend_error(E_EXCEPTION | E_ERROR, "Cannot access parent:: when current class scope has no parent");
     346           0 :                                 return NULL;
     347             :                         } else {
     348          10 :                                 ce = scope->parent;
     349             :                         }
     350         114 :                 } else if (class_name_len == sizeof("static")-1 &&
     351          13 :                            !memcmp(lcname, "static", sizeof("static")-1)) {
     352           5 :                         ce = zend_get_called_scope(EG(current_execute_data));
     353           5 :                         if (UNEXPECTED(!ce)) {
     354           0 :                                 zend_error(E_EXCEPTION | E_ERROR, "Cannot access static:: when no class scope is active");
     355           0 :                                 return NULL;
     356             :                         }
     357             :                 } else {
     358          91 :                         ce = zend_fetch_class(class_name, flags);
     359             :                 }
     360         133 :                 free_alloca(lcname, use_heap);
     361         133 :                 if (ce) {
     362         130 :                         ret_constant = zend_hash_find(&ce->constants_table, constant_name);
     363         130 :                         if (ret_constant == NULL) {
     364           6 :                                 if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
     365           1 :                                         zend_error(E_EXCEPTION | E_ERROR, "Undefined class constant '%s::%s'", class_name->val, constant_name->val);
     366             :                                         zend_string_release(class_name);
     367             :                                         zend_string_free(constant_name);
     368           1 :                                         return NULL;
     369             :                                 }
     370         124 :                         } else if (Z_ISREF_P(ret_constant)) {
     371          37 :                                 ret_constant = Z_REFVAL_P(ret_constant);
     372             :                         }
     373             :                 }
     374             :                 zend_string_release(class_name);
     375             :                 zend_string_free(constant_name);
     376         132 :                 if (ret_constant && Z_CONSTANT_P(ret_constant)) {
     377           8 :                         if (UNEXPECTED(zval_update_constant_ex(ret_constant, 1, ce) != SUCCESS)) {
     378           5 :                                 return NULL;
     379             :                         }
     380             :                 }
     381         127 :                 return ret_constant;
     382             :         }
     383             : 
     384             :         /* non-class constant */
     385         267 :         if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
     386             :                 /* compound constant name */
     387          77 :                 int prefix_len = colon - name;
     388          77 :                 size_t const_name_len = name_len - prefix_len - 1;
     389          77 :                 const char *constant_name = colon + 1;
     390             :                 char *lcname;
     391             :                 size_t lcname_len;
     392             :                 ALLOCA_FLAG(use_heap)
     393             : 
     394          77 :                 lcname_len = prefix_len + 1 + const_name_len;
     395          77 :                 lcname = do_alloca(lcname_len + 1, use_heap);
     396          77 :                 zend_str_tolower_copy(lcname, name, prefix_len);
     397             :                 /* Check for namespace constant */
     398             : 
     399          77 :                 lcname[prefix_len] = '\\';
     400          77 :                 memcpy(lcname + prefix_len + 1, constant_name, const_name_len + 1);
     401             : 
     402         154 :                 if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len)) == NULL) {
     403             :                         /* try lowercase */
     404          25 :                         zend_str_tolower(lcname + prefix_len + 1, const_name_len);
     405          50 :                         if ((c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len)) != NULL) {
     406           0 :                                 if ((c->flags & CONST_CS) != 0) {
     407           0 :                                         c = NULL;
     408             :                                 }
     409             :                         }
     410             :                 }
     411          77 :                 free_alloca(lcname, use_heap);
     412          77 :                 if (c) {
     413          52 :                         return &c->value;
     414             :                 }
     415             :                 /* name requires runtime resolution, need to check non-namespaced name */
     416          25 :                 if ((flags & IS_CONSTANT_UNQUALIFIED) != 0) {
     417          17 :                         return zend_get_constant_str(constant_name, const_name_len);
     418             :                 }
     419           8 :                 return NULL;
     420             :         }
     421             : 
     422         190 :         if (cname) {
     423         189 :                 return zend_get_constant(cname);
     424             :         } else {
     425           1 :                 return zend_get_constant_str(name, name_len);
     426             :         }
     427             : }
     428             : 
     429        3417 : zend_constant *zend_quick_get_constant(const zval *key, zend_ulong flags)
     430             : {
     431             :         zend_constant *c;
     432             : 
     433        6834 :         if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL) {
     434         304 :                 key++;
     435         830 :                 if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL ||
     436         222 :                     (c->flags & CONST_CS) != 0) {
     437          83 :                         if ((flags & (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) == (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) {
     438          14 :                                 key++;
     439          28 :                                 if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL) {
     440           5 :                                     key++;
     441          10 :                                         if ((c = zend_hash_find_ptr(EG(zend_constants), Z_STR_P(key))) == NULL ||
     442           0 :                                             (c->flags & CONST_CS) != 0) {
     443             : 
     444           5 :                                                 key--;
     445           5 :                                                 c = NULL;
     446             :                                         }
     447             :                                 }
     448             :                         } else {
     449          69 :                                 key--;
     450          69 :                                 c = NULL;
     451             :                         }
     452             :                 }
     453             :         }
     454        3417 :         return c;
     455             : }
     456             : 
     457    51593619 : static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
     458             : {
     459             :         void *ret;
     460   103187238 :         zend_constant *copy = pemalloc(sizeof(zend_constant), c->flags & CONST_PERSISTENT);
     461             : 
     462    51593619 :         memcpy(copy, c, sizeof(zend_constant));
     463    51593619 :         ret = zend_hash_add_ptr(ht, key, copy);
     464    51593619 :         if (!ret) {
     465           5 :                 pefree(copy, c->flags & CONST_PERSISTENT);
     466             :         }
     467    51593619 :         return ret;
     468             : }
     469             : 
     470    51593622 : ZEND_API int zend_register_constant(zend_constant *c)
     471             : {
     472    51593622 :         zend_string *lowercase_name = NULL;
     473             :         zend_string *name;
     474    51593622 :         int ret = SUCCESS;
     475             : 
     476             : #if 0
     477             :         printf("Registering constant for module %d\n", c->module_number);
     478             : #endif
     479             : 
     480    51593622 :         if (!(c->flags & CONST_CS)) {
     481     1852938 :                 lowercase_name = zend_string_alloc(c->name->len, c->flags & CONST_PERSISTENT);
     482      926469 :                 zend_str_tolower_copy(lowercase_name->val, c->name->val, c->name->len);
     483      926469 :                 lowercase_name = zend_new_interned_string(lowercase_name);
     484      926469 :                 name = lowercase_name;
     485             :         } else {
     486    50667153 :                 char *slash = strrchr(c->name->val, '\\');
     487    50667153 :                 if (slash) {
     488          92 :                         lowercase_name = zend_string_init(c->name->val, c->name->len, c->flags & CONST_PERSISTENT);
     489          46 :                         zend_str_tolower(lowercase_name->val, slash - c->name->val);
     490          46 :                         lowercase_name = zend_new_interned_string(lowercase_name);
     491          46 :                         name = lowercase_name;
     492             :                 } else {
     493    50667107 :                         name = c->name;
     494             :                 }
     495             :         }
     496             : 
     497             :         /* Check if the user is trying to define the internal pseudo constant name __COMPILER_HALT_OFFSET__ */
     498   156233286 :         if ((c->name->len == sizeof("__COMPILER_HALT_OFFSET__")-1
     499    53046045 :                 && !memcmp(name->val, "__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1))
     500    51593619 :                 || zend_hash_add_constant(EG(zend_constants), name, c) == NULL) {
     501             : 
     502             :                 /* The internal __COMPILER_HALT_OFFSET__ is prefixed by NULL byte */
     503           8 :                 if (c->name->val[0] == '\0' && c->name->len > sizeof("\0__COMPILER_HALT_OFFSET__")-1
     504           0 :                         && memcmp(name->val, "\0__COMPILER_HALT_OFFSET__", sizeof("\0__COMPILER_HALT_OFFSET__")) == 0) {
     505             :                 }
     506           8 :                 zend_error(E_NOTICE,"Constant %s already defined", name->val);
     507           8 :                 zend_string_release(c->name);
     508           8 :                 if (!(c->flags & CONST_PERSISTENT)) {
     509           8 :                         zval_dtor(&c->value);
     510             :                 }
     511           8 :                 ret = FAILURE;
     512             :         }
     513    51593622 :         if (lowercase_name) {
     514             :                 zend_string_release(lowercase_name);
     515             :         }
     516    51593622 :         return ret;
     517             : }
     518             : 
     519             : 
     520             : /*
     521             :  * Local variables:
     522             :  * tab-width: 4
     523             :  * c-basic-offset: 4
     524             :  * indent-tabs-mode: t
     525             :  * End:
     526             :  */

Generated by: LCOV version 1.10

Generated at Mon, 27 Jul 2015 02:32:15 +0000 (3 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.