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: 0 397 0.0 %
Date: 2014-04-16 Functions: 0 33 0.0 %
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           0 : void zend_exception_set_previous(zval *exception, zval *add_previous TSRMLS_DC)
      38             : {
      39             :         zval *previous;
      40             : 
      41           0 :         if (exception == add_previous || !add_previous || !exception) {
      42           0 :                 return;
      43             :         }
      44           0 :         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           0 :         while (exception && exception != add_previous && Z_OBJ_HANDLE_P(exception) != Z_OBJ_HANDLE_P(add_previous)) {
      49           0 :                 previous = zend_read_property(default_exception_ce, exception, "previous", sizeof("previous")-1, 1 TSRMLS_CC);
      50           0 :                 if (Z_TYPE_P(previous) == IS_NULL) {
      51           0 :                         zend_update_property(default_exception_ce, exception, "previous", sizeof("previous")-1, add_previous TSRMLS_CC);
      52             :                         Z_DELREF_P(add_previous);
      53           0 :                         return;
      54             :                 }
      55           0 :                 exception = previous;
      56             :         }
      57             : }
      58             : 
      59           0 : void zend_exception_save(TSRMLS_D) /* {{{ */
      60             : {
      61           0 :         if (EG(prev_exception)) {
      62           0 :                 zend_exception_set_previous(EG(exception), EG(prev_exception) TSRMLS_CC);
      63             :         }
      64           0 :         if (EG(exception)) {
      65           0 :                 EG(prev_exception) = EG(exception);
      66             :         }
      67           0 :         EG(exception) = NULL;
      68           0 : }
      69             : /* }}} */
      70             : 
      71           0 : void zend_exception_restore(TSRMLS_D) /* {{{ */
      72             : {
      73           0 :         if (EG(prev_exception)) {
      74           0 :                 if (EG(exception)) {
      75           0 :                         zend_exception_set_previous(EG(exception), EG(prev_exception) TSRMLS_CC);
      76             :                 } else {
      77           0 :                         EG(exception) = EG(prev_exception);
      78             :                 }
      79           0 :                 EG(prev_exception) = NULL;
      80             :         }
      81           0 : }
      82             : /* }}} */
      83             : 
      84           0 : 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           0 :         if (exception != NULL) {
     101           0 :                 zval *previous = EG(exception);
     102           0 :                 zend_exception_set_previous(exception, EG(exception) TSRMLS_CC);
     103           0 :                 EG(exception) = exception;
     104           0 :                 if (previous) {
     105           0 :                         return;
     106             :                 }
     107             :         }
     108           0 :         if (!EG(current_execute_data)) {
     109           0 :                 if(EG(exception)) {
     110           0 :                         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           0 :         if (zend_throw_exception_hook) {
     116           0 :                 zend_throw_exception_hook(exception TSRMLS_CC);
     117             :         }
     118             : 
     119           0 :         if (EG(current_execute_data)->opline == NULL ||
     120           0 :             (EG(current_execute_data)->opline+1)->opcode == ZEND_HANDLE_EXCEPTION) {
     121             :                 /* no need to rethrow the exception */
     122           0 :                 return;
     123             :         }
     124           0 :         EG(opline_before_exception) = EG(current_execute_data)->opline;
     125           0 :         EG(current_execute_data)->opline = EG(exception_op);
     126             : }
     127             : /* }}} */
     128             : 
     129           0 : ZEND_API void zend_clear_exception(TSRMLS_D) /* {{{ */
     130             : {
     131           0 :         if (EG(prev_exception)) {
     132           0 :                 zval_ptr_dtor(&EG(prev_exception));
     133           0 :                 EG(prev_exception) = NULL;
     134             :         }
     135           0 :         if (!EG(exception)) {
     136           0 :                 return;
     137             :         }
     138           0 :         zval_ptr_dtor(&EG(exception));
     139           0 :         EG(exception) = NULL;
     140           0 :         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           0 : 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           0 :         Z_OBJVAL(obj) = zend_objects_new(&object, class_type TSRMLS_CC);
     154           0 :         Z_OBJ_HT(obj) = &default_exception_handlers;
     155             : 
     156           0 :         object_properties_init(object, class_type);
     157             : 
     158           0 :         ALLOC_ZVAL(trace);
     159             :         Z_UNSET_ISREF_P(trace);
     160             :         Z_SET_REFCOUNT_P(trace, 0);
     161           0 :         zend_fetch_debug_backtrace(trace, skip_top_traces, 0, 0 TSRMLS_CC);
     162             : 
     163           0 :         zend_update_property_string(default_exception_ce, &obj, "file", sizeof("file")-1, zend_get_executed_filename(TSRMLS_C) TSRMLS_CC);
     164           0 :         zend_update_property_long(default_exception_ce, &obj, "line", sizeof("line")-1, zend_get_executed_lineno(TSRMLS_C) TSRMLS_CC);
     165           0 :         zend_update_property(default_exception_ce, &obj, "trace", sizeof("trace")-1, trace TSRMLS_CC);
     166             : 
     167           0 :         return Z_OBJVAL(obj);
     168             : }
     169             : /* }}} */
     170             : 
     171           0 : static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
     172             : {
     173           0 :         return zend_default_exception_new_ex(class_type, 0 TSRMLS_CC);
     174             : }
     175             : /* }}} */
     176             : 
     177           0 : static zend_object_value zend_error_exception_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
     178             : {
     179           0 :         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           0 : ZEND_METHOD(exception, __construct)
     195             : {
     196           0 :         char  *message = NULL;
     197           0 :         long   code = 0;
     198           0 :         zval  *object, *previous = NULL;
     199           0 :         int    argc = ZEND_NUM_ARGS(), message_len;
     200             : 
     201           0 :         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           0 :         object = getThis();
     206             : 
     207           0 :         if (message) {
     208           0 :                 zend_update_property_stringl(default_exception_ce, object, "message", sizeof("message")-1, message, message_len TSRMLS_CC);
     209             :         }
     210             : 
     211           0 :         if (code) {
     212           0 :                 zend_update_property_long(default_exception_ce, object, "code", sizeof("code")-1, code TSRMLS_CC);
     213             :         }
     214             : 
     215           0 :         if (previous) {
     216           0 :                 zend_update_property(default_exception_ce, object, "previous", sizeof("previous")-1, previous TSRMLS_CC);
     217             :         }
     218           0 : }
     219             : /* }}} */
     220             : 
     221             : /* {{{ proto ErrorException::__construct(string message, int code, int severity [, string filename [, int lineno [, Exception previous]]])
     222             :    ErrorException constructor */
     223           0 : ZEND_METHOD(error_exception, __construct)
     224             : {
     225           0 :         char  *message = NULL, *filename = NULL;
     226           0 :         long   code = 0, severity = E_ERROR, lineno;
     227           0 :         zval  *object, *previous = NULL;
     228           0 :         int    argc = ZEND_NUM_ARGS(), message_len, filename_len;
     229             : 
     230           0 :         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           0 :         object = getThis();
     235             : 
     236           0 :         if (message) {
     237           0 :                 zend_update_property_string(default_exception_ce, object, "message", sizeof("message")-1, message TSRMLS_CC);
     238             :         }
     239             : 
     240           0 :         if (code) {
     241           0 :                 zend_update_property_long(default_exception_ce, object, "code", sizeof("code")-1, code TSRMLS_CC);
     242             :         }
     243             : 
     244           0 :         if (previous) {
     245           0 :                 zend_update_property(default_exception_ce, object, "previous", sizeof("previous")-1, previous TSRMLS_CC);
     246             :         }
     247             : 
     248           0 :         zend_update_property_long(default_exception_ce, object, "severity", sizeof("severity")-1, severity TSRMLS_CC);
     249             : 
     250           0 :         if (argc >= 4) {
     251           0 :             zend_update_property_string(default_exception_ce, object, "file", sizeof("file")-1, filename TSRMLS_CC);
     252           0 :         if (argc < 5) {
     253           0 :             lineno = 0; /* invalidate lineno */
     254             :         }
     255           0 :         zend_update_property_long(default_exception_ce, object, "line", sizeof("line")-1, lineno TSRMLS_CC);
     256             :         }
     257           0 : }
     258             : /* }}} */
     259             : 
     260             : #define DEFAULT_0_PARAMS \
     261             :         if (zend_parse_parameters_none() == FAILURE) { \
     262             :                 return; \
     263             :         }
     264             : 
     265           0 : static void _default_exception_get_entry(zval *object, char *name, int name_len, zval *return_value TSRMLS_DC) /* {{{ */
     266             : {
     267             :         zval *value;
     268             : 
     269           0 :         value = zend_read_property(default_exception_ce, object, name, name_len, 0 TSRMLS_CC);
     270             : 
     271           0 :         *return_value = *value;
     272             :         zval_copy_ctor(return_value);
     273           0 :         INIT_PZVAL(return_value);
     274           0 : }
     275             : /* }}} */
     276             : 
     277             : /* {{{ proto string Exception::getFile()
     278             :    Get the file in which the exception occurred */
     279           0 : ZEND_METHOD(exception, getFile)
     280             : {
     281           0 :         DEFAULT_0_PARAMS;
     282             : 
     283           0 :         _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           0 : ZEND_METHOD(exception, getLine)
     290             : {
     291           0 :         DEFAULT_0_PARAMS;
     292             : 
     293           0 :         _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           0 : ZEND_METHOD(exception, getMessage)
     300             : {
     301           0 :         DEFAULT_0_PARAMS;
     302             : 
     303           0 :         _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           0 : ZEND_METHOD(exception, getCode)
     310             : {
     311           0 :         DEFAULT_0_PARAMS;
     312             : 
     313           0 :         _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           0 : ZEND_METHOD(exception, getTrace)
     320             : {
     321           0 :         DEFAULT_0_PARAMS;
     322             : 
     323           0 :         _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           0 : 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           0 :         str = va_arg(args, char**);
     376           0 :         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           0 :         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           0 :                         TRACE_APPEND_CHR('\'');
     391           0 :                         if (Z_STRLEN_PP(arg) > 15) {
     392           0 :                                 TRACE_APPEND_STRL(Z_STRVAL_PP(arg), 15);
     393           0 :                                 TRACE_APPEND_STR("...', ");
     394           0 :                                 l_added = 15 + 6 + 1; /* +1 because of while (--l_added) */
     395             :                         } else {
     396           0 :                                 l_added = Z_STRLEN_PP(arg);
     397           0 :                                 TRACE_APPEND_STRL(Z_STRVAL_PP(arg), l_added);
     398           0 :                                 TRACE_APPEND_STR("', ");
     399           0 :                                 l_added += 3 + 1;
     400             :                         }
     401           0 :                         while (--l_added) {
     402           0 :                                 unsigned char chr = (*str)[*len - l_added];
     403           0 :                                 if (chr < 32 || chr == '\\' || chr > 126) {
     404           0 :                                         (*str)[*len - l_added] = '\\';
     405             : 
     406           0 :                                         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           0 :                                                         TRACE_ARG_APPEND(3);
     441           0 :                                                         (*str)[*len - l_added + 1] = 'x';
     442           0 :                                                         if ((chr >> 4) < 10) {
     443           0 :                                                                 (*str)[*len - l_added + 2] = (chr >> 4) + '0';
     444             :                                                         } else {
     445           0 :                                                                 (*str)[*len - l_added + 2] = (chr >> 4) + 'A' - 10;
     446             :                                                         }
     447           0 :                                                         if (chr % 16 < 10) {
     448           0 :                                                                 (*str)[*len - l_added + 3] = chr % 16 + '0';
     449             :                                                         } else {
     450           0 :                                                                 (*str)[*len - l_added + 3] = chr % 16 + 'A' - 10;
     451             :                                                         }
     452           0 :                                                         *len += 3;
     453             :                                         }
     454             :                                 }
     455             :                         }
     456           0 :                         break;
     457             :                 }
     458             :                 case IS_BOOL:
     459           0 :                         if (Z_LVAL_PP(arg)) {
     460           0 :                                 TRACE_APPEND_STR("true, ");
     461             :                         } else {
     462           0 :                                 TRACE_APPEND_STR("false, ");
     463             :                         }
     464           0 :                         break;
     465             :                 case IS_RESOURCE:
     466           0 :                         TRACE_APPEND_STR("Resource id #");
     467             :                         /* break; */
     468             :                 case IS_LONG: {
     469           0 :                         long lval = Z_LVAL_PP(arg);
     470             :                         char s_tmp[MAX_LENGTH_OF_LONG + 1];
     471           0 :                         int l_tmp = zend_sprintf(s_tmp, "%ld", lval);  /* SAFE */
     472           0 :                         TRACE_APPEND_STRL(s_tmp, l_tmp);
     473           0 :                         TRACE_APPEND_STR(", ");
     474           0 :                         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           0 :                         TRACE_APPEND_STR("Array, ");
     491           0 :                         break;
     492             :                 case IS_OBJECT: {
     493             :                         const char *class_name;
     494             :                         zend_uint class_name_len;
     495             :                         int dup;
     496             : 
     497           0 :                         TRACE_APPEND_STR("Object(");
     498             : 
     499           0 :                         dup = zend_get_object_classname(*arg, &class_name, &class_name_len TSRMLS_CC);
     500             : 
     501           0 :                         TRACE_APPEND_STRL(class_name, class_name_len);
     502           0 :                         if(!dup) {
     503           0 :                                 efree((char*)class_name);
     504             :                         }
     505             : 
     506           0 :                         TRACE_APPEND_STR("), ");
     507             :                         break;
     508             :                 }
     509             :                 default:
     510             :                         break;
     511             :         }
     512           0 :         return ZEND_HASH_APPLY_KEEP;
     513             : }
     514             : /* }}} */
     515             : 
     516           0 : 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           0 :         HashTable *ht = Z_ARRVAL_PP(frame);
     522             :         zval **file, **tmp;
     523             : 
     524           0 :         if (Z_TYPE_PP(frame) != IS_ARRAY) {
     525           0 :                 zend_error(E_WARNING, "Expected array for frame %lu", hash_key->h);
     526           0 :                 return ZEND_HASH_APPLY_KEEP;
     527             :         }
     528             : 
     529           0 :         str = va_arg(args, char**);
     530           0 :         len = va_arg(args, int*);
     531           0 :         num = va_arg(args, int*);
     532             : 
     533           0 :         s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 1 + 1);
     534           0 :         sprintf(s_tmp, "#%d ", (*num)++);
     535           0 :         TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     536           0 :         efree(s_tmp);
     537           0 :         if (zend_hash_find(ht, "file", sizeof("file"), (void**)&file) == SUCCESS) {
     538           0 :                 if (Z_TYPE_PP(file) != IS_STRING) {
     539           0 :                         zend_error(E_WARNING, "Function name is no string");
     540           0 :                         TRACE_APPEND_STR("[unknown function]");
     541             :                 } else{
     542           0 :                         if (zend_hash_find(ht, "line", sizeof("line"), (void**)&tmp) == SUCCESS) {
     543           0 :                                 if (Z_TYPE_PP(tmp) == IS_LONG) {
     544           0 :                                         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           0 :                         s_tmp = emalloc(Z_STRLEN_PP(file) + MAX_LENGTH_OF_LONG + 4 + 1);
     553           0 :                         sprintf(s_tmp, "%s(%ld): ", Z_STRVAL_PP(file), line);
     554           0 :                         TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     555           0 :                         efree(s_tmp);
     556             :                 }
     557             :         } else {
     558           0 :                 TRACE_APPEND_STR("[internal function]: ");
     559             :         }
     560           0 :         TRACE_APPEND_KEY("class");
     561           0 :         TRACE_APPEND_KEY("type");
     562           0 :         TRACE_APPEND_KEY("function");
     563           0 :         TRACE_APPEND_CHR('(');
     564           0 :         if (zend_hash_find(ht, "args", sizeof("args"), (void**)&tmp) == SUCCESS) {
     565           0 :                 if (Z_TYPE_PP(tmp) == IS_ARRAY) {
     566           0 :                         int last_len = *len;
     567           0 :                         zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp) TSRMLS_CC, (apply_func_args_t)_build_trace_args, 2, str, len);
     568           0 :                         if (last_len != *len) {
     569           0 :                                 *len -= 2; /* remove last ', ' */
     570             :                         }
     571             :                 } else {
     572           0 :                         zend_error(E_WARNING, "args element is no array");
     573             :                 }
     574             :         }
     575           0 :         TRACE_APPEND_STR(")\n");
     576           0 :         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           0 : ZEND_METHOD(exception, getTraceAsString)
     583             : {
     584             :         zval *trace;
     585             :         char *res, **str, *s_tmp;
     586           0 :         int res_len = 0, *len = &res_len, num = 0;
     587             : 
     588           0 :         DEFAULT_0_PARAMS;
     589             :         
     590           0 :         res = estrdup("");
     591           0 :         str = &res;
     592             : 
     593           0 :         trace = zend_read_property(default_exception_ce, getThis(), "trace", sizeof("trace")-1, 1 TSRMLS_CC);
     594           0 :         zend_hash_apply_with_arguments(Z_ARRVAL_P(trace) TSRMLS_CC, (apply_func_args_t)_build_trace_string, 3, str, len, &num);
     595             : 
     596           0 :         s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 7 + 1);
     597           0 :         sprintf(s_tmp, "#%d {main}", num);
     598           0 :         TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
     599           0 :         efree(s_tmp);
     600             : 
     601           0 :         res[res_len] = '\0';    
     602           0 :         RETURN_STRINGL(res, res_len, 0); 
     603             : }
     604             : /* }}} */
     605             : 
     606             : /* {{{ proto string Exception::getPrevious()
     607             :    Return previous Exception or NULL. */
     608           0 : ZEND_METHOD(exception, getPrevious)
     609             : {
     610             :         zval *previous;
     611             : 
     612           0 :         DEFAULT_0_PARAMS;
     613             : 
     614           0 :         previous = zend_read_property(default_exception_ce, getThis(), "previous", sizeof("previous")-1, 1 TSRMLS_CC);
     615           0 :         RETURN_ZVAL(previous, 1, 0);
     616             : }
     617             : 
     618           0 : int zend_spprintf(char **message, int max_len, const char *format, ...) /* {{{ */
     619             : {
     620             :         va_list arg;
     621             :         int len;
     622             : 
     623           0 :         va_start(arg, format);
     624           0 :         len = zend_vspprintf(message, max_len, format, arg);
     625           0 :         va_end(arg);
     626           0 :         return len;
     627             : }
     628             : /* }}} */
     629             : 
     630             : /* {{{ proto string Exception::__toString()
     631             :    Obtain the string representation of the Exception object */
     632           0 : ZEND_METHOD(exception, __toString)
     633             : {
     634             :         zval message, file, line, *trace, *exception;
     635             :         char *str, *prev_str;
     636           0 :         int len = 0;
     637             :         zend_fcall_info fci;
     638             :         zval fname;
     639             :         
     640           0 :         DEFAULT_0_PARAMS;
     641             :         
     642           0 :         str = estrndup("", 0);
     643             : 
     644           0 :         exception = getThis();
     645           0 :         ZVAL_STRINGL(&fname, "gettraceasstring", sizeof("gettraceasstring")-1, 1);
     646             : 
     647           0 :         while (exception && Z_TYPE_P(exception) == IS_OBJECT) {
     648           0 :                 prev_str = str;
     649           0 :                 _default_exception_get_entry(exception, "message", sizeof("message")-1, &message TSRMLS_CC);
     650           0 :                 _default_exception_get_entry(exception, "file", sizeof("file")-1, &file TSRMLS_CC);
     651           0 :                 _default_exception_get_entry(exception, "line", sizeof("line")-1, &line TSRMLS_CC);
     652             : 
     653           0 :                 convert_to_string(&message);
     654           0 :                 convert_to_string(&file);
     655           0 :                 convert_to_long(&line);
     656             : 
     657           0 :                 fci.size = sizeof(fci);
     658           0 :                 fci.function_table = &Z_OBJCE_P(exception)->function_table;
     659           0 :                 fci.function_name = &fname;
     660           0 :                 fci.symbol_table = NULL;
     661           0 :                 fci.object_ptr = exception;
     662           0 :                 fci.retval_ptr_ptr = &trace;
     663           0 :                 fci.param_count = 0;
     664           0 :                 fci.params = NULL;
     665           0 :                 fci.no_separation = 1;
     666             : 
     667           0 :                 zend_call_function(&fci, NULL TSRMLS_CC);
     668             : 
     669           0 :                 if (Z_TYPE_P(trace) != IS_STRING) {
     670           0 :                         zval_ptr_dtor(&trace);
     671           0 :                         trace = NULL;
     672             :                 }
     673             : 
     674           0 :                 if (Z_STRLEN(message) > 0) {
     675           0 :                         len = zend_spprintf(&str, 0, "exception '%s' with message '%s' in %s:%ld\nStack trace:\n%s%s%s",
     676           0 :                                                                 Z_OBJCE_P(exception)->name, Z_STRVAL(message), Z_STRVAL(file), Z_LVAL(line),
     677           0 :                                                                 (trace && Z_STRLEN_P(trace)) ? Z_STRVAL_P(trace) : "#0 {main}\n",
     678             :                                                                 len ? "\n\nNext " : "", prev_str);
     679             :                 } else {
     680           0 :                         len = zend_spprintf(&str, 0, "exception '%s' in %s:%ld\nStack trace:\n%s%s%s",
     681           0 :                                                                 Z_OBJCE_P(exception)->name, Z_STRVAL(file), Z_LVAL(line),
     682           0 :                                                                 (trace && Z_STRLEN_P(trace)) ? Z_STRVAL_P(trace) : "#0 {main}\n",
     683             :                                                                 len ? "\n\nNext " : "", prev_str);
     684             :                 }
     685           0 :                 efree(prev_str);
     686             :                 zval_dtor(&message);
     687             :                 zval_dtor(&file);
     688             :                 zval_dtor(&line);
     689             : 
     690           0 :                 exception = zend_read_property(default_exception_ce, exception, "previous", sizeof("previous")-1, 0 TSRMLS_CC);
     691             : 
     692           0 :                 if (trace) {
     693           0 :                         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           0 :         zend_update_property_string(default_exception_ce, getThis(), "string", sizeof("string")-1, str TSRMLS_CC);
     702             : 
     703           0 :         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           0 : void zend_register_default_exception(TSRMLS_D) /* {{{ */
     754             : {
     755             :         zend_class_entry ce;
     756             : 
     757           0 :         INIT_CLASS_ENTRY(ce, "Exception", default_exception_functions);
     758           0 :         default_exception_ce = zend_register_internal_class(&ce TSRMLS_CC);
     759           0 :         default_exception_ce->create_object = zend_default_exception_new;
     760           0 :         memcpy(&default_exception_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
     761           0 :         default_exception_handlers.clone_obj = NULL;
     762             : 
     763           0 :         zend_declare_property_string(default_exception_ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
     764           0 :         zend_declare_property_string(default_exception_ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
     765           0 :         zend_declare_property_long(default_exception_ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC);
     766           0 :         zend_declare_property_null(default_exception_ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
     767           0 :         zend_declare_property_null(default_exception_ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
     768           0 :         zend_declare_property_null(default_exception_ce, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
     769           0 :         zend_declare_property_null(default_exception_ce, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
     770             : 
     771           0 :         INIT_CLASS_ENTRY(ce, "ErrorException", error_exception_functions);
     772           0 :         error_exception_ce = zend_register_internal_class_ex(&ce, default_exception_ce, NULL TSRMLS_CC);
     773           0 :         error_exception_ce->create_object = zend_error_exception_new;
     774           0 :         zend_declare_property_long(error_exception_ce, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED TSRMLS_CC);
     775           0 : }
     776             : /* }}} */
     777             : 
     778           0 : ZEND_API zend_class_entry *zend_exception_get_default(TSRMLS_D) /* {{{ */
     779             : {
     780           0 :         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           0 : ZEND_API zval * zend_throw_exception(zend_class_entry *exception_ce, const char *message, long code TSRMLS_DC) /* {{{ */
     791             : {
     792             :         zval *ex;
     793             : 
     794           0 :         MAKE_STD_ZVAL(ex);
     795           0 :         if (exception_ce) {
     796           0 :                 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           0 :                 exception_ce = default_exception_ce;
     802             :         }
     803           0 :         object_init_ex(ex, exception_ce);
     804             : 
     805             : 
     806           0 :         if (message) {
     807           0 :                 zend_update_property_string(default_exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
     808             :         }
     809           0 :         if (code) {
     810           0 :                 zend_update_property_long(default_exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
     811             :         }
     812             : 
     813           0 :         zend_throw_exception_internal(ex TSRMLS_CC);
     814           0 :         return ex;
     815             : }
     816             : /* }}} */
     817             : 
     818           0 : ZEND_API zval * zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, const char *format, ...) /* {{{ */
     819             : {
     820             :         va_list arg;
     821             :         char *message;
     822             :         zval *zexception;
     823             : 
     824           0 :         va_start(arg, format);
     825           0 :         zend_vspprintf(&message, 0, format, arg);
     826           0 :         va_end(arg);
     827           0 :         zexception = zend_throw_exception(exception_ce, message, code TSRMLS_CC);
     828           0 :         efree(message);
     829           0 :         return zexception;
     830             : }
     831             : /* }}} */
     832             : 
     833           0 : ZEND_API zval * zend_throw_error_exception(zend_class_entry *exception_ce, const char *message, long code, int severity TSRMLS_DC) /* {{{ */
     834             : {
     835           0 :         zval *ex = zend_throw_exception(exception_ce, message, code TSRMLS_CC);
     836           0 :         zend_update_property_long(default_exception_ce, ex, "severity", sizeof("severity")-1, severity TSRMLS_CC);
     837           0 :         return ex;
     838             : }
     839             : /* }}} */
     840             : 
     841           0 : static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...) /* {{{ */
     842             : {
     843             :         va_list args;
     844             : 
     845           0 :         va_start(args, format);
     846           0 :         zend_error_cb(type, file, lineno, format, args);
     847           0 :         va_end(args);
     848           0 : }
     849             : /* }}} */
     850             : 
     851             : /* This function doesn't return if it uses E_ERROR */
     852           0 : ZEND_API void zend_exception_error(zval *exception, int severity TSRMLS_DC) /* {{{ */
     853             : {
     854           0 :         zend_class_entry *ce_exception = Z_OBJCE_P(exception);
     855           0 :         if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
     856             :                 zval *str, *file, *line;
     857             : 
     858           0 :                 EG(exception) = NULL;
     859             : 
     860           0 :                 zend_call_method_with_0_params(&exception, ce_exception, NULL, "__tostring", &str);
     861           0 :                 if (!EG(exception)) {
     862           0 :                         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           0 :                                 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           0 :                 zval_ptr_dtor(&str);
     869             : 
     870           0 :                 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           0 :                 str = zend_read_property(default_exception_ce, exception, "string", sizeof("string")-1, 1 TSRMLS_CC);
     887           0 :                 file = zend_read_property(default_exception_ce, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
     888           0 :                 line = zend_read_property(default_exception_ce, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
     889             : 
     890           0 :                 convert_to_string(str);
     891           0 :                 convert_to_string(file);
     892           0 :                 convert_to_long(line);
     893             : 
     894           0 :                 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           0 : }
     899             : /* }}} */
     900             : 
     901           0 : ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC) /* {{{ */
     902             : {
     903             :         zend_class_entry *exception_ce;
     904             : 
     905           0 :         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           0 :         exception_ce = Z_OBJCE_P(exception);
     910             : 
     911           0 :         if (!exception_ce || !instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
     912           0 :                 zend_error(E_ERROR, "Exceptions must be valid objects derived from the Exception base class");
     913             :         }
     914           0 :         zend_throw_exception_internal(exception TSRMLS_CC);
     915           0 : }
     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 Wed, 16 Apr 2014 12:47:43 +0000 (41 hours ago)

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