diff --git a/NEWS b/NEWS index d54cc1c18ea4d..00a2dd67ead3d 100644 --- a/NEWS +++ b/NEWS @@ -26,6 +26,8 @@ PHP NEWS (srgoogleguy, Gustavo) . Implemented FR #60738 (Allow 'set_error_handler' to handle NULL). (Laruence, Nikita Popov) + . Return previous handler when passing NULL to set_error_handler and + set_exception_handler. (Nikita Popov) . Added optional second argument for assert() to specify custom message. Patch by Lonny Kapelushnik (lonny@lonnylot.com). (Lars) diff --git a/UPGRADING b/UPGRADING index 59dfbb436acd2..bc5773cfa89f8 100755 --- a/UPGRADING +++ b/UPGRADING @@ -107,6 +107,10 @@ PHP X.Y UPGRADE NOTES but that predated the existence of E_DEPRECATED. - php_logo_guid(), php_egg_logo_guid(), php_real_logo_guid() and zend_logo_guid() have been removed +- set_error_handler(NULL) can now be used to reset the error handler. + Furthermore both set_error_handler(NULL) and set_exception_handler(NULL) will + now return the previously defined error/exception handler. Previously + bool(true) was returned. ======================================== 5. New Functions diff --git a/Zend/tests/bug60738.phpt b/Zend/tests/bug60738.phpt index e0c9793fedf27..e4080715ec72a 100644 --- a/Zend/tests/bug60738.phpt +++ b/Zend/tests/bug60738.phpt @@ -3,15 +3,20 @@ Bug #60738 Allow 'set_error_handler' to handle NULL --FILE-- --EXPECTF-- +NULL Intercepted error! +object(Closure)#1 (0) { +} Notice: Error! in %s on line %d diff --git a/Zend/tests/bug60738_variation.phpt b/Zend/tests/bug60738_variation.phpt new file mode 100644 index 0000000000000..d7cf00ecdbc48 --- /dev/null +++ b/Zend/tests/bug60738_variation.phpt @@ -0,0 +1,23 @@ +--TEST-- +Bug #60738 Allow 'set_error_handler' to handle NULL +--FILE-- + +--EXPECTF-- +NULL +object(Closure)#1 (0) { +} + +Fatal error: Uncaught exception 'Exception' with message 'Exception!' in %s:%d +Stack trace: +#0 {main} + thrown in %s on line %d + diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index f8d467478c7f5..698ef72e544be 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -1519,41 +1519,31 @@ ZEND_FUNCTION(set_error_handler) return; } - if (IS_NULL != Z_TYPE_P(error_handler)) { - zend_bool had_orig_error_handler = 0; + if (Z_TYPE_P(error_handler) != IS_NULL) { /* NULL == unset */ if (!zend_is_callable(error_handler, 0, &error_handler_name TSRMLS_CC)) { zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback", - get_active_function_name(TSRMLS_C), error_handler_name?error_handler_name:"unknown"); + get_active_function_name(TSRMLS_C), error_handler_name?error_handler_name:"unknown"); efree(error_handler_name); return; } efree(error_handler_name); + } - if (EG(user_error_handler)) { - had_orig_error_handler = 1; - *return_value = *EG(user_error_handler); - zval_copy_ctor(return_value); - INIT_PZVAL(return_value); - zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting), sizeof(EG(user_error_handler_error_reporting))); - zend_ptr_stack_push(&EG(user_error_handlers), EG(user_error_handler)); - } - - ALLOC_ZVAL(EG(user_error_handler)); - EG(user_error_handler_error_reporting) = (int)error_type; - MAKE_COPY_ZVAL(&error_handler, EG(user_error_handler)); + if (EG(user_error_handler)) { + RETVAL_ZVAL(EG(user_error_handler), 1, 0); - if (!had_orig_error_handler) { - RETURN_NULL(); - } - } else { /* unset user-defined handler */ - if (EG(user_error_handler)) { - zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting), sizeof(EG(user_error_handler_error_reporting))); - zend_ptr_stack_push(&EG(user_error_handlers), EG(user_error_handler)); - } + zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting), sizeof(EG(user_error_handler_error_reporting))); + zend_ptr_stack_push(&EG(user_error_handlers), EG(user_error_handler)); + } + if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */ EG(user_error_handler) = NULL; - RETURN_TRUE; + return; } + + ALLOC_ZVAL(EG(user_error_handler)); + MAKE_COPY_ZVAL(&error_handler, EG(user_error_handler)); + EG(user_error_handler_error_reporting) = (int)error_type; } /* }}} */ @@ -1593,36 +1583,28 @@ ZEND_FUNCTION(set_exception_handler) } if (Z_TYPE_P(exception_handler) != IS_NULL) { /* NULL == unset */ - zend_bool had_orig_exception_handler = 0; - if (!zend_is_callable(exception_handler, 0, &exception_handler_name TSRMLS_CC)) { zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback", - get_active_function_name(TSRMLS_C), exception_handler_name?exception_handler_name:"unknown"); + get_active_function_name(TSRMLS_C), exception_handler_name?exception_handler_name:"unknown"); efree(exception_handler_name); return; } efree(exception_handler_name); + } - if (EG(user_exception_handler)) { - had_orig_exception_handler = 1; - *return_value = *EG(user_exception_handler); - zval_copy_ctor(return_value); - zend_ptr_stack_push(&EG(user_exception_handlers), EG(user_exception_handler)); - } + if (EG(user_exception_handler)) { + RETVAL_ZVAL(EG(user_exception_handler), 1, 0); - ALLOC_ZVAL(EG(user_exception_handler)); - MAKE_COPY_ZVAL(&exception_handler, EG(user_exception_handler)); + zend_ptr_stack_push(&EG(user_exception_handlers), EG(user_exception_handler)); + } - if (!had_orig_exception_handler) { - RETURN_NULL(); - } - } else { - if (EG(user_exception_handler)) { - zend_ptr_stack_push(&EG(user_exception_handlers), EG(user_exception_handler)); - } + if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */ EG(user_exception_handler) = NULL; - RETURN_TRUE; + return; } + + ALLOC_ZVAL(EG(user_exception_handler)); + MAKE_COPY_ZVAL(&exception_handler, EG(user_exception_handler)) } /* }}} */