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