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: 325 397 81.9 %
Date: 2014-12-20 Functions: 30 33 90.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 21 Dec 2014 04:58:47 +0000 (21 hours ago)

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