From bd1d11d352ddfb2c376060d7e7b282636dce1605 Mon Sep 17 00:00:00 2001 From: twosee Date: Thu, 10 Sep 2020 17:36:04 +0800 Subject: [PATCH] Simplify error type filter Closes GH-6049. --- Zend/zend.c | 39 ++++++++++++++----------------------- ext/soap/soap.c | 14 ++----------- main/main.c | 30 +++++++++++----------------- sapi/cli/php_cli_server.c | 18 ++++++----------- sapi/phpdbg/phpdbg.c | 41 ++++++++++++++++----------------------- 5 files changed, 51 insertions(+), 91 deletions(-) diff --git a/Zend/zend.c b/Zend/zend.c index 88530faa064..2a834eb6c45 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -1311,30 +1311,21 @@ static ZEND_COLD void zend_error_impl( zend_execute_data *ex; const zend_op *opline; - switch (type) { - case E_CORE_ERROR: - case E_ERROR: - case E_RECOVERABLE_ERROR: - case E_PARSE: - case E_COMPILE_ERROR: - case E_USER_ERROR: - ex = EG(current_execute_data); - opline = NULL; - while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) { - ex = ex->prev_execute_data; - } - if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION && - EG(opline_before_exception)) { - opline = EG(opline_before_exception); - } - zend_exception_error(EG(exception), E_WARNING); - EG(exception) = NULL; - if (opline) { - ex->opline = opline; - } - break; - default: - break; + if (type & E_FATAL_ERRORS) { + ex = EG(current_execute_data); + opline = NULL; + while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) { + ex = ex->prev_execute_data; + } + if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION && + EG(opline_before_exception)) { + opline = EG(opline_before_exception); + } + zend_exception_error(EG(exception), E_WARNING); + EG(exception) = NULL; + if (opline) { + ex->opline = opline; + } } } diff --git a/ext/soap/soap.c b/ext/soap/soap.c index 18dae727330..387beb78e57 100644 --- a/ext/soap/soap.c +++ b/ext/soap/soap.c @@ -1843,12 +1843,7 @@ static zend_never_inline ZEND_COLD void soap_real_error_handler(int error_num, c use_exceptions = 1; } - if ((error_num == E_USER_ERROR || - error_num == E_COMPILE_ERROR || - error_num == E_CORE_ERROR || - error_num == E_ERROR || - error_num == E_PARSE) && - use_exceptions) { + if ((error_num & E_FATAL_ERRORS) && use_exceptions) { zval fault; char *code = SOAP_GLOBAL(error_code); if (code == NULL) { @@ -1870,12 +1865,7 @@ static zend_never_inline ZEND_COLD void soap_real_error_handler(int error_num, c int fault = 0; zval fault_obj; - if (error_num == E_USER_ERROR || - error_num == E_COMPILE_ERROR || - error_num == E_CORE_ERROR || - error_num == E_ERROR || - error_num == E_PARSE) { - + if (error_num & E_FATAL_ERRORS) { char* code = SOAP_GLOBAL(error_code); zend_string *buffer; zval outbuf; diff --git a/main/main.c b/main/main.c index 9eb610fe644..890116e45c8 100644 --- a/main/main.c +++ b/main/main.c @@ -1190,30 +1190,22 @@ static ZEND_COLD void php_error_cb(int orig_type, const char *error_filename, co /* according to error handling mode, throw exception or show it */ if (EG(error_handling) == EH_THROW) { switch (type) { - case E_ERROR: - case E_CORE_ERROR: - case E_COMPILE_ERROR: - case E_USER_ERROR: - case E_PARSE: - /* fatal errors are real errors and cannot be made exceptions */ - break; - case E_STRICT: - case E_DEPRECATED: - case E_USER_DEPRECATED: - /* for the sake of BC to old damaged code */ - break; - case E_NOTICE: - case E_USER_NOTICE: - /* notices are no errors and are not treated as such like E_WARNINGS */ - break; - default: - /* throw an exception if we are in EH_THROW mode - * but DO NOT overwrite a pending exception + case E_WARNING: + case E_CORE_WARNING: + case E_COMPILE_WARNING: + case E_USER_WARNING: + /* throw an exception if we are in EH_THROW mode and the type is warning. + * fatal errors are real errors and cannot be made exceptions. + * exclude deprecated for the sake of BC to old damaged code. + * notices are no errors and are not treated as such like E_WARNINGS. + * DO NOT overwrite a pending exception. */ if (!EG(exception)) { zend_throw_error_exception(EG(exception_class), message, 0, type); } return; + default: + break; } } diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 64e29e8524a..337886bcd34 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -1177,19 +1177,13 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu zend_bool append_error_message = 0; if (PG(last_error_message)) { - switch (PG(last_error_type)) { - case E_ERROR: - case E_CORE_ERROR: - case E_COMPILE_ERROR: - case E_USER_ERROR: - case E_PARSE: - if (status == 200) { - /* the status code isn't changed by a fatal error, so fake it */ - effective_status = 500; - } + if (PG(last_error_type) & E_FATAL_ERRORS) { + if (status == 200) { + /* the status code isn't changed by a fatal error, so fake it */ + effective_status = 500; + } - append_error_message = 1; - break; + append_error_message = 1; } } diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index d817349941b..4629f94986f 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -798,32 +798,25 @@ static void php_sapi_phpdbg_log_message(const char *message, int syslog_type_int return; } - switch (PG(last_error_type)) { - case E_ERROR: - case E_CORE_ERROR: - case E_COMPILE_ERROR: - case E_USER_ERROR: - case E_PARSE: - case E_RECOVERABLE_ERROR: { - const char *file_char = zend_get_executed_filename(); - zend_string *file = zend_string_init(file_char, strlen(file_char), 0); - phpdbg_list_file(file, 3, zend_get_executed_lineno() - 1, zend_get_executed_lineno()); - zend_string_release(file); + if (PG(last_error_type) & E_FATAL_ERRORS) { + const char *file_char = zend_get_executed_filename(); + zend_string *file = zend_string_init(file_char, strlen(file_char), 0); + phpdbg_list_file(file, 3, zend_get_executed_lineno() - 1, zend_get_executed_lineno()); + zend_string_release(file); - if (!phpdbg_fully_started) { - return; - } - - do { - switch (phpdbg_interactive(1, NULL)) { - case PHPDBG_LEAVE: - case PHPDBG_FINISH: - case PHPDBG_UNTIL: - case PHPDBG_NEXT: - return; - } - } while (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)); + if (!phpdbg_fully_started) { + return; } + + do { + switch (phpdbg_interactive(1, NULL)) { + case PHPDBG_LEAVE: + case PHPDBG_FINISH: + case PHPDBG_UNTIL: + case PHPDBG_NEXT: + return; + } + } while (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)); } } else { fprintf(stdout, "%s\n", message);