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: 303 359 84.4 %
Date: 2015-02-21 Functions: 32 35 91.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 21 Feb 2015 16:01:45 +0000 (6 days ago)

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