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 - ext/standard - assert.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 155 165 93.9 %
Date: 2016-08-28 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Thies C. Arntzen <thies@thieso.net>                          |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : /* {{{ includes */
      22             : #include "php.h"
      23             : #include "php_assert.h"
      24             : #include "php_ini.h"
      25             : #include "zend_exceptions.h"
      26             : /* }}} */
      27             : 
      28             : ZEND_BEGIN_MODULE_GLOBALS(assert)
      29             :         zval callback;
      30             :         char *cb;
      31             :         zend_bool active;
      32             :         zend_bool bail;
      33             :         zend_bool warning;
      34             :         zend_bool quiet_eval;
      35             :         zend_bool exception;
      36             : ZEND_END_MODULE_GLOBALS(assert)
      37             : 
      38             : ZEND_DECLARE_MODULE_GLOBALS(assert)
      39             : 
      40             : static zend_class_entry *assertion_error_ce;
      41             : 
      42             : #define ASSERTG(v) ZEND_MODULE_GLOBALS_ACCESSOR(assert, v)
      43             : 
      44             : #define SAFE_STRING(s) ((s)?(s):"")
      45             : 
      46             : enum {
      47             :         ASSERT_ACTIVE=1,
      48             :         ASSERT_CALLBACK,
      49             :         ASSERT_BAIL,
      50             :         ASSERT_WARNING,
      51             :         ASSERT_QUIET_EVAL,
      52             :         ASSERT_EXCEPTION
      53             : };
      54             : 
      55       23508 : static PHP_INI_MH(OnChangeCallback) /* {{{ */
      56             : {
      57       23508 :         if (EG(current_execute_data)) {
      58           2 :                 if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
      59           2 :                         zval_ptr_dtor(&ASSERTG(callback));
      60           2 :                         ZVAL_UNDEF(&ASSERTG(callback));
      61             :                 }
      62           4 :                 if (new_value && (Z_TYPE(ASSERTG(callback)) != IS_UNDEF || ZSTR_LEN(new_value))) {
      63           2 :                         ZVAL_STR_COPY(&ASSERTG(callback), new_value);
      64             :                 }
      65             :         } else {
      66       23506 :                 if (ASSERTG(cb)) {
      67           1 :                         pefree(ASSERTG(cb), 1);
      68             :                 }
      69       23520 :                 if (new_value && ZSTR_LEN(new_value)) {
      70          14 :                         ASSERTG(cb) = pemalloc(ZSTR_LEN(new_value) + 1, 1);
      71          14 :                         memcpy(ASSERTG(cb), ZSTR_VAL(new_value), ZSTR_LEN(new_value));
      72          14 :                         ASSERTG(cb)[ZSTR_LEN(new_value)] = '\0';
      73             :                 } else {
      74       23492 :                         ASSERTG(cb) = NULL;
      75             :                 }
      76             :         }
      77       23508 :         return SUCCESS;
      78             : }
      79             : /* }}} */
      80             : 
      81             : PHP_INI_BEGIN()
      82             :          STD_PHP_INI_ENTRY("assert.active",           "1",  PHP_INI_ALL,    OnUpdateBool,           active,                         zend_assert_globals,            assert_globals)
      83             :          STD_PHP_INI_ENTRY("assert.bail",             "0",  PHP_INI_ALL,    OnUpdateBool,           bail,                           zend_assert_globals,            assert_globals)
      84             :          STD_PHP_INI_ENTRY("assert.warning",  "1",  PHP_INI_ALL,    OnUpdateBool,           warning,                        zend_assert_globals,            assert_globals)
      85             :          PHP_INI_ENTRY("assert.callback",             NULL,   PHP_INI_ALL,    OnChangeCallback)
      86             :          STD_PHP_INI_ENTRY("assert.quiet_eval", "0",        PHP_INI_ALL,    OnUpdateBool,           quiet_eval,                     zend_assert_globals,            assert_globals)
      87             :          STD_PHP_INI_ENTRY("assert.exception",        "0",  PHP_INI_ALL,    OnUpdateBool,           exception,                      zend_assert_globals,            assert_globals)
      88             : PHP_INI_END()
      89             : 
      90       23504 : static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */
      91             : {
      92       23504 :         ZVAL_UNDEF(&assert_globals_p->callback);
      93       23504 :         assert_globals_p->cb = NULL;
      94       23504 : }
      95             : /* }}} */
      96             : 
      97       23504 : PHP_MINIT_FUNCTION(assert) /* {{{ */
      98             : {
      99             :         zend_class_entry ce;
     100             : 
     101       23504 :         ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
     102             : 
     103       23504 :         REGISTER_INI_ENTRIES();
     104             : 
     105       23504 :         REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
     106       23504 :         REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
     107       23504 :         REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
     108       23504 :         REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
     109       23504 :         REGISTER_LONG_CONSTANT("ASSERT_QUIET_EVAL", ASSERT_QUIET_EVAL, CONST_CS|CONST_PERSISTENT);
     110       23504 :         REGISTER_LONG_CONSTANT("ASSERT_EXCEPTION", ASSERT_EXCEPTION, CONST_CS|CONST_PERSISTENT);
     111             : 
     112       23504 :         INIT_CLASS_ENTRY(ce, "AssertionError", NULL);
     113       23504 :         assertion_error_ce = zend_register_internal_class_ex(&ce, zend_ce_error);
     114             : 
     115       23504 :         return SUCCESS;
     116             : }
     117             : /* }}} */
     118             : 
     119       23544 : PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
     120             : {
     121       23544 :         if (ASSERTG(cb)) {
     122          13 :                 pefree(ASSERTG(cb), 1);
     123          13 :                 ASSERTG(cb) = NULL;
     124             :         }
     125       23544 :         return SUCCESS;
     126             : }
     127             : /* }}} */
     128             : 
     129       23500 : PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
     130             : {
     131       23500 :         if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
     132          10 :                 zval_ptr_dtor(&ASSERTG(callback));
     133          10 :                 ZVAL_UNDEF(&ASSERTG(callback));
     134             :         }
     135             : 
     136       23500 :         return SUCCESS;
     137             : }
     138             : /* }}} */
     139             : 
     140         151 : PHP_MINFO_FUNCTION(assert) /* {{{ */
     141             : {
     142         151 :         DISPLAY_INI_ENTRIES();
     143         151 : }
     144             : /* }}} */
     145             : 
     146             : /* {{{ proto int assert(string|bool assertion[, mixed description])
     147             :    Checks if assertion is false */
     148      100239 : PHP_FUNCTION(assert)
     149             : {
     150             :         zval *assertion;
     151      100239 :         zval *description = NULL;
     152             :         int val;
     153      100239 :         char *myeval = NULL;
     154             :         char *compiled_string_description;
     155             : 
     156      100239 :         if (! ASSERTG(active)) {
     157           5 :                 RETURN_TRUE;
     158             :         }
     159             : 
     160      100234 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|z", &assertion, &description) == FAILURE) {
     161           2 :                 return;
     162             :         }
     163             : 
     164      200464 :         if (Z_TYPE_P(assertion) == IS_STRING) {
     165             :                 zval retval;
     166          74 :                 int old_error_reporting = 0; /* shut up gcc! */
     167             : 
     168          74 :                 if (zend_forbid_dynamic_call("assert() with string argument") == FAILURE) {
     169           1 :                         RETURN_FALSE;
     170             :                 }
     171             : 
     172          73 :                 myeval = Z_STRVAL_P(assertion);
     173             : 
     174          73 :                 if (ASSERTG(quiet_eval)) {
     175          10 :                         old_error_reporting = EG(error_reporting);
     176          10 :                         EG(error_reporting) = 0;
     177             :                 }
     178             : 
     179          73 :                 compiled_string_description = zend_make_compiled_string_description("assert code");
     180          73 :                 if (zend_eval_stringl(myeval, Z_STRLEN_P(assertion), &retval, compiled_string_description) == FAILURE) {
     181           5 :                         efree(compiled_string_description);
     182           5 :                         if (!description) {
     183           4 :                                 zend_throw_error(NULL, "Failure evaluating code: %s%s", PHP_EOL, myeval);
     184             :                         } else {
     185           2 :                                 zend_string *str = zval_get_string(description);
     186           1 :                                 zend_throw_error(NULL, "Failure evaluating code: %s%s:\"%s\"", PHP_EOL, ZSTR_VAL(str), myeval);
     187             :                                 zend_string_release(str);
     188             :                         }
     189           5 :                         if (ASSERTG(bail)) {
     190           1 :                                 zend_bailout();
     191             :                         }
     192           4 :                         RETURN_FALSE;
     193             :                 }
     194          68 :                 efree(compiled_string_description);
     195             : 
     196          68 :                 if (ASSERTG(quiet_eval)) {
     197           9 :                         EG(error_reporting) = old_error_reporting;
     198             :                 }
     199             : 
     200          68 :                 convert_to_boolean(&retval);
     201          68 :                 val = Z_TYPE(retval) == IS_TRUE;
     202             :         } else {
     203      100158 :                 val = zend_is_true(assertion);
     204             :         }
     205             : 
     206      100226 :         if (val) {
     207      100173 :                 RETURN_TRUE;
     208             :         }
     209             : 
     210          53 :         if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
     211          14 :                 ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
     212             :         }
     213             : 
     214          53 :         if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
     215          20 :                 zval *args = safe_emalloc(!description ? 3 : 4, sizeof(zval), 0);
     216             :                 zval retval;
     217             :                 int i;
     218          20 :                 uint lineno = zend_get_executed_lineno();
     219          20 :                 const char *filename = zend_get_executed_filename();
     220             : 
     221          40 :                 ZVAL_STRING(&args[0], SAFE_STRING(filename));
     222          20 :                 ZVAL_LONG (&args[1], lineno);
     223          40 :                 ZVAL_STRING(&args[2], SAFE_STRING(myeval));
     224             : 
     225          20 :                 ZVAL_FALSE(&retval);
     226             : 
     227             :                 /* XXX do we want to check for error here? */
     228          20 :                 if (!description) {
     229           6 :                         call_user_function(CG(function_table), NULL, &ASSERTG(callback), &retval, 3, args);
     230          24 :                         for (i = 0; i <= 2; i++) {
     231          18 :                                 zval_ptr_dtor(&(args[i]));
     232             :                         }
     233             :                 } else {
     234          28 :                         ZVAL_STR(&args[3], zval_get_string(description));
     235          14 :                         call_user_function(CG(function_table), NULL, &ASSERTG(callback), &retval, 4, args);
     236          70 :                         for (i = 0; i <= 3; i++) {
     237          56 :                                 zval_ptr_dtor(&(args[i]));
     238             :                         }
     239             :                 }
     240             : 
     241          20 :                 efree(args);
     242          20 :                 zval_ptr_dtor(&retval);
     243             :         }
     244             : 
     245          53 :         if (ASSERTG(exception)) {
     246          11 :                 if (!description) {
     247           0 :                         zend_throw_exception(assertion_error_ce, NULL, E_ERROR);
     248          26 :                 } else if (Z_TYPE_P(description) == IS_OBJECT &&
     249           2 :                         instanceof_function(Z_OBJCE_P(description), zend_ce_throwable)) {
     250           2 :                         Z_ADDREF_P(description);
     251           2 :                         zend_throw_exception_object(description);
     252             :                 } else {
     253          18 :                         zend_string *str = zval_get_string(description);
     254           9 :                         zend_throw_exception(assertion_error_ce, ZSTR_VAL(str), E_ERROR);
     255             :                         zend_string_release(str);
     256             :                 }
     257          42 :         } else if (ASSERTG(warning)) {
     258          29 :                 if (!description) {
     259           3 :                         if (myeval) {
     260           3 :                                 php_error_docref(NULL, E_WARNING, "Assertion \"%s\" failed", myeval);
     261             :                         } else {
     262           0 :                                 php_error_docref(NULL, E_WARNING, "Assertion failed");
     263             :                         }
     264             :                 } else {
     265          52 :                         zend_string *str = zval_get_string(description);
     266          26 :                         if (myeval) {
     267           3 :                                 php_error_docref(NULL, E_WARNING, "%s: \"%s\" failed", ZSTR_VAL(str), myeval);
     268             :                         } else {
     269          23 :                                 php_error_docref(NULL, E_WARNING, "%s failed", ZSTR_VAL(str));
     270             :                         }
     271             :                         zend_string_release(str);
     272             :                 }
     273             :         }
     274             : 
     275          53 :         if (ASSERTG(bail)) {
     276           6 :                 zend_bailout();
     277             :         }
     278             :         
     279          47 :         RETURN_FALSE;
     280             : }
     281             : /* }}} */
     282             : 
     283             : /* {{{ proto mixed assert_options(int what [, mixed value])
     284             :    Set/get the various assert flags */
     285          45 : PHP_FUNCTION(assert_options)
     286             : {
     287          45 :         zval *value = NULL;
     288             :         zend_long what;
     289             :         zend_bool oldint;
     290          45 :         int ac = ZEND_NUM_ARGS();
     291             :         zend_string *key;
     292             : 
     293          45 :         if (zend_parse_parameters(ac, "l|z", &what, &value) == FAILURE) {
     294           4 :                 return;
     295             :         }
     296             : 
     297          41 :         switch (what) {
     298             :         case ASSERT_ACTIVE:
     299           5 :                 oldint = ASSERTG(active);
     300           5 :                 if (ac == 2) {
     301           8 :                         zend_string *value_str = zval_get_string(value);
     302           4 :                         key = zend_string_init("assert.active", sizeof("assert.active")-1, 0);
     303           4 :                         zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     304             :                         zend_string_release(key);
     305             :                         zend_string_release(value_str);
     306             :                 }
     307           5 :                 RETURN_LONG(oldint);
     308             :                 break;
     309             : 
     310             :         case ASSERT_BAIL:
     311           6 :                 oldint = ASSERTG(bail);
     312           6 :                 if (ac == 2) {
     313          10 :                         zend_string *value_str = zval_get_string(value);
     314           5 :                         key = zend_string_init("assert.bail", sizeof("assert.bail")-1, 0);
     315           5 :                         zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     316             :                         zend_string_release(key);
     317             :                         zend_string_release(value_str);
     318             :                 }
     319           6 :                 RETURN_LONG(oldint);
     320             :                 break;
     321             : 
     322             :         case ASSERT_QUIET_EVAL:
     323           4 :                 oldint = ASSERTG(quiet_eval);
     324           4 :                 if (ac == 2) {
     325           6 :                         zend_string *value_str = zval_get_string(value);
     326           3 :                         key = zend_string_init("assert.quiet_eval", sizeof("assert.quiet_eval")-1, 0);
     327           3 :                         zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     328             :                         zend_string_release(key);
     329             :                         zend_string_release(value_str);
     330             :                 }
     331           4 :                 RETURN_LONG(oldint);
     332             :                 break;
     333             : 
     334             :         case ASSERT_WARNING:
     335           7 :                 oldint = ASSERTG(warning);
     336           7 :                 if (ac == 2) {
     337          10 :                         zend_string *value_str = zval_get_string(value);
     338           5 :                         key = zend_string_init("assert.warning", sizeof("assert.warning")-1, 0);
     339           5 :                         zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     340             :                         zend_string_release(key);
     341             :                         zend_string_release(value_str);
     342             :                 }
     343           7 :                 RETURN_LONG(oldint);
     344             :                 break;
     345             : 
     346             :         case ASSERT_CALLBACK:
     347          19 :                 if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
     348          13 :                         ZVAL_COPY(return_value, &ASSERTG(callback));
     349           6 :                 } else if (ASSERTG(cb)) {
     350           6 :                         RETVAL_STRING(ASSERTG(cb));
     351             :                 } else {
     352           3 :                         RETVAL_NULL();
     353             :                 }
     354          19 :                 if (ac == 2) {
     355          10 :                         zval_ptr_dtor(&ASSERTG(callback));
     356          10 :                         ZVAL_COPY(&ASSERTG(callback), value);
     357             :                 }
     358          19 :                 return;
     359             : 
     360             :         case ASSERT_EXCEPTION:
     361           0 :                 oldint = ASSERTG(exception);
     362           0 :                 if (ac == 2) {
     363           0 :                         zend_string *key = zend_string_init("assert.exception", sizeof("assert.exception")-1, 0);
     364           0 :                         zend_string *val = zval_get_string(value);
     365           0 :                         zend_alter_ini_entry_ex(key, val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     366             :                         zend_string_release(val);
     367             :                         zend_string_release(key);
     368             :                 }
     369           0 :                 RETURN_LONG(oldint);
     370             :                 break;
     371             : 
     372             :         default:
     373           0 :                 php_error_docref(NULL, E_WARNING, "Unknown value " ZEND_LONG_FMT, what);
     374             :                 break;
     375             :         }
     376             : 
     377           0 :         RETURN_FALSE;
     378             : }
     379             : /* }}} */
     380             : 
     381             : /*
     382             :  * Local variables:
     383             :  * tab-width: 4
     384             :  * c-basic-offset: 4
     385             :  * End:
     386             :  * vim600: sw=4 ts=4 fdm=marker
     387             :  * vim<600: sw=4 ts=4
     388             :  */
     389             : 

Generated by: LCOV version 1.10

Generated at Sun, 28 Aug 2016 17:10:13 +0000 (2 days ago)

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