xref: /PHP-7.3/Zend/zend_exceptions.c (revision fc6f53d4)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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@php.net>                                 |
16    |          Marcus Boerger <helly@php.net>                              |
17    |          Sterling Hughes <sterling@php.net>                          |
18    |          Zeev Suraski <zeev@php.net>                                 |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include "zend.h"
23 #include "zend_API.h"
24 #include "zend_builtin_functions.h"
25 #include "zend_interfaces.h"
26 #include "zend_exceptions.h"
27 #include "zend_vm.h"
28 #include "zend_dtrace.h"
29 #include "zend_smart_str.h"
30 
31 ZEND_API zend_class_entry *zend_ce_throwable;
32 ZEND_API zend_class_entry *zend_ce_exception;
33 ZEND_API zend_class_entry *zend_ce_error_exception;
34 ZEND_API zend_class_entry *zend_ce_error;
35 ZEND_API zend_class_entry *zend_ce_compile_error;
36 ZEND_API zend_class_entry *zend_ce_parse_error;
37 ZEND_API zend_class_entry *zend_ce_type_error;
38 ZEND_API zend_class_entry *zend_ce_argument_count_error;
39 ZEND_API zend_class_entry *zend_ce_arithmetic_error;
40 ZEND_API zend_class_entry *zend_ce_division_by_zero_error;
41 
42 ZEND_API void (*zend_throw_exception_hook)(zval *ex);
43 
44 static zend_object_handlers default_exception_handlers;
45 
46 /* {{{ zend_implement_throwable */
zend_implement_throwable(zend_class_entry * interface,zend_class_entry * class_type)47 static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type)
48 {
49 	if (instanceof_function(class_type, zend_ce_exception) || instanceof_function(class_type, zend_ce_error)) {
50 		return SUCCESS;
51 	}
52 	zend_error_noreturn(E_ERROR, "Class %s cannot implement interface %s, extend %s or %s instead",
53 		ZSTR_VAL(class_type->name),
54 		ZSTR_VAL(interface->name),
55 		ZSTR_VAL(zend_ce_exception->name),
56 		ZSTR_VAL(zend_ce_error->name));
57 	return FAILURE;
58 }
59 /* }}} */
60 
i_get_exception_base(zval * object)61 static inline zend_class_entry *i_get_exception_base(zval *object) /* {{{ */
62 {
63 	return instanceof_function(Z_OBJCE_P(object), zend_ce_exception) ? zend_ce_exception : zend_ce_error;
64 }
65 /* }}} */
66 
zend_get_exception_base(zval * object)67 ZEND_API zend_class_entry *zend_get_exception_base(zval *object) /* {{{ */
68 {
69 	return i_get_exception_base(object);
70 }
71 /* }}} */
72 
zend_exception_set_previous(zend_object * exception,zend_object * add_previous)73 void zend_exception_set_previous(zend_object *exception, zend_object *add_previous) /* {{{ */
74 {
75     zval *previous, *ancestor, *ex;
76 	zval  pv, zv, rv;
77 	zend_class_entry *base_ce;
78 
79 	if (!exception || !add_previous) {
80 		return;
81 	}
82 
83 	if (exception == add_previous) {
84 		OBJ_RELEASE(add_previous);
85 		return;
86 	}
87 
88 	ZVAL_OBJ(&pv, add_previous);
89 	if (!instanceof_function(Z_OBJCE(pv), zend_ce_throwable)) {
90 		zend_error_noreturn(E_CORE_ERROR, "Previous exception must implement Throwable");
91 		return;
92 	}
93 	ZVAL_OBJ(&zv, exception);
94 	ex = &zv;
95 	do {
96 		ancestor = zend_read_property_ex(i_get_exception_base(&pv), &pv, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
97 		while (Z_TYPE_P(ancestor) == IS_OBJECT) {
98 			if (Z_OBJ_P(ancestor) == Z_OBJ_P(ex)) {
99 				OBJ_RELEASE(add_previous);
100 				return;
101 			}
102 			ancestor = zend_read_property_ex(i_get_exception_base(ancestor), ancestor, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
103 		}
104 		base_ce = i_get_exception_base(ex);
105 		previous = zend_read_property_ex(base_ce, ex, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
106 		if (Z_TYPE_P(previous) == IS_NULL) {
107 			zend_update_property_ex(base_ce, ex, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &pv);
108 			GC_DELREF(add_previous);
109 			return;
110 		}
111 		ex = previous;
112 	} while (Z_OBJ_P(ex) != add_previous);
113 }
114 /* }}} */
115 
zend_exception_save(void)116 void zend_exception_save(void) /* {{{ */
117 {
118 	if (EG(prev_exception)) {
119 		zend_exception_set_previous(EG(exception), EG(prev_exception));
120 	}
121 	if (EG(exception)) {
122 		EG(prev_exception) = EG(exception);
123 	}
124 	EG(exception) = NULL;
125 }
126 /* }}} */
127 
zend_exception_restore(void)128 void zend_exception_restore(void) /* {{{ */
129 {
130 	if (EG(prev_exception)) {
131 		if (EG(exception)) {
132 			zend_exception_set_previous(EG(exception), EG(prev_exception));
133 		} else {
134 			EG(exception) = EG(prev_exception);
135 		}
136 		EG(prev_exception) = NULL;
137 	}
138 }
139 /* }}} */
140 
zend_throw_exception_internal(zval * exception)141 ZEND_API ZEND_COLD void zend_throw_exception_internal(zval *exception) /* {{{ */
142 {
143 #ifdef HAVE_DTRACE
144 	if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
145 		if (exception != NULL) {
146 			DTRACE_EXCEPTION_THROWN(ZSTR_VAL(Z_OBJ_P(exception)->ce->name));
147 		} else {
148 			DTRACE_EXCEPTION_THROWN(NULL);
149 		}
150 	}
151 #endif /* HAVE_DTRACE */
152 
153 	if (exception != NULL) {
154 		zend_object *previous = EG(exception);
155 		zend_exception_set_previous(Z_OBJ_P(exception), EG(exception));
156 		EG(exception) = Z_OBJ_P(exception);
157 		if (previous) {
158 			return;
159 		}
160 	}
161 	if (!EG(current_execute_data)) {
162 		if (exception && (Z_OBJCE_P(exception) == zend_ce_parse_error || Z_OBJCE_P(exception) == zend_ce_compile_error)) {
163 			return;
164 		}
165 		if(EG(exception)) {
166 			zend_exception_error(EG(exception), E_ERROR);
167 		}
168 		zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
169 	}
170 
171 	if (zend_throw_exception_hook) {
172 		zend_throw_exception_hook(exception);
173 	}
174 
175 	if (!EG(current_execute_data)->func ||
176 	    !ZEND_USER_CODE(EG(current_execute_data)->func->common.type) ||
177 	    EG(current_execute_data)->opline->opcode == ZEND_HANDLE_EXCEPTION) {
178 		/* no need to rethrow the exception */
179 		return;
180 	}
181 	EG(opline_before_exception) = EG(current_execute_data)->opline;
182 	EG(current_execute_data)->opline = EG(exception_op);
183 }
184 /* }}} */
185 
zend_clear_exception(void)186 ZEND_API void zend_clear_exception(void) /* {{{ */
187 {
188 	if (EG(prev_exception)) {
189 
190 		OBJ_RELEASE(EG(prev_exception));
191 		EG(prev_exception) = NULL;
192 	}
193 	if (!EG(exception)) {
194 		return;
195 	}
196 	OBJ_RELEASE(EG(exception));
197 	EG(exception) = NULL;
198 	if (EG(current_execute_data)) {
199 		EG(current_execute_data)->opline = EG(opline_before_exception);
200 	}
201 #if ZEND_DEBUG
202 	EG(opline_before_exception) = NULL;
203 #endif
204 }
205 /* }}} */
206 
zend_default_exception_new_ex(zend_class_entry * class_type,int skip_top_traces)207 static zend_object *zend_default_exception_new_ex(zend_class_entry *class_type, int skip_top_traces) /* {{{ */
208 {
209 	zval obj, tmp;
210 	zend_object *object;
211 	zval trace;
212 	zend_class_entry *base_ce;
213 	zend_string *filename;
214 
215 	Z_OBJ(obj) = object = zend_objects_new(class_type);
216 	Z_OBJ_HT(obj) = &default_exception_handlers;
217 
218 	object_properties_init(object, class_type);
219 
220 	if (EG(current_execute_data)) {
221 		zend_fetch_debug_backtrace(&trace, skip_top_traces, 0, 0);
222 	} else {
223 		array_init(&trace);
224 	}
225 	Z_SET_REFCOUNT(trace, 0);
226 
227 	base_ce = i_get_exception_base(&obj);
228 
229 	if (EXPECTED((class_type != zend_ce_parse_error && class_type != zend_ce_compile_error)
230 			|| !(filename = zend_get_compiled_filename()))) {
231 		ZVAL_STRING(&tmp, zend_get_executed_filename());
232 		zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
233 		zval_ptr_dtor(&tmp);
234 		ZVAL_LONG(&tmp, zend_get_executed_lineno());
235 		zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
236 	} else {
237 		ZVAL_STR(&tmp, filename);
238 		zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
239 		ZVAL_LONG(&tmp, zend_get_compiled_lineno());
240 		zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
241 	}
242 	zend_update_property_ex(base_ce, &obj, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
243 
244 	return object;
245 }
246 /* }}} */
247 
zend_default_exception_new(zend_class_entry * class_type)248 static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
249 {
250 	return zend_default_exception_new_ex(class_type, 0);
251 }
252 /* }}} */
253 
zend_error_exception_new(zend_class_entry * class_type)254 static zend_object *zend_error_exception_new(zend_class_entry *class_type) /* {{{ */
255 {
256 	return zend_default_exception_new_ex(class_type, 2);
257 }
258 /* }}} */
259 
260 /* {{{ proto Exception|Error Exception|Error::__clone()
261    Clone the exception object */
ZEND_METHOD(exception,__clone)262 ZEND_COLD ZEND_METHOD(exception, __clone)
263 {
264 	/* Should never be executable */
265 	zend_throw_exception(NULL, "Cannot clone object using __clone()", 0);
266 }
267 /* }}} */
268 
269 /* {{{ proto Exception|Error::__construct(string message, int code [, Throwable previous])
270    Exception constructor */
ZEND_METHOD(exception,__construct)271 ZEND_METHOD(exception, __construct)
272 {
273 	zend_string *message = NULL;
274 	zend_long   code = 0;
275 	zval  tmp, *object, *previous = NULL;
276 	zend_class_entry *base_ce;
277 	int    argc = ZEND_NUM_ARGS();
278 
279 	object = getThis();
280 	base_ce = i_get_exception_base(object);
281 
282 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc, "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
283 		zend_class_entry *ce;
284 
285 		if (Z_TYPE(EX(This)) == IS_OBJECT) {
286 			ce = Z_OBJCE(EX(This));
287 		} else if (Z_CE(EX(This))) {
288 			ce = Z_CE(EX(This));
289 		} else {
290 			ce = base_ce;
291 		}
292 		zend_throw_error(NULL, "Wrong parameters for %s([string $message [, long $code [, Throwable $previous = NULL]]])", ZSTR_VAL(ce->name));
293 		return;
294 	}
295 
296 	if (message) {
297 		ZVAL_STR(&tmp, message);
298 		zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
299 	}
300 
301 	if (code) {
302 		ZVAL_LONG(&tmp, code);
303 		zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
304 	}
305 
306 	if (previous) {
307 		zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
308 	}
309 }
310 /* }}} */
311 
312 /* {{{ proto Exception::__wakeup()
313    Exception unserialize checks */
314 #define CHECK_EXC_TYPE(id, type) \
315 	pvalue = zend_read_property_ex(i_get_exception_base(object), (object), ZSTR_KNOWN(id), 1, &value); \
316 	if (Z_TYPE_P(pvalue) != IS_NULL && Z_TYPE_P(pvalue) != type) { \
317 		zend_unset_property(i_get_exception_base(object), object, ZSTR_VAL(ZSTR_KNOWN(id)), ZSTR_LEN(ZSTR_KNOWN(id))); \
318 	}
319 
ZEND_METHOD(exception,__wakeup)320 ZEND_METHOD(exception, __wakeup)
321 {
322 	zval value, *pvalue;
323 	zval *object = getThis();
324 	CHECK_EXC_TYPE(ZEND_STR_MESSAGE,  IS_STRING);
325 	CHECK_EXC_TYPE(ZEND_STR_STRING,   IS_STRING);
326 	CHECK_EXC_TYPE(ZEND_STR_CODE,     IS_LONG);
327 	CHECK_EXC_TYPE(ZEND_STR_FILE,     IS_STRING);
328 	CHECK_EXC_TYPE(ZEND_STR_LINE,     IS_LONG);
329 	CHECK_EXC_TYPE(ZEND_STR_TRACE,    IS_ARRAY);
330 	pvalue = zend_read_property(i_get_exception_base(object), object, "previous", sizeof("previous")-1, 1, &value);
331 	if (pvalue && Z_TYPE_P(pvalue) != IS_NULL && (Z_TYPE_P(pvalue) != IS_OBJECT ||
332 			!instanceof_function(Z_OBJCE_P(pvalue), zend_ce_throwable) ||
333 			pvalue == object)) {
334 		zend_unset_property(i_get_exception_base(object), object, "previous", sizeof("previous")-1);
335 	}
336 }
337 /* }}} */
338 
339 /* {{{ proto ErrorException::__construct(string message, int code, int severity [, string filename [, int lineno [, Throwable previous]]])
340    ErrorException constructor */
ZEND_METHOD(error_exception,__construct)341 ZEND_METHOD(error_exception, __construct)
342 {
343 	zend_string *message = NULL, *filename = NULL;
344 	zend_long   code = 0, severity = E_ERROR, lineno;
345 	zval   tmp, *object, *previous = NULL;
346 	int    argc = ZEND_NUM_ARGS();
347 
348 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc, "|SllSlO!", &message, &code, &severity, &filename, &lineno, &previous, zend_ce_throwable) == FAILURE) {
349 		zend_class_entry *ce;
350 
351 		if (Z_TYPE(EX(This)) == IS_OBJECT) {
352 			ce = Z_OBJCE(EX(This));
353 		} else if (Z_CE(EX(This))) {
354 			ce = Z_CE(EX(This));
355 		} else {
356 			ce = zend_ce_error_exception;
357 		}
358 		zend_throw_error(NULL, "Wrong parameters for %s([string $message [, long $code, [ long $severity, [ string $filename, [ long $lineno  [, Throwable $previous = NULL]]]]]])", ZSTR_VAL(ce->name));
359 		return;
360 	}
361 
362 	object = getThis();
363 
364 	if (message) {
365 		ZVAL_STR_COPY(&tmp, message);
366 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
367 		zval_ptr_dtor(&tmp);
368 	}
369 
370 	if (code) {
371 		ZVAL_LONG(&tmp, code);
372 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
373 	}
374 
375 	if (previous) {
376 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
377 	}
378 
379 	ZVAL_LONG(&tmp, severity);
380 	zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
381 
382 	if (argc >= 4) {
383 		ZVAL_STR_COPY(&tmp, filename);
384 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
385 		zval_ptr_dtor(&tmp);
386     	if (argc < 5) {
387     	    lineno = 0; /* invalidate lineno */
388     	}
389 		ZVAL_LONG(&tmp, lineno);
390 		zend_update_property_ex(zend_ce_exception, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
391 	}
392 }
393 /* }}} */
394 
395 #define DEFAULT_0_PARAMS \
396 	if (zend_parse_parameters_none() == FAILURE) { \
397 		return; \
398 	}
399 
400 #define GET_PROPERTY(object, id) \
401 	zend_read_property_ex(i_get_exception_base(object), (object), ZSTR_KNOWN(id), 0, &rv)
402 #define GET_PROPERTY_SILENT(object, id) \
403 	zend_read_property_ex(i_get_exception_base(object), (object), ZSTR_KNOWN(id), 1, &rv)
404 
405 /* {{{ proto string Exception|Error::getFile()
406    Get the file in which the exception occurred */
ZEND_METHOD(exception,getFile)407 ZEND_METHOD(exception, getFile)
408 {
409 	zval *prop, rv;
410 
411 	DEFAULT_0_PARAMS;
412 
413 	prop = GET_PROPERTY(getThis(), ZEND_STR_FILE);
414 	ZVAL_DEREF(prop);
415 	ZVAL_COPY(return_value, prop);
416 }
417 /* }}} */
418 
419 /* {{{ proto int Exception|Error::getLine()
420    Get the line in which the exception occurred */
ZEND_METHOD(exception,getLine)421 ZEND_METHOD(exception, getLine)
422 {
423 	zval *prop, rv;
424 
425 	DEFAULT_0_PARAMS;
426 
427 	prop = GET_PROPERTY(getThis(), ZEND_STR_LINE);
428 	ZVAL_DEREF(prop);
429 	ZVAL_COPY(return_value, prop);
430 }
431 /* }}} */
432 
433 /* {{{ proto string Exception|Error::getMessage()
434    Get the exception message */
ZEND_METHOD(exception,getMessage)435 ZEND_METHOD(exception, getMessage)
436 {
437 	zval *prop, rv;
438 
439 	DEFAULT_0_PARAMS;
440 
441 	prop = GET_PROPERTY(getThis(), ZEND_STR_MESSAGE);
442 	ZVAL_DEREF(prop);
443 	ZVAL_COPY(return_value, prop);
444 }
445 /* }}} */
446 
447 /* {{{ proto int Exception|Error::getCode()
448    Get the exception code */
ZEND_METHOD(exception,getCode)449 ZEND_METHOD(exception, getCode)
450 {
451 	zval *prop, rv;
452 
453 	DEFAULT_0_PARAMS;
454 
455 	prop = GET_PROPERTY(getThis(), ZEND_STR_CODE);
456 	ZVAL_DEREF(prop);
457 	ZVAL_COPY(return_value, prop);
458 }
459 /* }}} */
460 
461 /* {{{ proto array Exception|Error::getTrace()
462    Get the stack trace for the location in which the exception occurred */
ZEND_METHOD(exception,getTrace)463 ZEND_METHOD(exception, getTrace)
464 {
465 	zval *prop, rv;
466 
467 	DEFAULT_0_PARAMS;
468 
469 	prop = GET_PROPERTY(getThis(), ZEND_STR_TRACE);
470 	ZVAL_DEREF(prop);
471 	ZVAL_COPY(return_value, prop);
472 }
473 /* }}} */
474 
475 /* {{{ proto int ErrorException::getSeverity()
476    Get the exception severity */
ZEND_METHOD(error_exception,getSeverity)477 ZEND_METHOD(error_exception, getSeverity)
478 {
479 	zval *prop, rv;
480 
481 	DEFAULT_0_PARAMS;
482 
483 	prop = GET_PROPERTY(getThis(), ZEND_STR_SEVERITY);
484 	ZVAL_DEREF(prop);
485 	ZVAL_COPY(return_value, prop);
486 }
487 /* }}} */
488 
489 #define TRACE_APPEND_KEY(key) do {                                          \
490 		tmp = zend_hash_find(ht, key);                                      \
491 		if (tmp) {                                                          \
492 			if (Z_TYPE_P(tmp) != IS_STRING) {                               \
493 				zend_error(E_WARNING, "Value for %s is no string",          \
494 					ZSTR_VAL(key));                                         \
495 				smart_str_appends(str, "[unknown]");                        \
496 			} else {                                                        \
497 				smart_str_appends(str, Z_STRVAL_P(tmp));                    \
498 			}                                                               \
499 		} \
500 	} while (0)
501 
_build_trace_args(zval * arg,smart_str * str)502 static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
503 {
504 	/* the trivial way would be to do
505 	 * convert_to_string_ex(arg);
506 	 * append it and kill the now tmp arg.
507 	 * but that could cause some E_NOTICE and also damn long lines.
508 	 */
509 
510 	ZVAL_DEREF(arg);
511 	switch (Z_TYPE_P(arg)) {
512 		case IS_NULL:
513 			smart_str_appends(str, "NULL, ");
514 			break;
515 		case IS_STRING:
516 			smart_str_appendc(str, '\'');
517 			smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), 15));
518 			if (Z_STRLEN_P(arg) > 15) {
519 				smart_str_appends(str, "...', ");
520 			} else {
521 				smart_str_appends(str, "', ");
522 			}
523 			break;
524 		case IS_FALSE:
525 			smart_str_appends(str, "false, ");
526 			break;
527 		case IS_TRUE:
528 			smart_str_appends(str, "true, ");
529 			break;
530 		case IS_RESOURCE:
531 			smart_str_appends(str, "Resource id #");
532 			smart_str_append_long(str, Z_RES_HANDLE_P(arg));
533 			smart_str_appends(str, ", ");
534 			break;
535 		case IS_LONG:
536 			smart_str_append_long(str, Z_LVAL_P(arg));
537 			smart_str_appends(str, ", ");
538 			break;
539 		case IS_DOUBLE: {
540 			smart_str_append_printf(str, "%.*G", (int) EG(precision), Z_DVAL_P(arg));
541 			smart_str_appends(str, ", ");
542 			break;
543 		}
544 		case IS_ARRAY:
545 			smart_str_appends(str, "Array, ");
546 			break;
547 		case IS_OBJECT: {
548 			zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
549 			smart_str_appends(str, "Object(");
550 			smart_str_appends(str, ZSTR_VAL(class_name));
551 			smart_str_appends(str, "), ");
552 			zend_string_release_ex(class_name, 0);
553 			break;
554 		}
555 	}
556 }
557 /* }}} */
558 
_build_trace_string(smart_str * str,HashTable * ht,uint32_t num)559 static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /* {{{ */
560 {
561 	zval *file, *tmp;
562 
563 	smart_str_appendc(str, '#');
564 	smart_str_append_long(str, num);
565 	smart_str_appendc(str, ' ');
566 
567 	file = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_FILE), 1);
568 	if (file) {
569 		if (Z_TYPE_P(file) != IS_STRING) {
570 			zend_error(E_WARNING, "Function name is no string");
571 			smart_str_appends(str, "[unknown function]");
572 		} else{
573 			zend_long line;
574 			tmp = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_LINE), 1);
575 			if (tmp) {
576 				if (Z_TYPE_P(tmp) == IS_LONG) {
577 					line = Z_LVAL_P(tmp);
578 				} else {
579 					zend_error(E_WARNING, "Line is no long");
580 					line = 0;
581 				}
582 			} else {
583 				line = 0;
584 			}
585 			smart_str_append(str, Z_STR_P(file));
586 			smart_str_appendc(str, '(');
587 			smart_str_append_long(str, line);
588 			smart_str_appends(str, "): ");
589 		}
590 	} else {
591 		smart_str_appends(str, "[internal function]: ");
592 	}
593 	TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_CLASS));
594 	TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_TYPE));
595 	TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_FUNCTION));
596 	smart_str_appendc(str, '(');
597 	tmp = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_ARGS), 1);
598 	if (tmp) {
599 		if (Z_TYPE_P(tmp) == IS_ARRAY) {
600 			size_t last_len = ZSTR_LEN(str->s);
601 			zval *arg;
602 
603 			ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmp), arg) {
604 				_build_trace_args(arg, str);
605 			} ZEND_HASH_FOREACH_END();
606 
607 			if (last_len != ZSTR_LEN(str->s)) {
608 				ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
609 			}
610 		} else {
611 			zend_error(E_WARNING, "args element is no array");
612 		}
613 	}
614 	smart_str_appends(str, ")\n");
615 }
616 /* }}} */
617 
618 /* {{{ proto string Exception|Error::getTraceAsString()
619    Obtain the backtrace for the exception as a string (instead of an array) */
ZEND_METHOD(exception,getTraceAsString)620 ZEND_METHOD(exception, getTraceAsString)
621 {
622 	zval *trace, *frame, rv;
623 	zend_ulong index;
624 	zval *object;
625 	zend_class_entry *base_ce;
626 	smart_str str = {0};
627 	uint32_t num = 0;
628 
629 	DEFAULT_0_PARAMS;
630 
631 	object = getThis();
632 	base_ce = i_get_exception_base(object);
633 
634 	trace = zend_read_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
635 	if (Z_TYPE_P(trace) != IS_ARRAY) {
636 		RETURN_FALSE;
637 	}
638 	ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(trace), index, frame) {
639 		if (Z_TYPE_P(frame) != IS_ARRAY) {
640 			zend_error(E_WARNING, "Expected array for frame " ZEND_ULONG_FMT, index);
641 			continue;
642 		}
643 
644 		_build_trace_string(&str, Z_ARRVAL_P(frame), num++);
645 	} ZEND_HASH_FOREACH_END();
646 
647 	smart_str_appendc(&str, '#');
648 	smart_str_append_long(&str, num);
649 	smart_str_appends(&str, " {main}");
650 	smart_str_0(&str);
651 
652 	RETURN_NEW_STR(str.s);
653 }
654 /* }}} */
655 
656 /* {{{ proto Throwable Exception|Error::getPrevious()
657    Return previous Throwable or NULL. */
ZEND_METHOD(exception,getPrevious)658 ZEND_METHOD(exception, getPrevious)
659 {
660 	zval rv;
661 
662 	DEFAULT_0_PARAMS;
663 
664 	ZVAL_COPY(return_value, GET_PROPERTY_SILENT(getThis(), ZEND_STR_PREVIOUS));
665 } /* }}} */
666 
667 /* {{{ proto string Exception|Error::__toString()
668    Obtain the string representation of the Exception object */
ZEND_METHOD(exception,__toString)669 ZEND_METHOD(exception, __toString)
670 {
671 	zval trace, *exception;
672 	zend_class_entry *base_ce;
673 	zend_string *str;
674 	zend_fcall_info fci;
675 	zval rv, tmp;
676 	zend_string *fname;
677 
678 	DEFAULT_0_PARAMS;
679 
680 	str = ZSTR_EMPTY_ALLOC();
681 
682 	exception = getThis();
683 	fname = zend_string_init("gettraceasstring", sizeof("gettraceasstring")-1, 0);
684 
685 	while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
686 		zend_string *prev_str = str;
687 		zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
688 		zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
689 		zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));
690 
691 		fci.size = sizeof(fci);
692 		ZVAL_STR(&fci.function_name, fname);
693 		fci.object = Z_OBJ_P(exception);
694 		fci.retval = &trace;
695 		fci.param_count = 0;
696 		fci.params = NULL;
697 		fci.no_separation = 1;
698 
699 		zend_call_function(&fci, NULL);
700 
701 		if (Z_TYPE(trace) != IS_STRING) {
702 			zval_ptr_dtor(&trace);
703 			ZVAL_UNDEF(&trace);
704 		}
705 
706 		if ((Z_OBJCE_P(exception) == zend_ce_type_error || Z_OBJCE_P(exception) == zend_ce_argument_count_error) && strstr(ZSTR_VAL(message), ", called in ")) {
707 			zend_string *real_message = zend_strpprintf(0, "%s and defined", ZSTR_VAL(message));
708 			zend_string_release_ex(message, 0);
709 			message = real_message;
710 		}
711 
712 		if (ZSTR_LEN(message) > 0) {
713 			str = zend_strpprintf(0, "%s: %s in %s:" ZEND_LONG_FMT
714 					"\nStack trace:\n%s%s%s",
715 					ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(message), ZSTR_VAL(file), line,
716 					(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
717 					ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
718 		} else {
719 			str = zend_strpprintf(0, "%s in %s:" ZEND_LONG_FMT
720 					"\nStack trace:\n%s%s%s",
721 					ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(file), line,
722 					(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
723 					ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
724 		}
725 
726 		zend_string_release_ex(prev_str, 0);
727 		zend_string_release_ex(message, 0);
728 		zend_string_release_ex(file, 0);
729 		zval_ptr_dtor(&trace);
730 
731 		Z_PROTECT_RECURSION_P(exception);
732 		exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
733 		if (exception && Z_TYPE_P(exception) == IS_OBJECT && Z_IS_RECURSIVE_P(exception)) {
734 			break;
735 		}
736 	}
737 	zend_string_release_ex(fname, 0);
738 
739 	exception = getThis();
740 	/* Reset apply counts */
741 	while (exception && Z_TYPE_P(exception) == IS_OBJECT && (base_ce = i_get_exception_base(exception)) && instanceof_function(Z_OBJCE_P(exception), base_ce)) {
742 		if (Z_IS_RECURSIVE_P(exception)) {
743 			Z_UNPROTECT_RECURSION_P(exception);
744 		} else {
745 			break;
746 		}
747 		exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
748 	}
749 
750 	exception = getThis();
751 	base_ce = i_get_exception_base(exception);
752 
753 	/* We store the result in the private property string so we can access
754 	 * the result in uncaught exception handlers without memleaks. */
755 	ZVAL_STR(&tmp, str);
756 	zend_update_property_ex(base_ce, exception, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
757 
758 	RETURN_STR(str);
759 }
760 /* }}} */
761 
762 /** {{{ Throwable method definition */
763 static const zend_function_entry zend_funcs_throwable[] = {
764 	ZEND_ABSTRACT_ME(throwable, getMessage,       NULL)
765 	ZEND_ABSTRACT_ME(throwable, getCode,          NULL)
766 	ZEND_ABSTRACT_ME(throwable, getFile,          NULL)
767 	ZEND_ABSTRACT_ME(throwable, getLine,          NULL)
768 	ZEND_ABSTRACT_ME(throwable, getTrace,         NULL)
769 	ZEND_ABSTRACT_ME(throwable, getPrevious,      NULL)
770 	ZEND_ABSTRACT_ME(throwable, getTraceAsString, NULL)
771 	ZEND_ABSTRACT_ME(throwable, __toString,       NULL)
772 	ZEND_FE_END
773 };
774 /* }}} */
775 
776 /* {{{ internal structs */
777 /* All functions that may be used in uncaught exception handlers must be final
778  * and must not throw exceptions. Otherwise we would need a facility to handle
779  * such exceptions in that handler.
780  * Also all getXY() methods are final because thy serve as read only access to
781  * their corresponding properties, no more, no less. If after all you need to
782  * override somthing then it is method __toString().
783  * And never try to change the state of exceptions and never implement anything
784  * that gives the user anything to accomplish this.
785  */
786 ZEND_BEGIN_ARG_INFO_EX(arginfo_exception___construct, 0, 0, 0)
787 	ZEND_ARG_INFO(0, message)
788 	ZEND_ARG_INFO(0, code)
789 	ZEND_ARG_INFO(0, previous)
790 ZEND_END_ARG_INFO()
791 
792 static const zend_function_entry default_exception_functions[] = {
793 	ZEND_ME(exception, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
794 	ZEND_ME(exception, __construct, arginfo_exception___construct, ZEND_ACC_PUBLIC)
795 	ZEND_ME(exception, __wakeup, NULL, ZEND_ACC_PUBLIC)
796 	ZEND_ME(exception, getMessage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
797 	ZEND_ME(exception, getCode, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
798 	ZEND_ME(exception, getFile, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
799 	ZEND_ME(exception, getLine, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
800 	ZEND_ME(exception, getTrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
801 	ZEND_ME(exception, getPrevious, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
802 	ZEND_ME(exception, getTraceAsString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
803 	ZEND_ME(exception, __toString, NULL, 0)
804 	ZEND_FE_END
805 };
806 
807 ZEND_BEGIN_ARG_INFO_EX(arginfo_error_exception___construct, 0, 0, 0)
808 	ZEND_ARG_INFO(0, message)
809 	ZEND_ARG_INFO(0, code)
810 	ZEND_ARG_INFO(0, severity)
811 	ZEND_ARG_INFO(0, filename)
812 	ZEND_ARG_INFO(0, lineno)
813 	ZEND_ARG_INFO(0, previous)
814 ZEND_END_ARG_INFO()
815 
816 static const zend_function_entry error_exception_functions[] = {
817 	ZEND_ME(error_exception, __construct, arginfo_error_exception___construct, ZEND_ACC_PUBLIC)
818 	ZEND_ME(error_exception, getSeverity, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
819 	ZEND_FE_END
820 };
821 /* }}} */
822 
zend_register_default_exception(void)823 void zend_register_default_exception(void) /* {{{ */
824 {
825 	zend_class_entry ce;
826 
827 	REGISTER_MAGIC_INTERFACE(throwable, Throwable);
828 
829 	memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers));
830 	default_exception_handlers.clone_obj = NULL;
831 
832 	INIT_CLASS_ENTRY(ce, "Exception", default_exception_functions);
833 	zend_ce_exception = zend_register_internal_class_ex(&ce, NULL);
834 	zend_ce_exception->create_object = zend_default_exception_new;
835 	zend_class_implements(zend_ce_exception, 1, zend_ce_throwable);
836 
837 	zend_declare_property_string(zend_ce_exception, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
838 	zend_declare_property_string(zend_ce_exception, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
839 	zend_declare_property_long(zend_ce_exception, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
840 	zend_declare_property_null(zend_ce_exception, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
841 	zend_declare_property_null(zend_ce_exception, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
842 	zend_declare_property_null(zend_ce_exception, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE);
843 	zend_declare_property_null(zend_ce_exception, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE);
844 
845 	INIT_CLASS_ENTRY(ce, "ErrorException", error_exception_functions);
846 	zend_ce_error_exception = zend_register_internal_class_ex(&ce, zend_ce_exception);
847 	zend_ce_error_exception->create_object = zend_error_exception_new;
848 	zend_declare_property_long(zend_ce_error_exception, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED);
849 
850 	INIT_CLASS_ENTRY(ce, "Error", default_exception_functions);
851 	zend_ce_error = zend_register_internal_class_ex(&ce, NULL);
852 	zend_ce_error->create_object = zend_default_exception_new;
853 	zend_class_implements(zend_ce_error, 1, zend_ce_throwable);
854 
855 	zend_declare_property_string(zend_ce_error, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
856 	zend_declare_property_string(zend_ce_error, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
857 	zend_declare_property_long(zend_ce_error, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
858 	zend_declare_property_null(zend_ce_error, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
859 	zend_declare_property_null(zend_ce_error, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
860 	zend_declare_property_null(zend_ce_error, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE);
861 	zend_declare_property_null(zend_ce_error, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE);
862 
863 	INIT_CLASS_ENTRY(ce, "CompileError", NULL);
864 	zend_ce_compile_error = zend_register_internal_class_ex(&ce, zend_ce_error);
865 	zend_ce_compile_error->create_object = zend_default_exception_new;
866 
867 	INIT_CLASS_ENTRY(ce, "ParseError", NULL);
868 	zend_ce_parse_error = zend_register_internal_class_ex(&ce, zend_ce_compile_error);
869 	zend_ce_parse_error->create_object = zend_default_exception_new;
870 
871 	INIT_CLASS_ENTRY(ce, "TypeError", NULL);
872 	zend_ce_type_error = zend_register_internal_class_ex(&ce, zend_ce_error);
873 	zend_ce_type_error->create_object = zend_default_exception_new;
874 
875 	INIT_CLASS_ENTRY(ce, "ArgumentCountError", NULL);
876 	zend_ce_argument_count_error = zend_register_internal_class_ex(&ce, zend_ce_type_error);
877 	zend_ce_argument_count_error->create_object = zend_default_exception_new;
878 
879 	INIT_CLASS_ENTRY(ce, "ArithmeticError", NULL);
880 	zend_ce_arithmetic_error = zend_register_internal_class_ex(&ce, zend_ce_error);
881 	zend_ce_arithmetic_error->create_object = zend_default_exception_new;
882 
883 	INIT_CLASS_ENTRY(ce, "DivisionByZeroError", NULL);
884 	zend_ce_division_by_zero_error = zend_register_internal_class_ex(&ce, zend_ce_arithmetic_error);
885 	zend_ce_division_by_zero_error->create_object = zend_default_exception_new;
886 }
887 /* }}} */
888 
889 /* {{{ Deprecated - Use zend_ce_exception directly instead */
zend_exception_get_default(void)890 ZEND_API zend_class_entry *zend_exception_get_default(void)
891 {
892 	return zend_ce_exception;
893 }
894 /* }}} */
895 
896 /* {{{ Deprecated - Use zend_ce_error_exception directly instead */
zend_get_error_exception(void)897 ZEND_API zend_class_entry *zend_get_error_exception(void)
898 {
899 	return zend_ce_error_exception;
900 }
901 /* }}} */
902 
zend_throw_exception(zend_class_entry * exception_ce,const char * message,zend_long code)903 ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
904 {
905 	zval ex, tmp;
906 
907 	if (exception_ce) {
908 		if (!instanceof_function(exception_ce, zend_ce_throwable)) {
909 			zend_error(E_NOTICE, "Exceptions must implement Throwable");
910 			exception_ce = zend_ce_exception;
911 		}
912 	} else {
913 		exception_ce = zend_ce_exception;
914 	}
915 	object_init_ex(&ex, exception_ce);
916 
917 
918 	if (message) {
919 		ZVAL_STRING(&tmp, message);
920 		zend_update_property_ex(exception_ce, &ex, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
921 		zval_ptr_dtor(&tmp);
922 	}
923 	if (code) {
924 		ZVAL_LONG(&tmp, code);
925 		zend_update_property_ex(exception_ce, &ex, ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
926 	}
927 
928 	zend_throw_exception_internal(&ex);
929 	return Z_OBJ(ex);
930 }
931 /* }}} */
932 
zend_throw_exception_ex(zend_class_entry * exception_ce,zend_long code,const char * format,...)933 ZEND_API ZEND_COLD zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
934 {
935 	va_list arg;
936 	char *message;
937 	zend_object *obj;
938 
939 	va_start(arg, format);
940 	zend_vspprintf(&message, 0, format, arg);
941 	va_end(arg);
942 	obj = zend_throw_exception(exception_ce, message, code);
943 	efree(message);
944 	return obj;
945 }
946 /* }}} */
947 
zend_throw_error_exception(zend_class_entry * exception_ce,const char * message,zend_long code,int severity)948 ZEND_API ZEND_COLD zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, const char *message, zend_long code, int severity) /* {{{ */
949 {
950 	zval ex, tmp;
951 	zend_object *obj = zend_throw_exception(exception_ce, message, code);
952 	ZVAL_OBJ(&ex, obj);
953 	ZVAL_LONG(&tmp, severity);
954 	zend_update_property_ex(zend_ce_error_exception, &ex, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
955 	return obj;
956 }
957 /* }}} */
958 
zend_error_va(int type,const char * file,uint32_t lineno,const char * format,...)959 static void zend_error_va(int type, const char *file, uint32_t lineno, const char *format, ...) /* {{{ */
960 {
961 	va_list args;
962 
963 	va_start(args, format);
964 	zend_error_cb(type, file, lineno, format, args);
965 	va_end(args);
966 }
967 /* }}} */
968 
zend_error_helper(int type,const char * filename,const uint32_t lineno,const char * format,...)969 static void zend_error_helper(int type, const char *filename, const uint32_t lineno, const char *format, ...) /* {{{ */
970 {
971 	va_list va;
972 
973 	va_start(va, format);
974 	zend_error_cb(type, filename, lineno, format, va);
975 	va_end(va);
976 }
977 /* }}} */
978 
979 /* This function doesn't return if it uses E_ERROR */
zend_exception_error(zend_object * ex,int severity)980 ZEND_API ZEND_COLD void zend_exception_error(zend_object *ex, int severity) /* {{{ */
981 {
982 	zval exception, rv;
983 	zend_class_entry *ce_exception;
984 
985 	ZVAL_OBJ(&exception, ex);
986 	ce_exception = Z_OBJCE(exception);
987 	EG(exception) = NULL;
988 	if (ce_exception == zend_ce_parse_error || ce_exception == zend_ce_compile_error) {
989 		zend_string *message = zval_get_string(GET_PROPERTY(&exception, ZEND_STR_MESSAGE));
990 		zend_string *file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
991 		zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
992 
993 		zend_error_helper(ce_exception == zend_ce_parse_error ? E_PARSE : E_COMPILE_ERROR,
994 			ZSTR_VAL(file), line, "%s", ZSTR_VAL(message));
995 
996 		zend_string_release_ex(file, 0);
997 		zend_string_release_ex(message, 0);
998 	} else if (instanceof_function(ce_exception, zend_ce_throwable)) {
999 		zval tmp, rv;
1000 		zend_string *str, *file = NULL;
1001 		zend_long line = 0;
1002 
1003 		zend_call_method_with_0_params(&exception, ce_exception, &ex->ce->__tostring, "__tostring", &tmp);
1004 		if (!EG(exception)) {
1005 			if (Z_TYPE(tmp) != IS_STRING) {
1006 				zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
1007 			} else {
1008 				zend_update_property_ex(i_get_exception_base(&exception), &exception, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
1009 			}
1010 		}
1011 		zval_ptr_dtor(&tmp);
1012 
1013 		if (EG(exception)) {
1014 			zval zv;
1015 
1016 			ZVAL_OBJ(&zv, EG(exception));
1017 			/* do the best we can to inform about the inner exception */
1018 			if (instanceof_function(ce_exception, zend_ce_exception) || instanceof_function(ce_exception, zend_ce_error)) {
1019 				file = zval_get_string(GET_PROPERTY_SILENT(&zv, ZEND_STR_FILE));
1020 				line = zval_get_long(GET_PROPERTY_SILENT(&zv, ZEND_STR_LINE));
1021 			}
1022 
1023 			zend_error_va(E_WARNING, (file && ZSTR_LEN(file) > 0) ? ZSTR_VAL(file) : NULL, line,
1024 				"Uncaught %s in exception handling during call to %s::__tostring()",
1025 				ZSTR_VAL(Z_OBJCE(zv)->name), ZSTR_VAL(ce_exception->name));
1026 
1027 			if (file) {
1028 				zend_string_release_ex(file, 0);
1029 			}
1030 		}
1031 
1032 		str = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_STRING));
1033 		file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
1034 		line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
1035 
1036 		zend_error_va(severity, (file && ZSTR_LEN(file) > 0) ? ZSTR_VAL(file) : NULL, line,
1037 			"Uncaught %s\n  thrown", ZSTR_VAL(str));
1038 
1039 		zend_string_release_ex(str, 0);
1040 		zend_string_release_ex(file, 0);
1041 	} else {
1042 		zend_error(severity, "Uncaught exception '%s'", ZSTR_VAL(ce_exception->name));
1043 	}
1044 
1045 	OBJ_RELEASE(ex);
1046 }
1047 /* }}} */
1048 
zend_throw_exception_object(zval * exception)1049 ZEND_API ZEND_COLD void zend_throw_exception_object(zval *exception) /* {{{ */
1050 {
1051 	zend_class_entry *exception_ce;
1052 
1053 	if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
1054 		zend_error_noreturn(E_CORE_ERROR, "Need to supply an object when throwing an exception");
1055 	}
1056 
1057 	exception_ce = Z_OBJCE_P(exception);
1058 
1059 	if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
1060 		zend_throw_error(NULL, "Cannot throw objects that do not implement Throwable");
1061 		zval_ptr_dtor(exception);
1062 		return;
1063 	}
1064 	zend_throw_exception_internal(exception);
1065 }
1066 /* }}} */
1067 
1068 /*
1069  * Local variables:
1070  * tab-width: 4
1071  * c-basic-offset: 4
1072  * indent-tabs-mode: t
1073  * End:
1074  * vim600: sw=4 ts=4 fdm=marker
1075  * vim<600: sw=4 ts=4
1076  */
1077