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: 146 149 98.0 %
Date: 2014-09-21 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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             : /* }}} */
      26             : 
      27             : ZEND_BEGIN_MODULE_GLOBALS(assert)
      28             :         long active;
      29             :         long bail;
      30             :         long warning;
      31             :         long quiet_eval;
      32             :         zval *callback;
      33             :         char *cb;
      34             : ZEND_END_MODULE_GLOBALS(assert)
      35             : 
      36             : ZEND_DECLARE_MODULE_GLOBALS(assert)
      37             : 
      38             : #ifdef ZTS
      39             : #define ASSERTG(v) TSRMG(assert_globals_id, zend_assert_globals *, v)
      40             : #else
      41             : #define ASSERTG(v) (assert_globals.v)
      42             : #endif
      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             : };
      53             : 
      54       20229 : static PHP_INI_MH(OnChangeCallback) /* {{{ */
      55             : {
      56       20229 :         if (EG(in_execution)) {
      57           2 :                 if (ASSERTG(callback)) {
      58           2 :                         zval_ptr_dtor(&ASSERTG(callback));
      59           2 :                         ASSERTG(callback) = NULL;
      60             :                 }
      61           2 :                 if (new_value && (ASSERTG(callback) || new_value_length)) {
      62           2 :                         MAKE_STD_ZVAL(ASSERTG(callback));
      63           2 :                         ZVAL_STRINGL(ASSERTG(callback), new_value, new_value_length, 1);
      64             :                 }
      65             :         } else {
      66       20227 :                 if (ASSERTG(cb)) {
      67           1 :                         pefree(ASSERTG(cb), 1);
      68             :                 }
      69       20241 :                 if (new_value && new_value_length) {
      70          14 :                         ASSERTG(cb) = pemalloc(new_value_length + 1, 1);
      71          14 :                         memcpy(ASSERTG(cb), new_value, new_value_length);
      72          14 :                         ASSERTG(cb)[new_value_length] = '\0';
      73             :                 } else {
      74       20213 :                         ASSERTG(cb) = NULL;
      75             :                 }
      76             :         }
      77       20229 :         return SUCCESS;
      78             : }
      79             : /* }}} */
      80             : 
      81             : PHP_INI_BEGIN()
      82             :          STD_PHP_INI_ENTRY("assert.active",           "1",  PHP_INI_ALL,    OnUpdateLong,           active,                         zend_assert_globals,            assert_globals)
      83             :          STD_PHP_INI_ENTRY("assert.bail",             "0",  PHP_INI_ALL,    OnUpdateLong,           bail,                           zend_assert_globals,            assert_globals)
      84             :          STD_PHP_INI_ENTRY("assert.warning",  "1",  PHP_INI_ALL,    OnUpdateLong,           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,    OnUpdateLong,           quiet_eval,                     zend_assert_globals,            assert_globals)
      87             : PHP_INI_END()
      88             : 
      89       20225 : static void php_assert_init_globals(zend_assert_globals *assert_globals_p TSRMLS_DC) /* {{{ */
      90             : {
      91       20225 :         assert_globals_p->callback = NULL;
      92       20225 :         assert_globals_p->cb = NULL;
      93       20225 : }
      94             : /* }}} */
      95             : 
      96       20225 : PHP_MINIT_FUNCTION(assert) /* {{{ */
      97             : {
      98       20225 :         ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
      99             : 
     100       20225 :         REGISTER_INI_ENTRIES();
     101             : 
     102       20225 :         REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
     103       20225 :         REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
     104       20225 :         REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
     105       20225 :         REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
     106       20225 :         REGISTER_LONG_CONSTANT("ASSERT_QUIET_EVAL", ASSERT_QUIET_EVAL, CONST_CS|CONST_PERSISTENT);
     107             : 
     108       20225 :         return SUCCESS;
     109             : }
     110             : /* }}} */
     111             : 
     112       20261 : PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
     113             : {
     114       20261 :         if (ASSERTG(cb)) {
     115          13 :                 pefree(ASSERTG(cb), 1);
     116          13 :                 ASSERTG(cb) = NULL;
     117             :         }
     118       20261 :         return SUCCESS;
     119             : }
     120             : /* }}} */
     121             : 
     122       20220 : PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
     123             : {
     124       20220 :         if (ASSERTG(callback)) {
     125          10 :                 zval_ptr_dtor(&ASSERTG(callback));
     126          10 :                 ASSERTG(callback) = NULL;
     127             :         }
     128             : 
     129       20220 :         return SUCCESS;
     130             : }
     131             : /* }}} */
     132             : 
     133         149 : PHP_MINFO_FUNCTION(assert) /* {{{ */
     134             : {
     135         149 :         DISPLAY_INI_ENTRIES();
     136         149 : }
     137             : /* }}} */
     138             : 
     139             : /* {{{ proto int assert(string|bool assertion[, string description])
     140             :    Checks if assertion is false */
     141         232 : PHP_FUNCTION(assert)
     142             : {
     143             :         zval **assertion;
     144         232 :         int val, description_len = 0;
     145         232 :         char *myeval = NULL;
     146         232 :         char *compiled_string_description, *description = NULL;
     147             : 
     148         232 :         if (! ASSERTG(active)) {
     149           5 :                 RETURN_TRUE;
     150             :         }
     151             : 
     152         227 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|s", &assertion, &description, &description_len) == FAILURE) {
     153           2 :                 return;
     154             :         }
     155             : 
     156         225 :         if (Z_TYPE_PP(assertion) == IS_STRING) {
     157             :                 zval retval;
     158          50 :                 int old_error_reporting = 0; /* shut up gcc! */
     159             : 
     160          50 :                 myeval = Z_STRVAL_PP(assertion);
     161             : 
     162          50 :                 if (ASSERTG(quiet_eval)) {
     163          10 :                         old_error_reporting = EG(error_reporting);
     164          10 :                         EG(error_reporting) = 0;
     165             :                 }
     166             : 
     167          50 :                 compiled_string_description = zend_make_compiled_string_description("assert code" TSRMLS_CC);
     168          50 :                 if (zend_eval_stringl(myeval, Z_STRLEN_PP(assertion), &retval, compiled_string_description TSRMLS_CC) == FAILURE) {
     169           5 :                         efree(compiled_string_description);
     170           5 :                         if (description_len == 0) {
     171           4 :                                 php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
     172             :                         } else {
     173           1 :                                 php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s:\"%s\"", PHP_EOL, description, myeval);
     174             :                         }
     175           2 :                         if (ASSERTG(bail)) {
     176           1 :                                 zend_bailout();
     177             :                         }
     178           1 :                         RETURN_FALSE;
     179             :                 }
     180          45 :                 efree(compiled_string_description);
     181             : 
     182          45 :                 if (ASSERTG(quiet_eval)) {
     183           9 :                         EG(error_reporting) = old_error_reporting;
     184             :                 }
     185             : 
     186          45 :                 convert_to_boolean(&retval);
     187          45 :                 val = Z_LVAL(retval);
     188             :         } else {
     189         404 :                 convert_to_boolean_ex(assertion);
     190         175 :                 val = Z_LVAL_PP(assertion);
     191             :         }
     192             : 
     193         220 :         if (val) {
     194         187 :                 RETURN_TRUE;
     195             :         }
     196             : 
     197          33 :         if (!ASSERTG(callback) && ASSERTG(cb)) {
     198           7 :                 MAKE_STD_ZVAL(ASSERTG(callback));
     199           7 :                 ZVAL_STRING(ASSERTG(callback), ASSERTG(cb), 1);
     200             :         }
     201             : 
     202          33 :         if (ASSERTG(callback)) {
     203          20 :                 zval **args = safe_emalloc(description_len == 0 ? 3 : 4, sizeof(zval **), 0);
     204             :                 zval *retval;
     205             :                 int i;
     206          20 :                 uint lineno = zend_get_executed_lineno(TSRMLS_C);
     207          20 :                 const char *filename = zend_get_executed_filename(TSRMLS_C);
     208             : 
     209          20 :                 MAKE_STD_ZVAL(args[0]);
     210          20 :                 MAKE_STD_ZVAL(args[1]);
     211          20 :                 MAKE_STD_ZVAL(args[2]);
     212             : 
     213          20 :                 ZVAL_STRING(args[0], SAFE_STRING(filename), 1);
     214          20 :                 ZVAL_LONG (args[1], lineno);
     215          20 :                 ZVAL_STRING(args[2], SAFE_STRING(myeval), 1);
     216             : 
     217          20 :                 MAKE_STD_ZVAL(retval);
     218          20 :                 ZVAL_FALSE(retval);
     219             : 
     220             :                 /* XXX do we want to check for error here? */
     221          20 :                 if (description_len == 0) {
     222          19 :                         call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 3, args TSRMLS_CC);
     223          76 :                         for (i = 0; i <= 2; i++) {
     224          57 :                                 zval_ptr_dtor(&(args[i]));
     225             :                         }
     226             :                 } else {
     227           1 :                         MAKE_STD_ZVAL(args[3]);
     228           1 :                         ZVAL_STRINGL(args[3], SAFE_STRING(description), description_len, 1);
     229             : 
     230           1 :                         call_user_function(CG(function_table), NULL, ASSERTG(callback), retval, 4, args TSRMLS_CC);
     231           5 :                         for (i = 0; i <= 3; i++) {
     232           4 :                                 zval_ptr_dtor(&(args[i]));
     233             :                         }
     234             :                 }
     235             : 
     236          20 :                 efree(args);
     237          20 :                 zval_ptr_dtor(&retval);
     238             :         }
     239             : 
     240          33 :         if (ASSERTG(warning)) {
     241          20 :                 if (description_len == 0) {
     242          19 :                         if (myeval) {
     243           5 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion \"%s\" failed", myeval);
     244             :                         } else {
     245          14 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Assertion failed");
     246             :                         }
     247             :                 } else {
     248           1 :                         if (myeval) {
     249           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: \"%s\" failed", description, myeval);
     250             :                         } else {
     251           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s failed", description);
     252             :                         }
     253             :                 }
     254             :         }
     255             : 
     256          33 :         if (ASSERTG(bail)) {
     257           4 :                 zend_bailout();
     258             :         }
     259             : }
     260             : /* }}} */
     261             : 
     262             : /* {{{ proto mixed assert_options(int what [, mixed value])
     263             :    Set/get the various assert flags */
     264          44 : PHP_FUNCTION(assert_options)
     265             : {
     266          44 :         zval **value = NULL;
     267             :         long what;
     268             :         int oldint;
     269          44 :         int ac = ZEND_NUM_ARGS();
     270             : 
     271          44 :         if (zend_parse_parameters(ac TSRMLS_CC, "l|Z", &what, &value) == FAILURE) {
     272           4 :                 return;
     273             :         }
     274             : 
     275          40 :         switch (what) {
     276             :         case ASSERT_ACTIVE:
     277           5 :                 oldint = ASSERTG(active);
     278           5 :                 if (ac == 2) {
     279          16 :                         convert_to_string_ex(value);
     280           4 :                         zend_alter_ini_entry_ex("assert.active", sizeof("assert.active"), Z_STRVAL_PP(value), Z_STRLEN_PP(value), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
     281             :                 }
     282           5 :                 RETURN_LONG(oldint);
     283             :                 break;
     284             : 
     285             :         case ASSERT_BAIL:
     286           6 :                 oldint = ASSERTG(bail);
     287           6 :                 if (ac == 2) {
     288          20 :                         convert_to_string_ex(value);
     289           5 :                         zend_alter_ini_entry_ex("assert.bail", sizeof("assert.bail"), Z_STRVAL_PP(value), Z_STRLEN_PP(value), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
     290             :                 }
     291           6 :                 RETURN_LONG(oldint);
     292             :                 break;
     293             : 
     294             :         case ASSERT_QUIET_EVAL:
     295           4 :                 oldint = ASSERTG(quiet_eval);
     296           4 :                 if (ac == 2) {
     297          12 :                         convert_to_string_ex(value);
     298           3 :                         zend_alter_ini_entry_ex("assert.quiet_eval", sizeof("assert.quiet_eval"), Z_STRVAL_PP(value), Z_STRLEN_PP(value), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
     299             :                 }
     300           4 :                 RETURN_LONG(oldint);
     301             :                 break;
     302             : 
     303             :         case ASSERT_WARNING:
     304           6 :                 oldint = ASSERTG(warning);
     305           6 :                 if (ac == 2) {
     306          20 :                         convert_to_string_ex(value);
     307           5 :                         zend_alter_ini_entry_ex("assert.warning", sizeof("assert.warning"), Z_STRVAL_PP(value), Z_STRLEN_PP(value), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
     308             :                 }
     309           6 :                 RETURN_LONG(oldint);
     310             :                 break;
     311             : 
     312             :         case ASSERT_CALLBACK:
     313          19 :                 if (ASSERTG(callback) != NULL) {
     314          39 :                         RETVAL_ZVAL(ASSERTG(callback), 1, 0);
     315           6 :                 } else if (ASSERTG(cb)) {
     316           3 :                         RETVAL_STRING(ASSERTG(cb), 1);
     317             :                 } else {
     318           3 :                         RETVAL_NULL();
     319             :                 }
     320          19 :                 if (ac == 2) {
     321          10 :                         if (ASSERTG(callback)) {
     322           7 :                                 zval_ptr_dtor(&ASSERTG(callback));
     323             :                         }
     324          10 :                         ASSERTG(callback) = *value;
     325          10 :                         zval_add_ref(value);
     326             :                 }
     327          19 :                 return;
     328             :                 break;
     329             : 
     330             :         default:
     331           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown value %ld", what);
     332             :                 break;
     333             :         }
     334             : 
     335           0 :         RETURN_FALSE;
     336             : }
     337             : /* }}} */
     338             : 
     339             : /*
     340             :  * Local variables:
     341             :  * tab-width: 4
     342             :  * c-basic-offset: 4
     343             :  * End:
     344             :  * vim600: sw=4 ts=4 fdm=marker
     345             :  * vim<600: sw=4 ts=4
     346             :  */
     347             : 

Generated by: LCOV version 1.10

Generated at Sun, 21 Sep 2014 15:27:45 +0000 (2 days ago)

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