2003-03-23 04:32:24 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 09:23:29 +00:00
|
|
|
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
|
2003-03-23 04:32:24 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
2003-03-23 06:57:16 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-10 20:04:29 +00:00
|
|
|
| available through the world-wide-web at the following url: |
|
2003-03-23 04:32:24 +00:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2018-11-01 15:20:07 +00:00
|
|
|
| Authors: Andi Gutmans <andi@php.net> |
|
2003-08-23 19:48:52 +00:00
|
|
|
| Marcus Boerger <helly@php.net> |
|
2004-02-12 10:38:14 +00:00
|
|
|
| Sterling Hughes <sterling@php.net> |
|
2018-11-01 15:20:07 +00:00
|
|
|
| Zeev Suraski <zeev@php.net> |
|
2003-03-23 04:32:24 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_API.h"
|
2003-08-28 20:35:54 +00:00
|
|
|
#include "zend_builtin_functions.h"
|
2003-10-22 20:04:48 +00:00
|
|
|
#include "zend_interfaces.h"
|
2004-07-15 22:21:36 +00:00
|
|
|
#include "zend_exceptions.h"
|
2008-01-21 19:39:55 +00:00
|
|
|
#include "zend_vm.h"
|
2010-04-24 13:32:30 +00:00
|
|
|
#include "zend_dtrace.h"
|
2014-09-20 22:21:41 +00:00
|
|
|
#include "zend_smart_str.h"
|
2019-10-15 12:30:23 +00:00
|
|
|
#include "zend_exceptions_arginfo.h"
|
2020-09-14 12:05:54 +00:00
|
|
|
#include "zend_observer.h"
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2015-06-15 23:07:27 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_throwable;
|
2015-07-03 14:45:03 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_exception;
|
|
|
|
ZEND_API zend_class_entry *zend_ce_error_exception;
|
|
|
|
ZEND_API zend_class_entry *zend_ce_error;
|
2017-09-24 13:24:51 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_compile_error;
|
2015-07-03 14:45:03 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_parse_error;
|
|
|
|
ZEND_API zend_class_entry *zend_ce_type_error;
|
2016-08-30 19:09:26 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_argument_count_error;
|
2019-09-04 20:39:02 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_value_error;
|
2015-07-02 18:47:44 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_arithmetic_error;
|
2015-07-01 23:54:08 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_division_by_zero_error;
|
2020-04-09 20:36:37 +00:00
|
|
|
ZEND_API zend_class_entry *zend_ce_unhandled_match_error;
|
2015-06-15 23:07:27 +00:00
|
|
|
|
2020-03-06 13:57:55 +00:00
|
|
|
/* Internal pseudo-exception that is not exposed to userland. */
|
|
|
|
static zend_class_entry zend_ce_unwind_exit;
|
|
|
|
|
2020-08-05 16:45:07 +00:00
|
|
|
ZEND_API void (*zend_throw_exception_hook)(zend_object *ex);
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2015-07-03 01:57:53 +00:00
|
|
|
static zend_object_handlers default_exception_handlers;
|
|
|
|
|
2015-06-15 23:07:27 +00:00
|
|
|
/* {{{ zend_implement_throwable */
|
|
|
|
static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type)
|
|
|
|
{
|
2015-07-03 14:45:03 +00:00
|
|
|
if (instanceof_function(class_type, zend_ce_exception) || instanceof_function(class_type, zend_ce_error)) {
|
2015-06-15 23:07:27 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
zend_error_noreturn(E_ERROR, "Class %s cannot implement interface %s, extend %s or %s instead",
|
2015-06-30 10:59:27 +00:00
|
|
|
ZSTR_VAL(class_type->name),
|
|
|
|
ZSTR_VAL(interface->name),
|
2015-07-03 14:45:03 +00:00
|
|
|
ZSTR_VAL(zend_ce_exception->name),
|
|
|
|
ZSTR_VAL(zend_ce_error->name));
|
2015-06-15 23:07:27 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-08-07 13:15:50 +00:00
|
|
|
static inline zend_class_entry *i_get_exception_base(zend_object *object) /* {{{ */
|
2015-05-17 22:31:06 +00:00
|
|
|
{
|
2020-08-07 13:15:50 +00:00
|
|
|
return instanceof_function(object->ce, zend_ce_exception) ? zend_ce_exception : zend_ce_error;
|
2015-05-17 22:31:06 +00:00
|
|
|
}
|
2015-11-21 15:14:12 +00:00
|
|
|
/* }}} */
|
2015-05-17 22:31:06 +00:00
|
|
|
|
2020-08-07 13:15:50 +00:00
|
|
|
ZEND_API zend_class_entry *zend_get_exception_base(zend_object *object) /* {{{ */
|
2015-06-28 23:41:04 +00:00
|
|
|
{
|
|
|
|
return i_get_exception_base(object);
|
|
|
|
}
|
2015-11-21 15:14:12 +00:00
|
|
|
/* }}} */
|
2015-06-28 23:41:04 +00:00
|
|
|
|
2015-11-21 15:14:12 +00:00
|
|
|
void zend_exception_set_previous(zend_object *exception, zend_object *add_previous) /* {{{ */
|
2008-07-13 21:42:49 +00:00
|
|
|
{
|
2015-11-21 13:16:11 +00:00
|
|
|
zval *previous, *ancestor, *ex;
|
|
|
|
zval pv, zv, rv;
|
2015-05-17 22:31:06 +00:00
|
|
|
zend_class_entry *base_ce;
|
2008-07-13 21:42:49 +00:00
|
|
|
|
2020-06-30 10:22:41 +00:00
|
|
|
if (!exception || !add_previous) {
|
2008-07-13 21:42:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-06 13:57:55 +00:00
|
|
|
|
2020-06-30 10:26:03 +00:00
|
|
|
if (exception == add_previous || zend_is_unwind_exit(add_previous)) {
|
2020-03-06 13:57:55 +00:00
|
|
|
OBJ_RELEASE(add_previous);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:05:19 +00:00
|
|
|
ZEND_ASSERT(instanceof_function(add_previous->ce, zend_ce_throwable)
|
|
|
|
&& "Previous execption must implement Throwable");
|
|
|
|
|
2015-11-21 13:16:11 +00:00
|
|
|
ZVAL_OBJ(&pv, add_previous);
|
2014-02-10 06:04:30 +00:00
|
|
|
ZVAL_OBJ(&zv, exception);
|
2015-11-21 13:16:11 +00:00
|
|
|
ex = &zv;
|
2014-02-10 06:04:30 +00:00
|
|
|
do {
|
2020-08-07 13:15:50 +00:00
|
|
|
ancestor = zend_read_property_ex(i_get_exception_base(add_previous), add_previous, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
|
2015-11-21 13:16:11 +00:00
|
|
|
while (Z_TYPE_P(ancestor) == IS_OBJECT) {
|
|
|
|
if (Z_OBJ_P(ancestor) == Z_OBJ_P(ex)) {
|
|
|
|
OBJ_RELEASE(add_previous);
|
|
|
|
return;
|
|
|
|
}
|
2020-08-07 13:15:50 +00:00
|
|
|
ancestor = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(ancestor)), Z_OBJ_P(ancestor), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
|
2015-11-21 13:16:11 +00:00
|
|
|
}
|
2020-08-07 13:15:50 +00:00
|
|
|
base_ce = i_get_exception_base(Z_OBJ_P(ex));
|
2020-08-07 13:05:24 +00:00
|
|
|
previous = zend_read_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
|
2008-07-13 21:42:49 +00:00
|
|
|
if (Z_TYPE_P(previous) == IS_NULL) {
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), &pv);
|
2017-10-26 22:28:58 +00:00
|
|
|
GC_DELREF(add_previous);
|
2008-07-13 21:42:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-21 13:16:11 +00:00
|
|
|
ex = previous;
|
|
|
|
} while (Z_OBJ_P(ex) != add_previous);
|
2008-07-13 21:42:49 +00:00
|
|
|
}
|
2015-11-21 15:14:12 +00:00
|
|
|
/* }}} */
|
2008-07-13 21:42:49 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
void zend_exception_save(void) /* {{{ */
|
2008-08-14 10:24:52 +00:00
|
|
|
{
|
|
|
|
if (EG(prev_exception)) {
|
2014-12-13 22:06:14 +00:00
|
|
|
zend_exception_set_previous(EG(exception), EG(prev_exception));
|
2008-08-14 10:24:52 +00:00
|
|
|
}
|
|
|
|
if (EG(exception)) {
|
|
|
|
EG(prev_exception) = EG(exception);
|
|
|
|
}
|
|
|
|
EG(exception) = NULL;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
void zend_exception_restore(void) /* {{{ */
|
2008-08-14 10:24:52 +00:00
|
|
|
{
|
|
|
|
if (EG(prev_exception)) {
|
|
|
|
if (EG(exception)) {
|
2014-12-13 22:06:14 +00:00
|
|
|
zend_exception_set_previous(EG(exception), EG(prev_exception));
|
2008-08-14 10:24:52 +00:00
|
|
|
} else {
|
|
|
|
EG(exception) = EG(prev_exception);
|
|
|
|
}
|
|
|
|
EG(prev_exception) = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-10-13 09:38:30 +00:00
|
|
|
static zend_always_inline zend_bool is_handle_exception_set() {
|
|
|
|
zend_execute_data *execute_data = EG(current_execute_data);
|
|
|
|
return !execute_data->func
|
|
|
|
|| !ZEND_USER_CODE(execute_data->func->common.type)
|
|
|
|
|| execute_data->opline->opcode == ZEND_HANDLE_EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:45:07 +00:00
|
|
|
ZEND_API ZEND_COLD void zend_throw_exception_internal(zend_object *exception) /* {{{ */
|
2004-02-12 10:38:14 +00:00
|
|
|
{
|
2010-04-24 13:32:30 +00:00
|
|
|
#ifdef HAVE_DTRACE
|
|
|
|
if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
|
2010-11-17 21:41:30 +00:00
|
|
|
if (exception != NULL) {
|
2020-08-05 16:45:07 +00:00
|
|
|
DTRACE_EXCEPTION_THROWN(ZSTR_VAL(exception->ce->name));
|
2010-11-17 21:41:30 +00:00
|
|
|
} else {
|
|
|
|
DTRACE_EXCEPTION_THROWN(NULL);
|
|
|
|
}
|
2010-04-24 13:32:30 +00:00
|
|
|
}
|
2010-04-25 19:17:16 +00:00
|
|
|
#endif /* HAVE_DTRACE */
|
2010-04-24 13:32:30 +00:00
|
|
|
|
2004-02-12 10:38:14 +00:00
|
|
|
if (exception != NULL) {
|
2014-02-10 06:04:30 +00:00
|
|
|
zend_object *previous = EG(exception);
|
2020-08-05 16:45:07 +00:00
|
|
|
zend_exception_set_previous(exception, EG(exception));
|
|
|
|
EG(exception) = exception;
|
2008-08-14 10:24:52 +00:00
|
|
|
if (previous) {
|
2020-10-13 09:38:30 +00:00
|
|
|
ZEND_ASSERT(is_handle_exception_set() && "HANDLE_EXCEPTION not set?");
|
2008-08-14 10:24:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-02-12 10:38:14 +00:00
|
|
|
}
|
|
|
|
if (!EG(current_execute_data)) {
|
2020-08-05 16:45:07 +00:00
|
|
|
if (exception && (exception->ce == zend_ce_parse_error || exception->ce == zend_ce_compile_error)) {
|
2015-03-09 12:57:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-10-10 12:14:36 +00:00
|
|
|
if (EG(exception)) {
|
2014-12-13 22:06:14 +00:00
|
|
|
zend_exception_error(EG(exception), E_ERROR);
|
2019-10-10 12:14:36 +00:00
|
|
|
zend_bailout();
|
2011-01-16 21:24:43 +00:00
|
|
|
}
|
2015-04-02 11:19:52 +00:00
|
|
|
zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
|
2004-02-12 10:38:14 +00:00
|
|
|
}
|
|
|
|
|
2004-04-13 15:19:21 +00:00
|
|
|
if (zend_throw_exception_hook) {
|
2014-12-13 22:06:14 +00:00
|
|
|
zend_throw_exception_hook(exception);
|
2004-04-13 15:19:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 09:38:30 +00:00
|
|
|
if (is_handle_exception_set()) {
|
2004-02-12 10:38:14 +00:00
|
|
|
/* no need to rethrow the exception */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
EG(opline_before_exception) = EG(current_execute_data)->opline;
|
2008-01-21 19:39:55 +00:00
|
|
|
EG(current_execute_data)->opline = EG(exception_op);
|
2004-02-12 10:38:14 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2004-02-12 10:38:14 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
ZEND_API void zend_clear_exception(void) /* {{{ */
|
2004-02-12 10:38:14 +00:00
|
|
|
{
|
2020-04-29 10:58:28 +00:00
|
|
|
zend_object *exception;
|
2008-08-14 10:24:52 +00:00
|
|
|
if (EG(prev_exception)) {
|
2014-02-14 13:48:45 +00:00
|
|
|
OBJ_RELEASE(EG(prev_exception));
|
2008-08-14 10:24:52 +00:00
|
|
|
EG(prev_exception) = NULL;
|
|
|
|
}
|
2004-02-12 10:38:14 +00:00
|
|
|
if (!EG(exception)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-29 10:58:28 +00:00
|
|
|
/* exception may have destructor */
|
|
|
|
exception = EG(exception);
|
2004-02-12 10:38:14 +00:00
|
|
|
EG(exception) = NULL;
|
2020-04-29 10:58:28 +00:00
|
|
|
OBJ_RELEASE(exception);
|
2016-12-07 12:10:59 +00:00
|
|
|
if (EG(current_execute_data)) {
|
|
|
|
EG(current_execute_data)->opline = EG(opline_before_exception);
|
|
|
|
}
|
2004-02-12 10:38:14 +00:00
|
|
|
#if ZEND_DEBUG
|
|
|
|
EG(opline_before_exception) = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2004-02-12 10:38:14 +00:00
|
|
|
|
2020-08-28 13:41:27 +00:00
|
|
|
static zend_object *zend_default_exception_new_ex(zend_class_entry *class_type, bool skip_top_traces) /* {{{ */
|
2003-08-23 19:41:22 +00:00
|
|
|
{
|
2020-08-07 13:27:57 +00:00
|
|
|
zval tmp;
|
2014-02-10 06:04:30 +00:00
|
|
|
zval trace;
|
2015-05-17 22:31:06 +00:00
|
|
|
zend_class_entry *base_ce;
|
2015-05-18 03:09:09 +00:00
|
|
|
zend_string *filename;
|
2003-08-23 19:41:22 +00:00
|
|
|
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_object *object = zend_objects_new(class_type);
|
|
|
|
object->handlers = &default_exception_handlers;
|
2003-08-23 19:41:22 +00:00
|
|
|
|
2010-05-24 14:11:39 +00:00
|
|
|
object_properties_init(object, class_type);
|
2003-08-23 19:41:22 +00:00
|
|
|
|
2015-03-09 12:57:15 +00:00
|
|
|
if (EG(current_execute_data)) {
|
2019-06-17 18:51:52 +00:00
|
|
|
zend_fetch_debug_backtrace(&trace,
|
|
|
|
skip_top_traces,
|
|
|
|
EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
|
2015-03-09 12:57:15 +00:00
|
|
|
} else {
|
|
|
|
array_init(&trace);
|
|
|
|
}
|
2014-02-21 12:14:42 +00:00
|
|
|
Z_SET_REFCOUNT(trace, 0);
|
2015-08-04 23:14:24 +00:00
|
|
|
|
2020-08-07 13:15:50 +00:00
|
|
|
base_ce = i_get_exception_base(object);
|
2003-08-28 20:35:54 +00:00
|
|
|
|
2017-09-24 13:24:51 +00:00
|
|
|
if (EXPECTED((class_type != zend_ce_parse_error && class_type != zend_ce_compile_error)
|
|
|
|
|| !(filename = zend_get_compiled_filename()))) {
|
2017-11-02 04:12:40 +00:00
|
|
|
ZVAL_STRING(&tmp, zend_get_executed_filename());
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
|
2017-11-02 04:12:40 +00:00
|
|
|
zval_ptr_dtor(&tmp);
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_LONG(&tmp, zend_get_executed_lineno());
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
|
2015-03-09 12:57:15 +00:00
|
|
|
} else {
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_STR(&tmp, filename);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_LONG(&tmp, zend_get_compiled_lineno());
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
|
2015-03-09 12:57:15 +00:00
|
|
|
}
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
|
2003-08-23 19:41:22 +00:00
|
|
|
|
2014-02-10 06:04:30 +00:00
|
|
|
return object;
|
2003-08-23 19:41:22 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2003-08-23 19:41:22 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
|
2004-07-15 22:21:36 +00:00
|
|
|
{
|
2014-12-13 22:06:14 +00:00
|
|
|
return zend_default_exception_new_ex(class_type, 0);
|
2004-07-15 22:21:36 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2004-07-15 22:21:36 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static zend_object *zend_error_exception_new(zend_class_entry *class_type) /* {{{ */
|
2004-07-15 22:21:36 +00:00
|
|
|
{
|
2014-12-13 22:06:14 +00:00
|
|
|
return zend_default_exception_new_ex(class_type, 2);
|
2004-07-15 22:21:36 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2004-07-20 09:24:22 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Clone the exception object */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_COLD ZEND_METHOD(Exception, __clone)
|
2019-01-14 08:17:28 +00:00
|
|
|
{
|
|
|
|
/* Should never be executable */
|
|
|
|
zend_throw_exception(NULL, "Cannot clone object using __clone()", 0);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Exception constructor */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, __construct)
|
2003-03-23 04:32:24 +00:00
|
|
|
{
|
2014-02-21 12:14:42 +00:00
|
|
|
zend_string *message = NULL;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long code = 0;
|
2016-05-12 10:47:22 +00:00
|
|
|
zval tmp, *object, *previous = NULL;
|
2015-05-17 22:31:06 +00:00
|
|
|
zend_class_entry *base_ce;
|
2003-03-23 06:57:16 +00:00
|
|
|
|
2018-11-15 16:54:19 +00:00
|
|
|
object = ZEND_THIS;
|
2020-08-07 13:15:50 +00:00
|
|
|
base_ce = i_get_exception_base(Z_OBJ_P(object));
|
2015-06-18 00:48:17 +00:00
|
|
|
|
2020-04-25 22:15:52 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
|
2020-01-01 15:42:30 +00:00
|
|
|
RETURN_THROWS();
|
2003-03-23 04:32:24 +00:00
|
|
|
}
|
|
|
|
|
2003-08-23 19:41:22 +00:00
|
|
|
if (message) {
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_STR(&tmp, message);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
|
2003-08-23 19:41:22 +00:00
|
|
|
}
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2003-08-23 19:41:22 +00:00
|
|
|
if (code) {
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_LONG(&tmp, code);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
|
2003-08-23 19:41:22 +00:00
|
|
|
}
|
2008-07-12 14:57:14 +00:00
|
|
|
|
|
|
|
if (previous) {
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
|
2008-07-12 14:57:14 +00:00
|
|
|
}
|
2003-03-23 04:32:24 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Exception unserialize checks */
|
2016-05-12 10:47:22 +00:00
|
|
|
#define CHECK_EXC_TYPE(id, type) \
|
2020-08-07 13:15:50 +00:00
|
|
|
pvalue = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &value); \
|
2016-07-12 03:51:20 +00:00
|
|
|
if (Z_TYPE_P(pvalue) != IS_NULL && Z_TYPE_P(pvalue) != type) { \
|
2020-08-07 13:17:59 +00:00
|
|
|
zend_unset_property(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_VAL(ZSTR_KNOWN(id)), ZSTR_LEN(ZSTR_KNOWN(id))); \
|
2015-07-27 08:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, __wakeup)
|
2015-07-27 08:38:27 +00:00
|
|
|
{
|
2020-07-17 14:04:55 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
|
|
|
|
2015-08-05 06:41:00 +00:00
|
|
|
zval value, *pvalue;
|
2018-11-15 16:54:19 +00:00
|
|
|
zval *object = ZEND_THIS;
|
2016-05-12 10:47:22 +00:00
|
|
|
CHECK_EXC_TYPE(ZEND_STR_MESSAGE, IS_STRING);
|
|
|
|
CHECK_EXC_TYPE(ZEND_STR_STRING, IS_STRING);
|
|
|
|
CHECK_EXC_TYPE(ZEND_STR_CODE, IS_LONG);
|
|
|
|
CHECK_EXC_TYPE(ZEND_STR_FILE, IS_STRING);
|
|
|
|
CHECK_EXC_TYPE(ZEND_STR_LINE, IS_LONG);
|
2020-05-28 12:01:29 +00:00
|
|
|
/* The type of $trace and $previous is enforced through typed properties. */
|
2015-07-27 08:38:27 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ ErrorException constructor */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(ErrorException, __construct)
|
2004-07-15 22:21:36 +00:00
|
|
|
{
|
2018-03-06 18:20:19 +00:00
|
|
|
zend_string *message = NULL, *filename = NULL;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long code = 0, severity = E_ERROR, lineno;
|
2020-09-19 12:13:42 +00:00
|
|
|
zend_bool lineno_is_null = 1;
|
2016-05-12 10:47:22 +00:00
|
|
|
zval tmp, *object, *previous = NULL;
|
2004-07-15 22:21:36 +00:00
|
|
|
|
2020-09-19 12:13:42 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SllS!l!O!", &message, &code, &severity, &filename, &lineno, &lineno_is_null, &previous, zend_ce_throwable) == FAILURE) {
|
2020-01-01 15:42:30 +00:00
|
|
|
RETURN_THROWS();
|
2004-07-15 22:21:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 16:54:19 +00:00
|
|
|
object = ZEND_THIS;
|
2004-07-15 22:21:36 +00:00
|
|
|
|
|
|
|
if (message) {
|
2018-03-06 18:20:19 +00:00
|
|
|
ZVAL_STR_COPY(&tmp, message);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
|
2016-05-12 10:47:22 +00:00
|
|
|
zval_ptr_dtor(&tmp);
|
2004-07-15 22:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (code) {
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_LONG(&tmp, code);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
|
2004-07-15 22:21:36 +00:00
|
|
|
}
|
|
|
|
|
2008-07-12 14:57:14 +00:00
|
|
|
if (previous) {
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
|
2008-07-12 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_LONG(&tmp, severity);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2020-09-19 12:13:42 +00:00
|
|
|
if (filename) {
|
2018-03-06 18:20:19 +00:00
|
|
|
ZVAL_STR_COPY(&tmp, filename);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
|
2016-05-12 10:47:22 +00:00
|
|
|
zval_ptr_dtor(&tmp);
|
2020-09-19 12:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!lineno_is_null) {
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_LONG(&tmp, lineno);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
|
2020-09-19 12:13:42 +00:00
|
|
|
} else if (filename) {
|
|
|
|
ZVAL_LONG(&tmp, 0);
|
|
|
|
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
|
2004-07-25 07:05:48 +00:00
|
|
|
}
|
2004-07-15 22:21:36 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
2004-07-15 22:21:36 +00:00
|
|
|
|
2016-05-12 10:47:22 +00:00
|
|
|
#define GET_PROPERTY(object, id) \
|
2020-08-07 13:15:50 +00:00
|
|
|
zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 0, &rv)
|
2016-05-12 10:47:22 +00:00
|
|
|
#define GET_PROPERTY_SILENT(object, id) \
|
2020-08-07 13:15:50 +00:00
|
|
|
zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &rv)
|
2004-07-20 09:24:22 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Get the file in which the exception occurred */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, getFile)
|
2003-03-23 04:32:24 +00:00
|
|
|
{
|
2019-02-25 12:00:16 +00:00
|
|
|
zval *prop, rv;
|
2015-01-22 08:50:42 +00:00
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2019-02-25 12:02:58 +00:00
|
|
|
prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_FILE);
|
2020-05-28 12:19:47 +00:00
|
|
|
RETURN_STR(zval_get_string(prop));
|
2003-03-23 04:32:24 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Get the line in which the exception occurred */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, getLine)
|
2003-03-23 04:32:24 +00:00
|
|
|
{
|
2019-02-25 12:00:16 +00:00
|
|
|
zval *prop, rv;
|
2015-01-22 08:50:42 +00:00
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2019-02-25 12:02:58 +00:00
|
|
|
prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_LINE);
|
2020-05-28 12:19:47 +00:00
|
|
|
RETURN_LONG(zval_get_long(prop));
|
2003-03-23 04:32:24 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Get the exception message */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, getMessage)
|
2003-03-23 04:32:24 +00:00
|
|
|
{
|
2019-02-25 12:00:16 +00:00
|
|
|
zval *prop, rv;
|
2015-01-22 08:50:42 +00:00
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2019-02-25 12:02:58 +00:00
|
|
|
prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_MESSAGE);
|
2020-05-25 10:10:41 +00:00
|
|
|
RETURN_STR(zval_get_string(prop));
|
2003-03-23 04:32:24 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Get the exception code */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, getCode)
|
2003-03-23 04:32:24 +00:00
|
|
|
{
|
2019-02-25 12:00:16 +00:00
|
|
|
zval *prop, rv;
|
2015-01-22 08:50:42 +00:00
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2019-02-25 12:02:58 +00:00
|
|
|
prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_CODE);
|
2019-02-25 12:00:16 +00:00
|
|
|
ZVAL_DEREF(prop);
|
|
|
|
ZVAL_COPY(return_value, prop);
|
2003-03-23 04:32:24 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Get the stack trace for the location in which the exception occurred */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, getTrace)
|
2003-08-28 23:43:56 +00:00
|
|
|
{
|
2019-02-25 12:00:16 +00:00
|
|
|
zval *prop, rv;
|
2015-01-22 08:50:42 +00:00
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2003-08-28 23:43:56 +00:00
|
|
|
|
2019-02-25 12:02:58 +00:00
|
|
|
prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_TRACE);
|
2019-02-25 12:00:16 +00:00
|
|
|
ZVAL_DEREF(prop);
|
|
|
|
ZVAL_COPY(return_value, prop);
|
2003-08-28 23:43:56 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
2003-08-28 23:43:56 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Get the exception severity */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(ErrorException, getSeverity)
|
2004-07-15 22:21:36 +00:00
|
|
|
{
|
2019-02-25 12:00:16 +00:00
|
|
|
zval *prop, rv;
|
2015-01-22 08:50:42 +00:00
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2004-07-15 22:21:36 +00:00
|
|
|
|
2019-02-25 12:02:58 +00:00
|
|
|
prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_SEVERITY);
|
2019-02-25 12:00:16 +00:00
|
|
|
ZVAL_DEREF(prop);
|
|
|
|
ZVAL_COPY(return_value, prop);
|
2004-07-15 22:21:36 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
2004-07-15 22:21:36 +00:00
|
|
|
|
2014-02-10 06:04:30 +00:00
|
|
|
#define TRACE_APPEND_KEY(key) do { \
|
2016-05-12 10:47:22 +00:00
|
|
|
tmp = zend_hash_find(ht, key); \
|
2014-02-19 04:13:43 +00:00
|
|
|
if (tmp) { \
|
|
|
|
if (Z_TYPE_P(tmp) != IS_STRING) { \
|
2020-05-23 18:43:03 +00:00
|
|
|
zend_error(E_WARNING, "Value for %s is not a string", \
|
2016-05-12 10:47:22 +00:00
|
|
|
ZSTR_VAL(key)); \
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "[unknown]"); \
|
2014-02-10 06:04:30 +00:00
|
|
|
} else { \
|
2016-05-12 10:47:22 +00:00
|
|
|
smart_str_appends(str, Z_STRVAL_P(tmp)); \
|
2014-02-10 06:04:30 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2003-08-30 18:28:24 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
|
2003-08-30 18:28:24 +00:00
|
|
|
{
|
2014-10-06 18:14:30 +00:00
|
|
|
/* the trivial way would be to do
|
2014-02-20 16:39:58 +00:00
|
|
|
* convert_to_string_ex(arg);
|
2003-08-30 18:28:24 +00:00
|
|
|
* append it and kill the now tmp arg.
|
|
|
|
* but that could cause some E_NOTICE and also damn long lines.
|
|
|
|
*/
|
|
|
|
|
2014-03-27 09:39:09 +00:00
|
|
|
ZVAL_DEREF(arg);
|
2014-02-10 06:04:30 +00:00
|
|
|
switch (Z_TYPE_P(arg)) {
|
2003-08-30 18:28:24 +00:00
|
|
|
case IS_NULL:
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "NULL, ");
|
2003-08-30 18:28:24 +00:00
|
|
|
break;
|
2014-09-20 22:21:41 +00:00
|
|
|
case IS_STRING:
|
|
|
|
smart_str_appendc(str, '\'');
|
2020-06-25 13:44:12 +00:00
|
|
|
smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), EG(exception_string_param_max_len)));
|
|
|
|
if (Z_STRLEN_P(arg) > EG(exception_string_param_max_len)) {
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "...', ");
|
2003-08-30 18:28:24 +00:00
|
|
|
} else {
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "', ");
|
2003-08-30 18:28:24 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-04-30 14:32:42 +00:00
|
|
|
case IS_FALSE:
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "false, ");
|
2014-04-30 14:32:42 +00:00
|
|
|
break;
|
|
|
|
case IS_TRUE:
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "true, ");
|
2003-08-30 18:28:24 +00:00
|
|
|
break;
|
2014-09-20 22:21:41 +00:00
|
|
|
case IS_RESOURCE:
|
|
|
|
smart_str_appends(str, "Resource id #");
|
|
|
|
smart_str_append_long(str, Z_RES_HANDLE_P(arg));
|
|
|
|
smart_str_appends(str, ", ");
|
2014-02-21 18:59:51 +00:00
|
|
|
break;
|
2014-09-20 22:21:41 +00:00
|
|
|
case IS_LONG:
|
|
|
|
smart_str_append_long(str, Z_LVAL_P(arg));
|
|
|
|
smart_str_appends(str, ", ");
|
2003-08-30 18:28:24 +00:00
|
|
|
break;
|
|
|
|
case IS_DOUBLE: {
|
2017-02-07 11:40:10 +00:00
|
|
|
smart_str_append_printf(str, "%.*G", (int) EG(precision), Z_DVAL_P(arg));
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, ", ");
|
2003-08-30 18:28:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IS_ARRAY:
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "Array, ");
|
2003-08-30 18:28:24 +00:00
|
|
|
break;
|
2016-06-22 14:23:04 +00:00
|
|
|
case IS_OBJECT: {
|
|
|
|
zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "Object(");
|
2016-06-22 14:23:04 +00:00
|
|
|
smart_str_appends(str, ZSTR_VAL(class_name));
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "), ");
|
2018-05-28 13:27:12 +00:00
|
|
|
zend_string_release_ex(class_name, 0);
|
2003-08-30 18:28:24 +00:00
|
|
|
break;
|
2016-06-22 14:23:04 +00:00
|
|
|
}
|
2003-08-30 18:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2003-08-30 18:28:24 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /* {{{ */
|
2003-08-30 18:28:24 +00:00
|
|
|
{
|
2014-02-10 06:04:30 +00:00
|
|
|
zval *file, *tmp;
|
2003-08-30 18:28:24 +00:00
|
|
|
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appendc(str, '#');
|
|
|
|
smart_str_append_long(str, num);
|
|
|
|
smart_str_appendc(str, ' ');
|
2012-12-13 21:39:35 +00:00
|
|
|
|
2017-12-04 10:21:35 +00:00
|
|
|
file = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_FILE), 1);
|
2014-02-10 06:04:30 +00:00
|
|
|
if (file) {
|
|
|
|
if (Z_TYPE_P(file) != IS_STRING) {
|
2020-05-23 18:43:03 +00:00
|
|
|
zend_error(E_WARNING, "Function name is not a string");
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "[unknown function]");
|
2012-12-13 21:39:35 +00:00
|
|
|
} else{
|
2014-09-20 22:21:41 +00:00
|
|
|
zend_long line;
|
2017-12-04 10:21:35 +00:00
|
|
|
tmp = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_LINE), 1);
|
2014-02-10 06:04:30 +00:00
|
|
|
if (tmp) {
|
2014-08-25 17:24:55 +00:00
|
|
|
if (Z_TYPE_P(tmp) == IS_LONG) {
|
|
|
|
line = Z_LVAL_P(tmp);
|
2012-12-13 21:39:35 +00:00
|
|
|
} else {
|
2020-05-23 18:43:03 +00:00
|
|
|
zend_error(E_WARNING, "Line is not an int");
|
2012-12-13 21:39:35 +00:00
|
|
|
line = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
line = 0;
|
|
|
|
}
|
2014-09-21 18:47:07 +00:00
|
|
|
smart_str_append(str, Z_STR_P(file));
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appendc(str, '(');
|
|
|
|
smart_str_append_long(str, line);
|
|
|
|
smart_str_appends(str, "): ");
|
2003-08-30 18:28:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, "[internal function]: ");
|
2003-08-30 18:28:24 +00:00
|
|
|
}
|
2017-03-04 09:39:13 +00:00
|
|
|
TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_CLASS));
|
|
|
|
TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_TYPE));
|
|
|
|
TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_FUNCTION));
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appendc(str, '(');
|
2017-12-04 10:21:35 +00:00
|
|
|
tmp = zend_hash_find_ex(ht, ZSTR_KNOWN(ZEND_STR_ARGS), 1);
|
2014-02-10 06:04:30 +00:00
|
|
|
if (tmp) {
|
|
|
|
if (Z_TYPE_P(tmp) == IS_ARRAY) {
|
2015-06-30 10:59:27 +00:00
|
|
|
size_t last_len = ZSTR_LEN(str->s);
|
2020-04-06 10:46:52 +00:00
|
|
|
zend_string *name;
|
2014-05-28 14:45:01 +00:00
|
|
|
zval *arg;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2020-04-06 10:46:52 +00:00
|
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), name, arg) {
|
|
|
|
if (name) {
|
|
|
|
smart_str_append(str, name);
|
|
|
|
smart_str_appends(str, ": ");
|
|
|
|
}
|
2014-12-13 22:06:14 +00:00
|
|
|
_build_trace_args(arg, str);
|
2014-05-28 14:45:01 +00:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
|
2015-06-30 10:59:27 +00:00
|
|
|
if (last_len != ZSTR_LEN(str->s)) {
|
|
|
|
ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
|
2012-12-13 21:39:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-23 18:43:03 +00:00
|
|
|
zend_error(E_WARNING, "args element is not an array");
|
2003-09-01 21:40:58 +00:00
|
|
|
}
|
2003-08-30 18:28:24 +00:00
|
|
|
}
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appends(str, ")\n");
|
2003-08-30 18:28:24 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2004-07-20 09:24:22 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Obtain the backtrace for the exception as a string (instead of an array) */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, getTraceAsString)
|
2003-08-30 18:28:24 +00:00
|
|
|
{
|
2015-01-22 08:50:42 +00:00
|
|
|
zval *trace, *frame, rv;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_ulong index;
|
2015-05-17 22:31:06 +00:00
|
|
|
zval *object;
|
|
|
|
zend_class_entry *base_ce;
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str str = {0};
|
|
|
|
uint32_t num = 0;
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2015-07-27 00:09:34 +00:00
|
|
|
|
2018-11-15 16:54:19 +00:00
|
|
|
object = ZEND_THIS;
|
2020-08-07 13:15:50 +00:00
|
|
|
base_ce = i_get_exception_base(Z_OBJ_P(object));
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2020-08-07 13:05:24 +00:00
|
|
|
trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
|
2020-05-28 10:23:37 +00:00
|
|
|
if (EG(exception)) {
|
|
|
|
RETURN_THROWS();
|
2015-04-06 00:30:59 +00:00
|
|
|
}
|
2020-05-28 10:23:37 +00:00
|
|
|
|
|
|
|
/* Type should be guaranteed by property type. */
|
|
|
|
ZEND_ASSERT(Z_TYPE_P(trace) == IS_ARRAY);
|
2014-08-25 21:05:05 +00:00
|
|
|
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(trace), index, frame) {
|
2014-09-20 22:21:41 +00:00
|
|
|
if (Z_TYPE_P(frame) != IS_ARRAY) {
|
2016-06-21 13:00:37 +00:00
|
|
|
zend_error(E_WARNING, "Expected array for frame " ZEND_ULONG_FMT, index);
|
2014-09-20 22:21:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2003-08-30 18:28:24 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
_build_trace_string(&str, Z_ARRVAL_P(frame), num++);
|
2014-09-20 22:21:41 +00:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2014-02-19 04:13:43 +00:00
|
|
|
|
2014-09-20 22:21:41 +00:00
|
|
|
smart_str_appendc(&str, '#');
|
|
|
|
smart_str_append_long(&str, num);
|
|
|
|
smart_str_appends(&str, " {main}");
|
|
|
|
smart_str_0(&str);
|
2003-09-01 20:54:48 +00:00
|
|
|
|
2015-01-03 09:22:58 +00:00
|
|
|
RETURN_NEW_STR(str.s);
|
2003-08-30 18:28:24 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Return previous Throwable or NULL. */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, getPrevious)
|
2008-07-12 14:57:14 +00:00
|
|
|
{
|
2015-01-22 08:50:42 +00:00
|
|
|
zval rv;
|
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2009-05-11 14:14:52 +00:00
|
|
|
|
2018-11-15 16:54:19 +00:00
|
|
|
ZVAL_COPY(return_value, GET_PROPERTY_SILENT(ZEND_THIS, ZEND_STR_PREVIOUS));
|
2014-05-09 16:21:49 +00:00
|
|
|
} /* }}} */
|
2008-07-12 14:57:14 +00:00
|
|
|
|
2020-07-01 13:32:55 +00:00
|
|
|
/* {{{ Obtain the string representation of the Exception object */
|
2020-04-25 21:10:07 +00:00
|
|
|
ZEND_METHOD(Exception, __toString)
|
2003-08-30 18:28:24 +00:00
|
|
|
{
|
2014-10-06 18:14:30 +00:00
|
|
|
zval trace, *exception;
|
2015-05-17 22:31:06 +00:00
|
|
|
zend_class_entry *base_ce;
|
2014-10-06 18:14:30 +00:00
|
|
|
zend_string *str;
|
2003-08-30 18:28:24 +00:00
|
|
|
zend_fcall_info fci;
|
2016-05-12 10:47:22 +00:00
|
|
|
zval rv, tmp;
|
2016-04-29 11:44:56 +00:00
|
|
|
zend_string *fname;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2020-01-03 11:11:45 +00:00
|
|
|
ZEND_PARSE_PARAMETERS_NONE();
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2015-06-29 13:44:54 +00:00
|
|
|
str = ZSTR_EMPTY_ALLOC();
|
2004-02-10 16:08:01 +00:00
|
|
|
|
2018-11-15 16:54:19 +00:00
|
|
|
exception = ZEND_THIS;
|
2016-04-29 11:44:56 +00:00
|
|
|
fname = zend_string_init("gettraceasstring", sizeof("gettraceasstring")-1, 0);
|
2003-08-30 18:28:24 +00:00
|
|
|
|
2015-08-05 06:41:00 +00:00
|
|
|
while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
|
2014-10-06 18:14:30 +00:00
|
|
|
zend_string *prev_str = str;
|
2016-05-12 10:47:22 +00:00
|
|
|
zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
|
|
|
|
zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
|
|
|
|
zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));
|
2008-07-12 14:57:14 +00:00
|
|
|
|
|
|
|
fci.size = sizeof(fci);
|
2016-04-29 11:44:56 +00:00
|
|
|
ZVAL_STR(&fci.function_name, fname);
|
2014-03-27 22:11:22 +00:00
|
|
|
fci.object = Z_OBJ_P(exception);
|
2014-02-14 13:48:45 +00:00
|
|
|
fci.retval = &trace;
|
2008-07-12 14:57:14 +00:00
|
|
|
fci.param_count = 0;
|
|
|
|
fci.params = NULL;
|
2020-04-06 10:46:52 +00:00
|
|
|
fci.named_params = NULL;
|
2008-07-12 14:57:14 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
zend_call_function(&fci, NULL);
|
2008-07-12 14:57:14 +00:00
|
|
|
|
2014-02-14 13:48:45 +00:00
|
|
|
if (Z_TYPE(trace) != IS_STRING) {
|
|
|
|
zval_ptr_dtor(&trace);
|
|
|
|
ZVAL_UNDEF(&trace);
|
|
|
|
}
|
2003-08-30 18:28:24 +00:00
|
|
|
|
2016-08-30 19:09:26 +00:00
|
|
|
if ((Z_OBJCE_P(exception) == zend_ce_type_error || Z_OBJCE_P(exception) == zend_ce_argument_count_error) && strstr(ZSTR_VAL(message), ", called in ")) {
|
2015-06-30 10:59:27 +00:00
|
|
|
zend_string *real_message = zend_strpprintf(0, "%s and defined", ZSTR_VAL(message));
|
2018-05-28 13:27:12 +00:00
|
|
|
zend_string_release_ex(message, 0);
|
2015-05-17 16:59:34 +00:00
|
|
|
message = real_message;
|
|
|
|
}
|
|
|
|
|
2015-06-30 10:59:27 +00:00
|
|
|
if (ZSTR_LEN(message) > 0) {
|
2015-05-17 16:35:18 +00:00
|
|
|
str = zend_strpprintf(0, "%s: %s in %s:" ZEND_LONG_FMT
|
2014-10-06 18:14:30 +00:00
|
|
|
"\nStack trace:\n%s%s%s",
|
2015-06-30 10:59:27 +00:00
|
|
|
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(message), ZSTR_VAL(file), line,
|
2014-08-25 17:24:55 +00:00
|
|
|
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
|
2015-06-30 10:59:27 +00:00
|
|
|
ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
|
2008-07-12 14:57:14 +00:00
|
|
|
} else {
|
2015-05-17 16:35:18 +00:00
|
|
|
str = zend_strpprintf(0, "%s in %s:" ZEND_LONG_FMT
|
2014-10-06 18:14:30 +00:00
|
|
|
"\nStack trace:\n%s%s%s",
|
2015-06-30 10:59:27 +00:00
|
|
|
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(file), line,
|
2014-08-25 17:24:55 +00:00
|
|
|
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
|
2015-06-30 10:59:27 +00:00
|
|
|
ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
|
2008-07-12 14:57:14 +00:00
|
|
|
}
|
2010-08-16 08:11:08 +00:00
|
|
|
|
2018-05-28 13:27:12 +00:00
|
|
|
zend_string_release_ex(prev_str, 0);
|
|
|
|
zend_string_release_ex(message, 0);
|
|
|
|
zend_string_release_ex(file, 0);
|
2014-02-14 13:48:45 +00:00
|
|
|
zval_ptr_dtor(&trace);
|
2010-08-16 08:11:08 +00:00
|
|
|
|
2017-10-05 22:34:50 +00:00
|
|
|
Z_PROTECT_RECURSION_P(exception);
|
2016-05-12 10:47:22 +00:00
|
|
|
exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
|
2017-10-05 22:34:50 +00:00
|
|
|
if (exception && Z_TYPE_P(exception) == IS_OBJECT && Z_IS_RECURSIVE_P(exception)) {
|
2016-10-22 06:50:21 +00:00
|
|
|
break;
|
2016-10-12 18:09:24 +00:00
|
|
|
}
|
2004-01-02 19:27:02 +00:00
|
|
|
}
|
2018-05-28 13:27:12 +00:00
|
|
|
zend_string_release_ex(fname, 0);
|
2003-08-30 18:28:24 +00:00
|
|
|
|
2018-11-15 16:54:19 +00:00
|
|
|
exception = ZEND_THIS;
|
2016-10-12 18:09:24 +00:00
|
|
|
/* Reset apply counts */
|
2020-08-07 13:15:50 +00:00
|
|
|
while (exception && Z_TYPE_P(exception) == IS_OBJECT && (base_ce = i_get_exception_base(Z_OBJ_P(exception))) && instanceof_function(Z_OBJCE_P(exception), base_ce)) {
|
2017-10-05 22:34:50 +00:00
|
|
|
if (Z_IS_RECURSIVE_P(exception)) {
|
|
|
|
Z_UNPROTECT_RECURSION_P(exception);
|
2016-10-12 18:09:24 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:54:19 +00:00
|
|
|
exception = ZEND_THIS;
|
2020-08-07 13:15:50 +00:00
|
|
|
base_ce = i_get_exception_base(Z_OBJ_P(exception));
|
2015-05-17 22:31:06 +00:00
|
|
|
|
2003-08-30 18:28:24 +00:00
|
|
|
/* We store the result in the private property string so we can access
|
|
|
|
* the result in uncaught exception handlers without memleaks. */
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_STR(&tmp, str);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(base_ce, Z_OBJ_P(exception), ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
|
2004-02-10 16:08:01 +00:00
|
|
|
|
2014-05-09 16:21:49 +00:00
|
|
|
RETURN_STR(str);
|
2003-08-30 18:28:24 +00:00
|
|
|
}
|
2004-07-20 09:24:22 +00:00
|
|
|
/* }}} */
|
2003-08-30 18:28:24 +00:00
|
|
|
|
2020-05-28 10:13:08 +00:00
|
|
|
static void declare_exception_properties(zend_class_entry *ce)
|
|
|
|
{
|
2020-05-28 10:23:37 +00:00
|
|
|
zval val;
|
|
|
|
|
2020-05-28 10:13:08 +00:00
|
|
|
zend_declare_property_string(ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
|
|
|
|
zend_declare_property_string(ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
|
|
|
|
zend_declare_property_long(ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
|
|
|
|
zend_declare_property_null(ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
|
|
|
|
zend_declare_property_null(ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
|
2020-05-28 10:23:37 +00:00
|
|
|
|
|
|
|
ZVAL_EMPTY_ARRAY(&val);
|
|
|
|
zend_declare_typed_property(
|
|
|
|
ce, ZSTR_KNOWN(ZEND_STR_TRACE), &val, ZEND_ACC_PRIVATE, NULL,
|
|
|
|
(zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY));
|
|
|
|
|
2020-05-28 12:01:29 +00:00
|
|
|
ZVAL_NULL(&val);
|
|
|
|
zend_declare_typed_property(
|
|
|
|
ce, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &val, ZEND_ACC_PRIVATE, NULL,
|
|
|
|
(zend_type) ZEND_TYPE_INIT_CE(zend_ce_throwable, /* allow_null */ 1, 0));
|
2020-05-28 10:13:08 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
void zend_register_default_exception(void) /* {{{ */
|
2003-03-23 04:32:24 +00:00
|
|
|
{
|
2003-08-30 11:40:37 +00:00
|
|
|
zend_class_entry ce;
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2015-06-17 18:46:27 +00:00
|
|
|
REGISTER_MAGIC_INTERFACE(throwable, Throwable);
|
2020-01-13 16:06:26 +00:00
|
|
|
zend_class_implements(zend_ce_throwable, 1, zend_ce_stringable);
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2018-05-31 08:57:22 +00:00
|
|
|
memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers));
|
2015-06-15 03:43:11 +00:00
|
|
|
default_exception_handlers.clone_obj = NULL;
|
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "Exception", class_Exception_methods);
|
2015-07-03 14:45:03 +00:00
|
|
|
zend_ce_exception = zend_register_internal_class_ex(&ce, NULL);
|
|
|
|
zend_ce_exception->create_object = zend_default_exception_new;
|
|
|
|
zend_class_implements(zend_ce_exception, 1, zend_ce_throwable);
|
2020-05-28 10:13:08 +00:00
|
|
|
declare_exception_properties(zend_ce_exception);
|
2004-07-15 22:21:36 +00:00
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "ErrorException", class_ErrorException_methods);
|
2015-07-03 14:45:03 +00:00
|
|
|
zend_ce_error_exception = zend_register_internal_class_ex(&ce, zend_ce_exception);
|
|
|
|
zend_ce_error_exception->create_object = zend_error_exception_new;
|
|
|
|
zend_declare_property_long(zend_ce_error_exception, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED);
|
2015-03-09 12:57:15 +00:00
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "Error", class_Error_methods);
|
2015-07-03 14:45:03 +00:00
|
|
|
zend_ce_error = zend_register_internal_class_ex(&ce, NULL);
|
|
|
|
zend_ce_error->create_object = zend_default_exception_new;
|
|
|
|
zend_class_implements(zend_ce_error, 1, zend_ce_throwable);
|
2020-05-28 10:13:08 +00:00
|
|
|
declare_exception_properties(zend_ce_error);
|
2015-03-18 16:23:09 +00:00
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "CompileError", class_CompileError_methods);
|
2017-09-24 13:24:51 +00:00
|
|
|
zend_ce_compile_error = zend_register_internal_class_ex(&ce, zend_ce_error);
|
|
|
|
zend_ce_compile_error->create_object = zend_default_exception_new;
|
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "ParseError", class_ParseError_methods);
|
2017-09-24 13:24:51 +00:00
|
|
|
zend_ce_parse_error = zend_register_internal_class_ex(&ce, zend_ce_compile_error);
|
2015-07-03 14:45:03 +00:00
|
|
|
zend_ce_parse_error->create_object = zend_default_exception_new;
|
2015-03-09 12:57:15 +00:00
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "TypeError", class_TypeError_methods);
|
2015-07-03 14:45:03 +00:00
|
|
|
zend_ce_type_error = zend_register_internal_class_ex(&ce, zend_ce_error);
|
|
|
|
zend_ce_type_error->create_object = zend_default_exception_new;
|
2015-07-01 23:54:08 +00:00
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "ArgumentCountError", class_ArgumentCountError_methods);
|
2016-08-30 19:09:26 +00:00
|
|
|
zend_ce_argument_count_error = zend_register_internal_class_ex(&ce, zend_ce_type_error);
|
|
|
|
zend_ce_argument_count_error->create_object = zend_default_exception_new;
|
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "ValueError", class_ValueError_methods);
|
2019-09-04 20:39:02 +00:00
|
|
|
zend_ce_value_error = zend_register_internal_class_ex(&ce, zend_ce_error);
|
|
|
|
zend_ce_value_error->create_object = zend_default_exception_new;
|
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "ArithmeticError", class_ArithmeticError_methods);
|
2015-07-03 14:45:03 +00:00
|
|
|
zend_ce_arithmetic_error = zend_register_internal_class_ex(&ce, zend_ce_error);
|
2015-07-02 18:47:44 +00:00
|
|
|
zend_ce_arithmetic_error->create_object = zend_default_exception_new;
|
|
|
|
|
2020-04-25 21:10:07 +00:00
|
|
|
INIT_CLASS_ENTRY(ce, "DivisionByZeroError", class_DivisionByZeroError_methods);
|
2015-07-02 18:47:44 +00:00
|
|
|
zend_ce_division_by_zero_error = zend_register_internal_class_ex(&ce, zend_ce_arithmetic_error);
|
2015-07-01 23:54:08 +00:00
|
|
|
zend_ce_division_by_zero_error->create_object = zend_default_exception_new;
|
2020-03-06 13:57:55 +00:00
|
|
|
|
|
|
|
INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL);
|
2020-04-09 20:36:37 +00:00
|
|
|
|
|
|
|
INIT_CLASS_ENTRY(ce, "UnhandledMatchError", NULL);
|
|
|
|
zend_ce_unhandled_match_error = zend_register_internal_class_ex(&ce, zend_ce_error);
|
|
|
|
zend_ce_unhandled_match_error->create_object = zend_default_exception_new;
|
2003-03-23 04:32:24 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2003-03-23 04:32:24 +00:00
|
|
|
|
2015-07-03 14:45:03 +00:00
|
|
|
/* {{{ Deprecated - Use zend_ce_exception directly instead */
|
2015-08-04 23:14:24 +00:00
|
|
|
ZEND_API zend_class_entry *zend_exception_get_default(void)
|
2015-03-09 12:57:15 +00:00
|
|
|
{
|
2015-07-03 14:45:03 +00:00
|
|
|
return zend_ce_exception;
|
2015-03-09 12:57:15 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-07-03 14:45:03 +00:00
|
|
|
/* {{{ Deprecated - Use zend_ce_error_exception directly instead */
|
2015-07-03 01:57:53 +00:00
|
|
|
ZEND_API zend_class_entry *zend_get_error_exception(void)
|
2015-03-18 16:23:09 +00:00
|
|
|
{
|
2015-07-03 14:45:03 +00:00
|
|
|
return zend_ce_error_exception;
|
2015-03-18 16:23:09 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2020-05-29 08:33:22 +00:00
|
|
|
static zend_object *zend_throw_exception_zstr(zend_class_entry *exception_ce, zend_string *message, zend_long code) /* {{{ */
|
2004-07-15 22:21:36 +00:00
|
|
|
{
|
2016-05-12 10:47:22 +00:00
|
|
|
zval ex, tmp;
|
2003-08-28 22:56:41 +00:00
|
|
|
|
2020-09-21 09:05:19 +00:00
|
|
|
if (!exception_ce) {
|
2015-07-03 14:45:03 +00:00
|
|
|
exception_ce = zend_ce_exception;
|
2003-08-28 22:56:41 +00:00
|
|
|
}
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2020-09-21 09:05:19 +00:00
|
|
|
ZEND_ASSERT(instanceof_function(exception_ce, zend_ce_throwable)
|
|
|
|
&& "Exceptions must implement Throwable");
|
|
|
|
|
|
|
|
object_init_ex(&ex, exception_ce);
|
2003-08-28 22:56:41 +00:00
|
|
|
|
|
|
|
if (message) {
|
2020-05-29 08:33:22 +00:00
|
|
|
ZVAL_STR(&tmp, message);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
|
2003-08-28 22:56:41 +00:00
|
|
|
}
|
|
|
|
if (code) {
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_LONG(&tmp, code);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
|
2003-08-28 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 16:45:07 +00:00
|
|
|
zend_throw_exception_internal(Z_OBJ(ex));
|
|
|
|
|
2014-02-10 06:04:30 +00:00
|
|
|
return Z_OBJ(ex);
|
2003-08-28 22:56:41 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2003-08-28 22:56:41 +00:00
|
|
|
|
2020-05-29 08:33:22 +00:00
|
|
|
ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
|
|
|
|
{
|
2020-06-06 06:47:39 +00:00
|
|
|
zend_string *msg_str = message ? zend_string_init(message, strlen(message), 0) : NULL;
|
2020-05-29 08:33:22 +00:00
|
|
|
zend_object *ex = zend_throw_exception_zstr(exception_ce, msg_str, code);
|
2020-06-06 06:47:39 +00:00
|
|
|
if (msg_str) {
|
|
|
|
zend_string_release(msg_str);
|
|
|
|
}
|
2020-05-29 08:33:22 +00:00
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-08-19 11:40:56 +00:00
|
|
|
ZEND_API ZEND_COLD zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
|
2003-08-21 23:32:13 +00:00
|
|
|
{
|
2004-07-15 22:21:36 +00:00
|
|
|
va_list arg;
|
|
|
|
char *message;
|
2014-02-10 06:04:30 +00:00
|
|
|
zend_object *obj;
|
2004-07-15 22:21:36 +00:00
|
|
|
|
2006-05-09 23:53:23 +00:00
|
|
|
va_start(arg, format);
|
2004-07-15 22:21:36 +00:00
|
|
|
zend_vspprintf(&message, 0, format, arg);
|
|
|
|
va_end(arg);
|
2014-12-13 22:06:14 +00:00
|
|
|
obj = zend_throw_exception(exception_ce, message, code);
|
2004-09-27 22:06:10 +00:00
|
|
|
efree(message);
|
2014-02-10 06:04:30 +00:00
|
|
|
return obj;
|
2003-08-21 23:32:13 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2003-08-21 23:32:13 +00:00
|
|
|
|
2020-05-29 08:33:22 +00:00
|
|
|
ZEND_API ZEND_COLD zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, zend_string *message, zend_long code, int severity) /* {{{ */
|
2004-07-15 22:21:36 +00:00
|
|
|
{
|
2020-08-07 13:27:57 +00:00
|
|
|
zval tmp;
|
2020-05-29 08:33:22 +00:00
|
|
|
zend_object *obj = zend_throw_exception_zstr(exception_ce, message, code);
|
2016-05-12 10:47:22 +00:00
|
|
|
ZVAL_LONG(&tmp, severity);
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(zend_ce_error_exception, obj, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
|
2014-02-10 06:04:30 +00:00
|
|
|
return obj;
|
2004-07-15 22:21:36 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2004-07-15 22:21:36 +00:00
|
|
|
|
2016-11-26 14:18:42 +00:00
|
|
|
static void zend_error_va(int type, const char *file, uint32_t lineno, const char *format, ...) /* {{{ */
|
2003-08-24 13:10:03 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2020-05-29 08:33:22 +00:00
|
|
|
zend_string *message = zend_vstrpprintf(0, format, args);
|
2020-09-14 12:05:54 +00:00
|
|
|
zend_observer_error_notify(type, file, lineno, message);
|
2020-05-29 08:33:22 +00:00
|
|
|
zend_error_cb(type, file, lineno, message);
|
|
|
|
zend_string_release(message);
|
2003-08-24 13:10:03 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2003-08-24 13:10:03 +00:00
|
|
|
|
2009-01-02 13:14:49 +00:00
|
|
|
/* This function doesn't return if it uses E_ERROR */
|
2020-08-28 13:41:27 +00:00
|
|
|
ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severity) /* {{{ */
|
2003-08-24 13:10:03 +00:00
|
|
|
{
|
2015-03-09 12:57:15 +00:00
|
|
|
zval exception, rv;
|
2014-02-10 06:04:30 +00:00
|
|
|
zend_class_entry *ce_exception;
|
2020-08-28 13:41:27 +00:00
|
|
|
zend_result result = FAILURE;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-02-10 06:04:30 +00:00
|
|
|
ZVAL_OBJ(&exception, ex);
|
2019-10-27 15:21:31 +00:00
|
|
|
ce_exception = ex->ce;
|
2015-03-09 12:57:15 +00:00
|
|
|
EG(exception) = NULL;
|
2017-09-24 13:24:51 +00:00
|
|
|
if (ce_exception == zend_ce_parse_error || ce_exception == zend_ce_compile_error) {
|
2016-05-12 10:47:22 +00:00
|
|
|
zend_string *message = zval_get_string(GET_PROPERTY(&exception, ZEND_STR_MESSAGE));
|
|
|
|
zend_string *file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
|
|
|
|
zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
|
2020-04-27 06:20:56 +00:00
|
|
|
int type = (ce_exception == zend_ce_parse_error ? E_PARSE : E_COMPILE_ERROR) | E_DONT_BAIL;
|
2015-03-09 12:57:15 +00:00
|
|
|
|
2020-09-14 12:05:54 +00:00
|
|
|
zend_observer_error_notify(type, ZSTR_VAL(file), line, message);
|
2020-04-27 06:20:56 +00:00
|
|
|
zend_error_cb(type, ZSTR_VAL(file), line, message);
|
2015-03-18 20:38:51 +00:00
|
|
|
|
2018-05-28 13:27:12 +00:00
|
|
|
zend_string_release_ex(file, 0);
|
|
|
|
zend_string_release_ex(message, 0);
|
2015-06-15 22:35:24 +00:00
|
|
|
} else if (instanceof_function(ce_exception, zend_ce_throwable)) {
|
2019-10-27 15:21:31 +00:00
|
|
|
zval tmp;
|
2014-10-06 18:14:30 +00:00
|
|
|
zend_string *str, *file = NULL;
|
|
|
|
zend_long line = 0;
|
2003-08-30 18:58:40 +00:00
|
|
|
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
|
2003-12-06 18:12:26 +00:00
|
|
|
if (!EG(exception)) {
|
2014-02-10 06:04:30 +00:00
|
|
|
if (Z_TYPE(tmp) != IS_STRING) {
|
2015-06-30 10:59:27 +00:00
|
|
|
zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
|
2003-12-06 18:12:26 +00:00
|
|
|
} else {
|
2020-08-07 13:27:57 +00:00
|
|
|
zend_update_property_ex(i_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
|
2003-12-06 18:12:26 +00:00
|
|
|
}
|
2003-12-05 01:44:45 +00:00
|
|
|
}
|
2014-02-10 06:04:30 +00:00
|
|
|
zval_ptr_dtor(&tmp);
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2003-11-29 17:03:45 +00:00
|
|
|
if (EG(exception)) {
|
2014-02-10 06:04:30 +00:00
|
|
|
zval zv;
|
|
|
|
|
|
|
|
ZVAL_OBJ(&zv, EG(exception));
|
2003-11-29 17:03:45 +00:00
|
|
|
/* do the best we can to inform about the inner exception */
|
2015-07-03 14:45:03 +00:00
|
|
|
if (instanceof_function(ce_exception, zend_ce_exception) || instanceof_function(ce_exception, zend_ce_error)) {
|
2016-05-12 10:47:22 +00:00
|
|
|
file = zval_get_string(GET_PROPERTY_SILENT(&zv, ZEND_STR_FILE));
|
|
|
|
line = zval_get_long(GET_PROPERTY_SILENT(&zv, ZEND_STR_LINE));
|
2014-10-06 18:14:30 +00:00
|
|
|
}
|
2013-05-12 12:00:32 +00:00
|
|
|
|
2015-06-30 10:59:27 +00:00
|
|
|
zend_error_va(E_WARNING, (file && ZSTR_LEN(file) > 0) ? ZSTR_VAL(file) : NULL, line,
|
2020-05-26 12:10:57 +00:00
|
|
|
"Uncaught %s in exception handling during call to %s::__toString()",
|
2015-06-30 10:59:27 +00:00
|
|
|
ZSTR_VAL(Z_OBJCE(zv)->name), ZSTR_VAL(ce_exception->name));
|
2014-10-06 18:14:30 +00:00
|
|
|
|
|
|
|
if (file) {
|
2018-05-28 13:27:12 +00:00
|
|
|
zend_string_release_ex(file, 0);
|
2003-11-29 17:03:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:47:22 +00:00
|
|
|
str = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_STRING));
|
|
|
|
file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
|
|
|
|
line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
|
2003-11-29 17:03:45 +00:00
|
|
|
|
2019-10-10 12:14:36 +00:00
|
|
|
zend_error_va(severity | E_DONT_BAIL,
|
|
|
|
(file && ZSTR_LEN(file) > 0) ? ZSTR_VAL(file) : NULL, line,
|
2015-08-18 11:59:37 +00:00
|
|
|
"Uncaught %s\n thrown", ZSTR_VAL(str));
|
2013-05-12 12:00:32 +00:00
|
|
|
|
2018-05-28 13:27:12 +00:00
|
|
|
zend_string_release_ex(str, 0);
|
|
|
|
zend_string_release_ex(file, 0);
|
2020-03-06 13:57:55 +00:00
|
|
|
} else if (ce_exception == &zend_ce_unwind_exit) {
|
2020-08-10 08:28:13 +00:00
|
|
|
/* We successfully unwound, nothing more to do.
|
|
|
|
* We still return FAILURE in this case, as further execution should still be aborted. */
|
2003-08-24 13:10:03 +00:00
|
|
|
} else {
|
2020-05-26 12:10:57 +00:00
|
|
|
zend_error(severity, "Uncaught exception %s", ZSTR_VAL(ce_exception->name));
|
2003-08-24 13:10:03 +00:00
|
|
|
}
|
2015-05-15 20:42:19 +00:00
|
|
|
|
|
|
|
OBJ_RELEASE(ex);
|
2020-03-06 13:57:55 +00:00
|
|
|
return result;
|
2003-08-24 13:10:03 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2003-08-24 13:10:03 +00:00
|
|
|
|
2015-08-19 11:40:56 +00:00
|
|
|
ZEND_API ZEND_COLD void zend_throw_exception_object(zval *exception) /* {{{ */
|
2004-02-12 10:24:40 +00:00
|
|
|
{
|
2006-05-09 23:53:23 +00:00
|
|
|
if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
|
2015-04-02 11:19:52 +00:00
|
|
|
zend_error_noreturn(E_CORE_ERROR, "Need to supply an object when throwing an exception");
|
2004-02-12 10:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 16:45:07 +00:00
|
|
|
zend_class_entry *exception_ce = Z_OBJCE_P(exception);
|
2004-02-12 10:24:40 +00:00
|
|
|
|
2015-05-17 22:31:06 +00:00
|
|
|
if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
|
2015-07-07 17:10:22 +00:00
|
|
|
zend_throw_error(NULL, "Cannot throw objects that do not implement Throwable");
|
2016-07-13 21:35:53 +00:00
|
|
|
zval_ptr_dtor(exception);
|
2015-04-02 11:19:52 +00:00
|
|
|
return;
|
2004-02-12 10:24:40 +00:00
|
|
|
}
|
2020-08-05 16:45:07 +00:00
|
|
|
|
|
|
|
zend_throw_exception_internal(Z_OBJ_P(exception));
|
2004-02-12 10:24:40 +00:00
|
|
|
}
|
2007-01-18 12:20:15 +00:00
|
|
|
/* }}} */
|
2020-03-06 13:57:55 +00:00
|
|
|
|
2020-07-22 16:59:00 +00:00
|
|
|
ZEND_API ZEND_COLD void zend_throw_unwind_exit(void)
|
2020-03-06 13:57:55 +00:00
|
|
|
{
|
|
|
|
ZEND_ASSERT(!EG(exception));
|
|
|
|
EG(exception) = zend_objects_new(&zend_ce_unwind_exit);
|
|
|
|
EG(opline_before_exception) = EG(current_execute_data)->opline;
|
|
|
|
EG(current_execute_data)->opline = EG(exception_op);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API zend_bool zend_is_unwind_exit(zend_object *ex)
|
|
|
|
{
|
|
|
|
return ex->ce == &zend_ce_unwind_exit;
|
|
|
|
}
|