PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - var/php_gcov/PHP_5_3/Zend - zend_exceptions.c
Test: PHP Code Coverage
Date: 2009-11-21 Instrumented lines: 350
Code covered: 87.1 % Executed lines: 305
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5

Generated at Sat, 21 Nov 2009 12:26:54 +0000 (3 days ago)

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