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