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_exceptions.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 323 372 86.8 %
Date: 2014-04-18 Functions: 30 33 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2013 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             :    |          Marcus Boerger <helly@php.net>                              |
      17             :    |          Sterling Hughes <sterling@php.net>                          |
      18             :    |          Zeev Suraski <zeev@zend.com>                                |
      19             :    +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : /* $Id$ */
      23             : 
      24             : #include "zend.h"
      25             : #include "zend_API.h"
      26             : #include "zend_builtin_functions.h"
      27             : #include "zend_interfaces.h"
      28             : #include "zend_exceptions.h"
      29             : #include "zend_vm.h"
      30             : 
      31             : zend_class_entry *default_exception_ce;
      32             : zend_class_entry *error_exception_ce;
      33             : static zend_object_handlers default_exception_handlers;
      34             : ZEND_API void (*zend_throw_exception_hook)(zval *ex TSRMLS_DC);
      35             : 
      36        1148 : void zend_exception_set_previous(zval *exception, zval *add_previous TSRMLS_DC)
      37             : {
      38             :         zval *previous;
      39             : 
      40        1148 :         if (exception == add_previous || !add_previous || !exception) {
      41        1141 :                 return;
      42             :         }
      43           7 :         if (Z_TYPE_P(add_previous) != IS_OBJECT && !instanceof_function(Z_OBJCE_P(add_previous), default_exception_ce TSRMLS_CC)) {
      44           0 :                 zend_error(E_ERROR, "Cannot set non exception as previous exception");
      45           0 :                 return;
      46             :         }
      47          14 :         while (exception && exception != add_previous && Z_OBJ_HANDLE_P(exception) != Z_OBJ_HANDLE_P(add_previous)) {
      48           7 :                 previous = zend_read_property(default_exception_ce, exception, "previous", sizeof("previous")-1, 1 TSRMLS_CC);
      49           7 :                 if (Z_TYPE_P(previous) == IS_NULL) {
      50           7 :                         zend_update_property(default_exception_ce, exception, "previous", sizeof("previous")-1, add_previous TSRMLS_CC);
      51             :                         Z_DELREF_P(add_previous);
      52           7 :                         return;
      53             :                 }
      54           0 :                 exception = previous;
      55             :         }
      56             : }
      57             : 
      58         631 : void zend_exception_save(TSRMLS_D) /* {{{ */
      59             : {
      60         631 :         if (EG(prev_exception)) {
      61           4 :                 zend_exception_set_previous(EG(exception), EG(prev_exception) TSRMLS_CC);
      62             :         }
      63         631 :         if (EG(exception)) {
      64          14 :                 EG(prev_exception) = EG(exception);
      65             :         }
      66         631 :         EG(exception) = NULL;
      67         631 : }
      68             : /* }}} */
      69             : 
      70       19496 : void zend_exception_restore(TSRMLS_D) /* {{{ */
      71             : {
      72       19496 :         if (EG(prev_exception)) {
      73          14 :                 if (EG(exception)) {
      74           3 :                         zend_exception_set_previous(EG(exception), EG(prev_exception) TSRMLS_CC);
      75             :                 } else {
      76          11 :                         EG(exception) = EG(prev_exception);
      77             :                 }
      78          14 :                 EG(prev_exception) = NULL;
      79             :         }
      80       19496 : }
      81             : /* }}} */
      82             : 
      83        2472 : void zend_throw_exception_internal(zval *exception TSRMLS_DC) /* {{{ */
      84             : {
      85        2472 :         if (exception != NULL) {
      86        1137 :                 zval *previous = EG(exception);
      87        1137 :                 zend_exception_set_previous(exception, EG(exception) TSRMLS_CC);
      88        1137 :                 EG(exception) = exception;
      89        1137 :                 if (previous) {
      90           0 :                         return;
      91             :                 }
      92             :         }
      93        2472 :         if (!EG(current_execute_data)) {
      94           3 :                 if(EG(exception)) {
      95           3 :                         zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
      96             :                 }
      97           0 :                 zend_error(E_ERROR, "Exception thrown without a stack frame");
      98             :         }
      99             : 
     100        2469 :         if (zend_throw_exception_hook) {
     101           0 :                 zend_throw_exception_hook(exception TSRMLS_CC);
     102             :         }
     103             : 
     104        4861 :         if (EG(current_execute_data)->opline == NULL ||
     105        2392 :             (EG(current_execute_data)->opline+1)->opcode == ZEND_HANDLE_EXCEPTION) {
     106             :                 /* no need to rethrow the exception */
     107        1020 :                 return;
     108             :         }
     109        1449 :         EG(opline_before_exception) = EG(current_execute_data)->opline;
     110        1449 :         EG(current_execute_data)->opline = EG(exception_op);
     111             : }
     112             : /* }}} */
     113             : 
     114         102 : ZEND_API void zend_clear_exception(TSRMLS_D) /* {{{ */
     115             : {
     116         102 :         if (EG(prev_exception)) {
     117           0 :                 zval_ptr_dtor(&EG(prev_exception));
     118           0 :                 EG(prev_exception) = NULL;
     119             :         }
     120         102 :         if (!EG(exception)) {
     121          92 :                 return;
     122             :         }
     123          10 :         zval_ptr_dtor(&EG(exception));
     124          10 :         EG(exception) = NULL;
     125          10 :         EG(current_execute_data)->opline = EG(opline_before_exception);
     126             : #if ZEND_DEBUG
     127             :         EG(opline_before_exception) = NULL;
     128             : #endif
     129             : }
     130             : /* }}} */
     131             : 
     132        1555 : static zend_object_value zend_default_exception_new_ex(zend_class_entry *class_type, int skip_top_traces TSRMLS_DC) /* {{{ */
     133             : {
     134             :         zval tmp, obj;
     135             :         zend_object *object;
     136             :         zval *trace;
     137             : 
     138        1555 :         Z_OBJVAL(obj) = zend_objects_new(&object, class_type TSRMLS_CC);
     139        1555 :         Z_OBJ_HT(obj) = &default_exception_handlers;
     140             : 
     141        1555 :         ALLOC_HASHTABLE(object->properties);
     142        1555 :         zend_hash_init(object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
     143        1555 :         zend_hash_copy(object->properties, &class_type->default_properties, zval_copy_property_ctor(class_type), (void *) &tmp, sizeof(zval *));
     144             : 
     145        1555 :         ALLOC_ZVAL(trace);
     146             :         Z_UNSET_ISREF_P(trace);
     147             :         Z_SET_REFCOUNT_P(trace, 0);
     148        1555 :         zend_fetch_debug_backtrace(trace, skip_top_traces, 0 TSRMLS_CC);
     149             : 
     150        1555 :         zend_update_property_string(default_exception_ce, &obj, "file", sizeof("file")-1, zend_get_executed_filename(TSRMLS_C) TSRMLS_CC);
     151        1555 :         zend_update_property_long(default_exception_ce, &obj, "line", sizeof("line")-1, zend_get_executed_lineno(TSRMLS_C) TSRMLS_CC);
     152        1555 :         zend_update_property(default_exception_ce, &obj, "trace", sizeof("trace")-1, trace TSRMLS_CC);
     153             : 
     154        1555 :         return Z_OBJVAL(obj);
     155             : }
     156             : /* }}} */
     157             : 
     158        1553 : static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
     159             : {
     160        1553 :         return zend_default_exception_new_ex(class_type, 0 TSRMLS_CC);
     161             : }
     162             : /* }}} */
     163             : 
     164           2 : static zend_object_value zend_error_exception_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
     165             : {
     166           2 :         return zend_default_exception_new_ex(class_type, 2 TSRMLS_CC);
     167             : }
     168             : /* }}} */
     169             : 
     170             : /* {{{ proto Exception Exception::__clone()
     171             :    Clone the exception object */
     172           0 : ZEND_METHOD(exception, __clone)
     173             : {
     174             :         /* Should never be executable */
     175           0 :         zend_throw_exception(NULL, "Cannot clone object using __clone()", 0 TSRMLS_CC);
     176           0 : }
     177             : /* }}} */
     178             : 
     179             : /* {{{ proto Exception::__construct(string message, int code [, Exception previous])
     180             :    Exception constructor */
     181         235 : ZEND_METHOD(exception, __construct)
     182             : {
     183         235 :         char  *message = NULL;
     184         235 :         long   code = 0;
     185         235 :         zval  *object, *previous = NULL;
     186         235 :         int    argc = ZEND_NUM_ARGS(), message_len;
     187             : 
     188         235 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|slO!", &message, &message_len, &code, &previous, default_exception_ce) == FAILURE) {
     189           0 :                 zend_error(E_ERROR, "Wrong parameters for Exception([string $exception [, long $code [, Exception $previous = NULL]]])");
     190             :         }
     191             : 
     192         235 :         object = getThis();
     193             : 
     194         235 :         if (message) {
     195         153 :                 zend_update_property_stringl(default_exception_ce, object, "message", sizeof("message")-1, message, message_len TSRMLS_CC);
     196             :         }
     197             : 
     198         235 :         if (code) {
     199          12 :                 zend_update_property_long(default_exception_ce, object, "code", sizeof("code")-1, code TSRMLS_CC);
     200             :         }
     201             : 
     202         235 :         if (previous) {
     203           3 :                 zend_update_property(default_exception_ce, object, "previous", sizeof("previous")-1, previous TSRMLS_CC);
     204             :         }
     205         235 : }
     206             : /* }}} */
     207             : 
     208             : /* {{{ proto ErrorException::__construct(string message, int code, int severity [, string filename [, int lineno [, Exception previous]]])
     209             :    ErrorException constructor */
     210           2 : ZEND_METHOD(error_exception, __construct)
     211             : {
     212           2 :         char  *message = NULL, *filename = NULL;
     213           2 :         long   code = 0, severity = E_ERROR, lineno;
     214           2 :         zval  *object, *previous = NULL;
     215           2 :         int    argc = ZEND_NUM_ARGS(), message_len, filename_len;
     216             : 
     217           2 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|sllslO!", &message, &message_len, &code, &severity, &filename, &filename_len, &lineno, &previous, default_exception_ce) == FAILURE) {
     218           0 :                 zend_error(E_ERROR, "Wrong parameters for ErrorException([string $exception [, long $code, [ long $severity, [ string $filename, [ long $lineno  [, Exception $previous = NULL]]]]]])");
     219             :         }
     220             : 
     221           2 :         object = getThis();
     222             : 
     223           2 :         if (message) {
     224           2 :                 zend_update_property_string(default_exception_ce, object, "message", sizeof("message")-1, message TSRMLS_CC);
     225             :         }
     226             : 
     227           2 :         if (code) {
     228           1 :                 zend_update_property_long(default_exception_ce, object, "code", sizeof("code")-1, code TSRMLS_CC);
     229             :         }
     230             : 
     231           2 :         if (previous) {
     232           0 :                 zend_update_property(default_exception_ce, object, "previous", sizeof("previous")-1, previous TSRMLS_CC);
     233             :         }
     234             : 
     235           2 :         zend_update_property_long(default_exception_ce, object, "severity", sizeof("severity")-1, severity TSRMLS_CC);
     236             : 
     237           2 :         if (argc >= 4) {
     238           2 :             zend_update_property_string(default_exception_ce, object, "file", sizeof("file")-1, filename TSRMLS_CC);
     239           2 :         if (argc < 5) {
     240           0 :             lineno = 0; /* invalidate lineno */
     241             :         }
     242           2 :         zend_update_property_long(default_exception_ce, object, "line", sizeof("line")-1, lineno TSRMLS_CC);
     243             :         }
     244           2 : }
     245             : /* }}} */
     246             : 
     247             : #define DEFAULT_0_PARAMS \
     248             :         if (zend_parse_parameters_none() == FAILURE) { \
     249             :                 return; \
     250             :         }
     251             : 
     252        1209 : static void _default_exception_get_entry(zval *object, char *name, int name_len, zval *return_value TSRMLS_DC) /* {{{ */
     253             : {
     254             :         zval *value;
     255             : 
     256        1209 :         value = zend_read_property(default_exception_ce, object, name, name_len, 0 TSRMLS_CC);
     257             : 
     258        1209 :         *return_value = *value;
     259        1209 :         zval_copy_ctor(return_value);
     260        1209 :         INIT_PZVAL(return_value);
     261        1209 : }
     262             : /* }}} */
     263             : 
     264             : /* {{{ proto string Exception::getFile()
     265             :    Get the file in which the exception occurred */
     266           7 : ZEND_METHOD(exception, getFile)
     267             : {
     268           7 :         DEFAULT_0_PARAMS;
     269             : 
     270           6 :         _default_exception_get_entry(getThis(), "file", sizeof("file")-1, return_value TSRMLS_CC);
     271             : }
     272             : /* }}} */
     273             : 
     274             : /* {{{ proto int Exception::getLine()
     275             :    Get the line in which the exception occurred */
     276          13 : ZEND_METHOD(exception, getLine)
     277             : {
     278          13 :         DEFAULT_0_PARAMS;
     279             : 
     280          12 :         _default_exception_get_entry(getThis(), "line", sizeof("line")-1, return_value TSRMLS_CC);
     281             : }
     282             : /* }}} */
     283             : 
     284             : /* {{{ proto string Exception::getMessage()
     285             :    Get the exception message */
     286         860 : ZEND_METHOD(exception, getMessage)
     287             : {
     288         860 :         DEFAULT_0_PARAMS;
     289             : 
     290         859 :         _default_exception_get_entry(getThis(), "message", sizeof("message")-1, return_value TSRMLS_CC);
     291             : }
     292             : /* }}} */
     293             : 
     294             : /* {{{ proto int Exception::getCode()
     295             :    Get the exception code */
     296          17 : ZEND_METHOD(exception, getCode)
     297             : {
     298          17 :         DEFAULT_0_PARAMS;
     299             : 
     300          16 :         _default_exception_get_entry(getThis(), "code", sizeof("code")-1, return_value TSRMLS_CC);
     301             : }
     302             : /* }}} */
     303             : 
     304             : /* {{{ proto array Exception::getTrace()
     305             :    Get the stack trace for the location in which the exception occurred */
     306           5 : ZEND_METHOD(exception, getTrace)
     307             : {
     308           5 :         DEFAULT_0_PARAMS;
     309             : 
     310           4 :         _default_exception_get_entry(getThis(), "trace", sizeof("trace")-1, return_value TSRMLS_CC);
     311             : }
     312             : /* }}} */
     313             : 
     314             : /* {{{ proto int ErrorException::getSeverity()
     315             :    Get the exception severity */
     316           0 : ZEND_METHOD(error_exception, getSeverity)
     317             : {
     318           0 :         DEFAULT_0_PARAMS;
     319             : 
     320           0 :         _default_exception_get_entry(getThis(), "severity", sizeof("severity")-1, return_value TSRMLS_CC);
     321             : }
     322             : /* }}} */
     323             : 
     324             : /* {{{ gettraceasstring() macros */
     325             : #define TRACE_APPEND_CHR(chr)                                            \
     326             :         *str = (char*)erealloc(*str, *len + 1 + 1);                          \
     327             :         (*str)[(*len)++] = chr
     328             : 
     329             : #define TRACE_APPEND_STRL(val, vallen)                                   \
     330             :         {                                                                    \
     331             :                 int l = vallen;                                                  \
     332             :                 *str = (char*)erealloc(*str, *len + l + 1);                      \
     333             :                 memcpy((*str) + *len, val, l);                                   \
     334             :                 *len += l;                                                       \
     335             :         }
     336             : 
     337             : #define TRACE_APPEND_STR(val)                                            \
     338             :         TRACE_APPEND_STRL(val, sizeof(val)-1)
     339             : 
     340             : #define TRACE_APPEND_KEY(key)                                                   \
     341             :         if (zend_hash_find(ht, key, sizeof(key), (void**)&tmp) == SUCCESS) {    \
     342             :                 if (Z_TYPE_PP(tmp) != IS_STRING) {                              \
     343             :                         zend_error(E_WARNING, "Value for %s is no string", key); \
     344             :                         TRACE_APPEND_STR("[unknown]");                          \
     345             :                 } else {                                                        \
     346             :                         TRACE_APPEND_STRL(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));  \
     347             :                 }                                                               \
     348             :         }
     349             : 
     350             : /* }}} */
     351             : 
     352         184 : static int _build_trace_args(zval **arg TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
     353             : {
     354             :         char **str;
     355             :         int *len;
     356             : 
     357         184 :         str = va_arg(args, char**);
     358         184 :         len = va_arg(args, int*);
     359             : 
     360             :         /* the trivial way would be to do:
     361             :          * conver_to_string_ex(arg);
     362             :          * append it and kill the now tmp arg.
     363             :          * but that could cause some E_NOTICE and also damn long lines.
     364             :          */
     365             : 
     366         184 :         switch (Z_TYPE_PP(arg)) {
     367             :                 case IS_NULL:
     368           0 :                         TRACE_APPEND_STR("NULL, ");
     369           0 :                         break;
     370             :                 case IS_STRING: {
     371             :                         int l_added;
     372         112 :                         TRACE_APPEND_CHR('\'');
     373         112 :                         if (Z_STRLEN_PP(arg) > 15) {
     374          40 :                                 TRACE_APPEND_STRL(Z_STRVAL_PP(arg), 15);
     375          40 :                                 TRACE_APPEND_STR("...', ");
     376          40 :                                 l_added = 15 + 6 + 1; /* +1 because of while (--l_added) */
     377             :                         } else {
     378          72 :                                 l_added = Z_STRLEN_PP(arg);
     379          72 :                                 TRACE_APPEND_STRL(Z_STRVAL_PP(arg), l_added);
     380          72 :                                 TRACE_APPEND_STR("', ");
     381          72 :                                 l_added += 3 + 1;
     382             :                         }
     383        1659 :                         while (--l_added) {
     384        1435 :                                 if ((*str)[*len - l_added] < 32) {
     385           1 :                                         (*str)[*len - l_added] = '?';
     386             :                                 }
     387             :                         }
     388         112 :                         break;
     389             :                 }
     390             :                 case IS_BOOL:
     391           1 :                         if (Z_LVAL_PP(arg)) {
     392           1 :                                 TRACE_APPEND_STR("true, ");
     393             :                         } else {
     394           0 :                                 TRACE_APPEND_STR("false, ");
     395             :                         }
     396           1 :                         break;
     397             :                 case IS_RESOURCE:
     398           1 :                         TRACE_APPEND_STR("Resource id #");
     399             :                         /* break; */
     400             :                 case IS_LONG: {
     401          29 :                         long lval = Z_LVAL_PP(arg);
     402             :                         char s_tmp[MAX_LENGTH_OF_LONG + 1];
     403          29 :                         int l_tmp = zend_sprintf(s_tmp, "%ld", lval);  /* SAFE */
     404          29 :                         TRACE_APPEND_STRL(s_tmp, l_tmp);
     405          29 :                         TRACE_APPEND_STR(", ");
     406          29 :                         break;
     407             :                 }
     408             :                 case IS_DOUBLE: {
     409           0 :                         double dval = Z_DVAL_PP(arg);
     410             :                         char *s_tmp;
     411             :                         int l_tmp;
     412             : 
     413           0 :                         s_tmp = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
     414           0 :                         l_tmp = zend_sprintf(s_tmp, "%.*G", (int) EG(precision), dval);  /* SAFE */
     415           0 :                         TRACE_APPEND_STRL(s_tmp, l_tmp);
     416             :                         /* %G already handles removing trailing zeros from the fractional part, yay */
     417           0 :                         efree(s_tmp);
     418           0 :                         TRACE_APPEND_STR(", ");
     419           0 :                         break;
     420             :                 }
     421             :                 case IS_ARRAY:
     422          32 :                         TRACE_APPEND_STR("Array, ");
     423          32 :                         break;
     424             :                 case IS_OBJECT: {
     425             :                         char *class_name;
     426             :                         zend_uint class_name_len;
     427             :                         int dup;
     428             : 
     429          10 :                         TRACE_APPEND_STR("Object(");
     430             : 
     431          10 :                         dup = zend_get_object_classname(*arg, &class_name, &class_name_len TSRMLS_CC);
     432             : 
     433          10 :                         TRACE_APPEND_STRL(class_name, class_name_len);
     434          10 :                         if(!dup) {
     435          10 :                                 efree(class_name);
     436             :                         }
     437             : 
     438          10 :                         TRACE_APPEND_STR("), ");
     439             :                         break;
     440             :                 }
     441             :                 default:
     442             :                         break;
     443             :         }
     444         184 :         return ZEND_HASH_APPLY_KEEP;
     445             : }
     446             : /* }}} */
     447             : 
     448         136 : static int _build_trace_string(zval **frame TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
     449             : {
     450             :         char *s_tmp, **str;
     451             :         int *len, *num;
     452             :         long line;
     453         136 :         HashTable *ht = Z_ARRVAL_PP(frame);
     454             :         zval **file, **tmp;
     455             : 
     456         136 :         if (Z_TYPE_PP(frame) != IS_ARRAY) {
     457           1 :                 zend_error(E_WARNING, "Expected array for frame %lu", hash_key->h);
     458           1 :                 return ZEND_HASH_APPLY_KEEP;
     459             :         }
     460             : 
     461         135 :         str = va_arg(args, char**);
     462         135 :         len = va_arg(args, int*);
     463         135 :         num = va_arg(args, int*);
     464             : 
     465         135 :         s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 1 + 1);
     466         135 :         sprintf(s_tmp, "#%d ", (*num)++);
     467         135 :         TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     468         135 :         efree(s_tmp);
     469         135 :         if (zend_hash_find(ht, "file", sizeof("file"), (void**)&file) == SUCCESS) {
     470         108 :                 if (Z_TYPE_PP(file) != IS_STRING) {
     471           1 :                         zend_error(E_WARNING, "Function name is no string");
     472           1 :                         TRACE_APPEND_STR("[unknown function]");
     473             :                 } else{
     474         107 :                         if (zend_hash_find(ht, "line", sizeof("line"), (void**)&tmp) == SUCCESS) {
     475         107 :                                 if (Z_TYPE_PP(tmp) == IS_LONG) {
     476         107 :                                         line = Z_LVAL_PP(tmp);
     477             :                                 } else {
     478           0 :                                         zend_error(E_WARNING, "Line is no long");
     479           0 :                                         line = 0;
     480             :                                 }
     481             :                         } else {
     482           0 :                                 line = 0;
     483             :                         }
     484         107 :                         s_tmp = emalloc(Z_STRLEN_PP(file) + MAX_LENGTH_OF_LONG + 4 + 1);
     485         107 :                         sprintf(s_tmp, "%s(%ld): ", Z_STRVAL_PP(file), line);
     486         107 :                         TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     487         107 :                         efree(s_tmp);
     488             :                 }
     489             :         } else {
     490          27 :                 TRACE_APPEND_STR("[internal function]: ");
     491             :         }
     492         135 :         TRACE_APPEND_KEY("class");
     493         135 :         TRACE_APPEND_KEY("type");
     494         135 :         TRACE_APPEND_KEY("function");
     495         135 :         TRACE_APPEND_CHR('(');
     496         135 :         if (zend_hash_find(ht, "args", sizeof("args"), (void**)&tmp) == SUCCESS) {
     497         132 :                 if (Z_TYPE_PP(tmp) == IS_ARRAY) {
     498         131 :                         int last_len = *len;
     499         131 :                         zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp) TSRMLS_CC, (apply_func_args_t)_build_trace_args, 2, str, len);
     500         131 :                         if (last_len != *len) {
     501          87 :                                 *len -= 2; /* remove last ', ' */
     502             :                         }
     503             :                 } else {
     504           1 :                         zend_error(E_WARNING, "args element is no array");
     505             :                 }
     506             :         }
     507         135 :         TRACE_APPEND_STR(")\n");
     508         135 :         return ZEND_HASH_APPLY_KEEP;
     509             : }
     510             : /* }}} */
     511             : 
     512             : /* {{{ proto string Exception::getTraceAsString()
     513             :    Obtain the backtrace for the exception as a string (instead of an array) */
     514         110 : ZEND_METHOD(exception, getTraceAsString)
     515             : {
     516             :         zval *trace;
     517             :         char *res, **str, *s_tmp;
     518         110 :         int res_len = 0, *len = &res_len, num = 0;
     519             : 
     520         110 :         DEFAULT_0_PARAMS;
     521             :         
     522         109 :         res = estrdup("");
     523         109 :         str = &res;
     524             : 
     525         109 :         trace = zend_read_property(default_exception_ce, getThis(), "trace", sizeof("trace")-1, 1 TSRMLS_CC);
     526         109 :         zend_hash_apply_with_arguments(Z_ARRVAL_P(trace) TSRMLS_CC, (apply_func_args_t)_build_trace_string, 3, str, len, &num);
     527             : 
     528         109 :         s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 7 + 1);
     529         109 :         sprintf(s_tmp, "#%d {main}", num);
     530         109 :         TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     531         109 :         efree(s_tmp);
     532             : 
     533         109 :         res[res_len] = '\0';    
     534         109 :         RETURN_STRINGL(res, res_len, 0); 
     535             : }
     536             : /* }}} */
     537             : 
     538             : /* {{{ proto string Exception::getPrevious()
     539             :    Return previous Exception or NULL. */
     540           4 : ZEND_METHOD(exception, getPrevious)
     541             : {
     542             :         zval *previous;
     543             : 
     544           4 :         DEFAULT_0_PARAMS;
     545             : 
     546           4 :         previous = zend_read_property(default_exception_ce, getThis(), "previous", sizeof("previous")-1, 1 TSRMLS_CC);
     547          12 :         RETURN_ZVAL(previous, 1, 0);
     548             : }
     549             : 
     550     1565181 : int zend_spprintf(char **message, int max_len, char *format, ...) /* {{{ */
     551             : {
     552             :         va_list arg;
     553             :         int len;
     554             : 
     555     1565181 :         va_start(arg, format);
     556     1565181 :         len = zend_vspprintf(message, max_len, format, arg);
     557     1565181 :         va_end(arg);
     558     1565181 :         return len;
     559             : }
     560             : /* }}} */
     561             : 
     562             : /* {{{ proto string Exception::__toString()
     563             :    Obtain the string representation of the Exception object */
     564          99 : ZEND_METHOD(exception, __toString)
     565             : {
     566             :         zval message, file, line, *trace, *exception;
     567             :         char *str, *prev_str;
     568          99 :         int len = 0;
     569             :         zend_fcall_info fci;
     570             :         zval fname;
     571             :         
     572          99 :         DEFAULT_0_PARAMS;
     573             :         
     574          98 :         str = estrndup("", 0);
     575             : 
     576          98 :         exception = getThis();
     577          98 :         ZVAL_STRINGL(&fname, "gettraceasstring", sizeof("gettraceasstring")-1, 1);
     578             : 
     579         299 :         while (exception && Z_TYPE_P(exception) == IS_OBJECT) {
     580         104 :                 prev_str = str;
     581         104 :                 _default_exception_get_entry(exception, "message", sizeof("message")-1, &message TSRMLS_CC);
     582         104 :                 _default_exception_get_entry(exception, "file", sizeof("file")-1, &file TSRMLS_CC);
     583         104 :                 _default_exception_get_entry(exception, "line", sizeof("line")-1, &line TSRMLS_CC);
     584             : 
     585         104 :                 convert_to_string(&message);
     586         103 :                 convert_to_string(&file);
     587         103 :                 convert_to_long(&line);
     588             : 
     589         103 :                 fci.size = sizeof(fci);
     590         103 :                 fci.function_table = &Z_OBJCE_P(exception)->function_table;
     591         103 :                 fci.function_name = &fname;
     592         103 :                 fci.symbol_table = NULL;
     593         103 :                 fci.object_ptr = exception;
     594         103 :                 fci.retval_ptr_ptr = &trace;
     595         103 :                 fci.param_count = 0;
     596         103 :                 fci.params = NULL;
     597         103 :                 fci.no_separation = 1;
     598             : 
     599         103 :                 zend_call_function(&fci, NULL TSRMLS_CC);
     600             : 
     601         103 :                 if (Z_TYPE_P(trace) != IS_STRING) {
     602           0 :                         zval_ptr_dtor(&trace);
     603           0 :                         trace = NULL;
     604             :                 }
     605             : 
     606         103 :                 if (Z_STRLEN(message) > 0) {
     607         410 :                         len = zend_spprintf(&str, 0, "exception '%s' with message '%s' in %s:%ld\nStack trace:\n%s%s%s",
     608          82 :                                                                 Z_OBJCE_P(exception)->name, Z_STRVAL(message), Z_STRVAL(file), Z_LVAL(line),
     609         246 :                                                                 (trace && Z_STRLEN_P(trace)) ? Z_STRVAL_P(trace) : "#0 {main}\n",
     610             :                                                                 len ? "\n\nNext " : "", prev_str);
     611             :                 } else {
     612         105 :                         len = zend_spprintf(&str, 0, "exception '%s' in %s:%ld\nStack trace:\n%s%s%s",
     613          21 :                                                                 Z_OBJCE_P(exception)->name, Z_STRVAL(file), Z_LVAL(line),
     614          63 :                                                                 (trace && Z_STRLEN_P(trace)) ? Z_STRVAL_P(trace) : "#0 {main}\n",
     615             :                                                                 len ? "\n\nNext " : "", prev_str);
     616             :                 }
     617         103 :                 efree(prev_str);
     618         103 :                 zval_dtor(&message);
     619         103 :                 zval_dtor(&file);
     620         103 :                 zval_dtor(&line);
     621             : 
     622         103 :                 exception = zend_read_property(default_exception_ce, exception, "previous", sizeof("previous")-1, 0 TSRMLS_CC);
     623             : 
     624         103 :                 if (trace) {
     625         103 :                         zval_ptr_dtor(&trace);
     626             :                 }
     627             :         }
     628          97 :         zval_dtor(&fname);
     629             : 
     630             :         /* We store the result in the private property string so we can access
     631             :          * the result in uncaught exception handlers without memleaks. */
     632          97 :         zend_update_property_string(default_exception_ce, getThis(), "string", sizeof("string")-1, str TSRMLS_CC);
     633             : 
     634          97 :         RETURN_STRINGL(str, len, 0);
     635             : }
     636             : /* }}} */
     637             : 
     638             : /* {{{ internal structs */
     639             : /* All functions that may be used in uncaught exception handlers must be final
     640             :  * and must not throw exceptions. Otherwise we would need a facility to handle
     641             :  * such exceptions in that handler.
     642             :  * Also all getXY() methods are final because thy serve as read only access to
     643             :  * their corresponding properties, no more, no less. If after all you need to
     644             :  * override somthing then it is method __toString().
     645             :  * And never try to change the state of exceptions and never implement anything
     646             :  * that gives the user anything to accomplish this.
     647             :  */
     648             : ZEND_BEGIN_ARG_INFO_EX(arginfo_exception___construct, 0, 0, 0)
     649             :         ZEND_ARG_INFO(0, message)
     650             :         ZEND_ARG_INFO(0, code)
     651             :         ZEND_ARG_INFO(0, previous)
     652             : ZEND_END_ARG_INFO()
     653             : 
     654             : const static zend_function_entry default_exception_functions[] = {
     655             :         ZEND_ME(exception, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
     656             :         ZEND_ME(exception, __construct, arginfo_exception___construct, ZEND_ACC_PUBLIC)
     657             :         ZEND_ME(exception, getMessage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
     658             :         ZEND_ME(exception, getCode, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
     659             :         ZEND_ME(exception, getFile, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
     660             :         ZEND_ME(exception, getLine, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
     661             :         ZEND_ME(exception, getTrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
     662             :         ZEND_ME(exception, getPrevious, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
     663             :         ZEND_ME(exception, getTraceAsString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
     664             :         ZEND_ME(exception, __toString, NULL, 0)
     665             :         {NULL, NULL, NULL}
     666             : };
     667             : 
     668             : ZEND_BEGIN_ARG_INFO_EX(arginfo_error_exception___construct, 0, 0, 0)
     669             :         ZEND_ARG_INFO(0, message)
     670             :         ZEND_ARG_INFO(0, code)
     671             :         ZEND_ARG_INFO(0, severity)
     672             :         ZEND_ARG_INFO(0, filename)
     673             :         ZEND_ARG_INFO(0, lineno)
     674             :         ZEND_ARG_INFO(0, previous)
     675             : ZEND_END_ARG_INFO()
     676             : 
     677             : static const zend_function_entry error_exception_functions[] = {
     678             :         ZEND_ME(error_exception, __construct, arginfo_error_exception___construct, ZEND_ACC_PUBLIC)
     679             :         ZEND_ME(error_exception, getSeverity, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
     680             :         {NULL, NULL, NULL}
     681             : };
     682             : /* }}} */
     683             : 
     684       19341 : void zend_register_default_exception(TSRMLS_D) /* {{{ */
     685             : {
     686             :         zend_class_entry ce;
     687             : 
     688       19341 :         INIT_CLASS_ENTRY(ce, "Exception", default_exception_functions);
     689       19341 :         default_exception_ce = zend_register_internal_class(&ce TSRMLS_CC);
     690       19341 :         default_exception_ce->create_object = zend_default_exception_new;
     691       19341 :         memcpy(&default_exception_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
     692       19341 :         default_exception_handlers.clone_obj = NULL;
     693             : 
     694       19341 :         zend_declare_property_string(default_exception_ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
     695       19341 :         zend_declare_property_string(default_exception_ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
     696       19341 :         zend_declare_property_long(default_exception_ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC);
     697       19341 :         zend_declare_property_null(default_exception_ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
     698       19341 :         zend_declare_property_null(default_exception_ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
     699       19341 :         zend_declare_property_null(default_exception_ce, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
     700       19341 :         zend_declare_property_null(default_exception_ce, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
     701             : 
     702       19341 :         INIT_CLASS_ENTRY(ce, "ErrorException", error_exception_functions);
     703       19341 :         error_exception_ce = zend_register_internal_class_ex(&ce, default_exception_ce, NULL TSRMLS_CC);
     704       19341 :         error_exception_ce->create_object = zend_error_exception_new;
     705       19341 :         zend_declare_property_long(error_exception_ce, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED TSRMLS_CC);
     706       19341 : }
     707             : /* }}} */
     708             : 
     709      116516 : ZEND_API zend_class_entry *zend_exception_get_default(TSRMLS_D) /* {{{ */
     710             : {
     711      116516 :         return default_exception_ce;
     712             : }
     713             : /* }}} */
     714             : 
     715           0 : ZEND_API zend_class_entry *zend_get_error_exception(TSRMLS_D) /* {{{ */
     716             : {
     717           0 :         return error_exception_ce;
     718             : }
     719             : /* }}} */
     720             : 
     721         846 : ZEND_API zval * zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC) /* {{{ */
     722             : {
     723             :         zval *ex;
     724             : 
     725         846 :         MAKE_STD_ZVAL(ex);
     726         846 :         if (exception_ce) {
     727         735 :                 if (!instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
     728           0 :                         zend_error(E_NOTICE, "Exceptions must be derived from the Exception base class");
     729           0 :                         exception_ce = default_exception_ce;
     730             :                 }
     731             :         } else {
     732         111 :                 exception_ce = default_exception_ce;
     733             :         }
     734         846 :         object_init_ex(ex, exception_ce);
     735             : 
     736             : 
     737         846 :         if (message) {
     738         846 :                 zend_update_property_string(default_exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
     739             :         }
     740         846 :         if (code) {
     741          75 :                 zend_update_property_long(default_exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
     742             :         }
     743             : 
     744         846 :         zend_throw_exception_internal(ex TSRMLS_CC);
     745         846 :         return ex;
     746             : }
     747             : /* }}} */
     748             : 
     749         568 : ZEND_API zval * zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...) /* {{{ */
     750             : {
     751             :         va_list arg;
     752             :         char *message;
     753             :         zval *zexception;
     754             : 
     755         568 :         va_start(arg, format);
     756         568 :         zend_vspprintf(&message, 0, format, arg);
     757         568 :         va_end(arg);
     758         568 :         zexception = zend_throw_exception(exception_ce, message, code TSRMLS_CC);
     759         568 :         efree(message);
     760         568 :         return zexception;
     761             : }
     762             : /* }}} */
     763             : 
     764         135 : ZEND_API zval * zend_throw_error_exception(zend_class_entry *exception_ce, char *message, long code, int severity TSRMLS_DC) /* {{{ */
     765             : {
     766         135 :         zval *ex = zend_throw_exception(exception_ce, message, code TSRMLS_CC);
     767         135 :         zend_update_property_long(default_exception_ce, ex, "severity", sizeof("severity")-1, severity TSRMLS_CC);
     768         135 :         return ex;
     769             : }
     770             : /* }}} */
     771             : 
     772          86 : static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...) /* {{{ */
     773             : {
     774             :         va_list args;
     775             : 
     776          86 :         va_start(args, format);
     777          86 :         zend_error_cb(type, file, lineno, format, args);
     778           3 :         va_end(args);
     779           3 : }
     780             : /* }}} */
     781             : 
     782             : /* This function doesn't return if it uses E_ERROR */
     783          87 : ZEND_API void zend_exception_error(zval *exception, int severity TSRMLS_DC) /* {{{ */
     784             : {
     785          87 :         zend_class_entry *ce_exception = Z_OBJCE_P(exception);
     786          87 :         if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
     787             :                 zval *str, *file, *line;
     788             : 
     789          87 :                 EG(exception) = NULL;
     790             : 
     791          87 :                 zend_call_method_with_0_params(&exception, ce_exception, NULL, "__tostring", &str);
     792          86 :                 if (!EG(exception)) {
     793          86 :                         if (Z_TYPE_P(str) != IS_STRING) {
     794           0 :                                 zend_error(E_WARNING, "%s::__toString() must return a string", ce_exception->name);
     795             :                         } else {
     796          86 :                                 zend_update_property_string(default_exception_ce, exception, "string", sizeof("string")-1, EG(exception) ? ce_exception->name : Z_STRVAL_P(str) TSRMLS_CC);
     797             :                         }
     798             :                 }
     799          86 :                 zval_ptr_dtor(&str);
     800             : 
     801          86 :                 if (EG(exception)) {
     802             :                         /* do the best we can to inform about the inner exception */
     803           0 :                         if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
     804           0 :                                 file = zend_read_property(default_exception_ce, EG(exception), "file", sizeof("file")-1, 1 TSRMLS_CC);
     805           0 :                                 line = zend_read_property(default_exception_ce, EG(exception), "line", sizeof("line")-1, 1 TSRMLS_CC);
     806             : 
     807           0 :                                 convert_to_string(file);
     808           0 :                                 file = (Z_STRLEN_P(file) > 0) ? file : NULL;
     809           0 :                                 line = (Z_TYPE_P(line) == IS_LONG) ? line : NULL;
     810             :                         } else {
     811           0 :                                 file = NULL;
     812           0 :                                 line = NULL;
     813             :                         }
     814           0 :                         zend_error_va(E_WARNING, file ? Z_STRVAL_P(file) : NULL, line ? Z_LVAL_P(line) : 0, "Uncaught %s in exception handling during call to %s::__tostring()", Z_OBJCE_P(EG(exception))->name, ce_exception->name);
     815             :                 }
     816             : 
     817          86 :                 str = zend_read_property(default_exception_ce, exception, "string", sizeof("string")-1, 1 TSRMLS_CC);
     818          86 :                 file = zend_read_property(default_exception_ce, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
     819          86 :                 line = zend_read_property(default_exception_ce, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
     820             : 
     821          86 :                 convert_to_string(str);
     822          86 :                 convert_to_string(file);
     823          86 :                 convert_to_long(line);
     824             : 
     825          86 :                 zend_error_va(severity, (Z_STRLEN_P(file) > 0) ? Z_STRVAL_P(file) : NULL, Z_LVAL_P(line), "Uncaught %s\n  thrown", Z_STRVAL_P(str));
     826             :         } else {
     827           0 :                 zend_error(severity, "Uncaught exception '%s'", ce_exception->name);
     828             :         }
     829           3 : }
     830             : /* }}} */
     831             : 
     832         292 : ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC) /* {{{ */
     833             : {
     834             :         zend_class_entry *exception_ce;
     835             : 
     836         292 :         if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
     837           0 :                 zend_error(E_ERROR, "Need to supply an object when throwing an exception");
     838             :         }
     839             : 
     840         292 :         exception_ce = Z_OBJCE_P(exception);
     841             : 
     842         292 :         if (!exception_ce || !instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
     843           1 :                 zend_error(E_ERROR, "Exceptions must be valid objects derived from the Exception base class");
     844             :         }
     845         291 :         zend_throw_exception_internal(exception TSRMLS_CC);
     846         291 : }
     847             : /* }}} */
     848             : 
     849             : /*
     850             :  * Local variables:
     851             :  * tab-width: 4
     852             :  * c-basic-offset: 4
     853             :  * indent-tabs-mode: t
     854             :  * End:
     855             :  */

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:22 +0000 (5 days ago)

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