From 4954aba2edbc4e29b8b18837298016b435ff7968 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 22 Sep 2012 21:41:51 +0200 Subject: [PATCH 1/5] Revert error/exception handler changes This reverts the following two commits: * 6ba2e662e447f369c6e7686e8b39dde033fd5334 * d8f8e98d8e0493adf1fae622595bd3435bdbf835 Laruence already did some partial changes to set_error_handler and set_exception_handler. I'm reverting those modifications to apply the full set of changes. (The modifications changed the code structure in a way that would lead to more duplication with the new behavior.) --- Zend/tests/bug60738.phpt | 17 ------- Zend/zend_builtin_functions.c | 93 +++++++++++++++++------------------ 2 files changed, 45 insertions(+), 65 deletions(-) delete mode 100644 Zend/tests/bug60738.phpt diff --git a/Zend/tests/bug60738.phpt b/Zend/tests/bug60738.phpt deleted file mode 100644 index e0c9793fedf27..0000000000000 --- a/Zend/tests/bug60738.phpt +++ /dev/null @@ -1,17 +0,0 @@ ---TEST-- -Bug #60738 Allow 'set_error_handler' to handle NULL ---FILE-- - ---EXPECTF-- -Intercepted error! - -Notice: Error! in %s on line %d diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index f8d467478c7f5..eab98ed944e77 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -1512,6 +1512,7 @@ ZEND_FUNCTION(trigger_error) ZEND_FUNCTION(set_error_handler) { zval *error_handler; + zend_bool had_orig_error_handler=0; char *error_handler_name = NULL; long error_type = E_ALL; @@ -1519,41 +1520,38 @@ ZEND_FUNCTION(set_error_handler) return; } - if (IS_NULL != Z_TYPE_P(error_handler)) { - zend_bool had_orig_error_handler = 0; - 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"); - efree(error_handler_name); - return; - } + 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"); 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 (!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)); - } + 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)); + if (!zend_is_true(error_handler)) { /* unset user-defined handler */ + FREE_ZVAL(EG(user_error_handler)); EG(user_error_handler) = NULL; RETURN_TRUE; } + + EG(user_error_handler_error_reporting) = (int)error_type; + *EG(user_error_handler) = *error_handler; + zval_copy_ctor(EG(user_error_handler)); + INIT_PZVAL(EG(user_error_handler)); + + if (!had_orig_error_handler) { + RETURN_NULL(); + } } /* }}} */ @@ -1587,42 +1585,41 @@ ZEND_FUNCTION(set_exception_handler) { zval *exception_handler; char *exception_handler_name = NULL; + zend_bool had_orig_exception_handler=0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &exception_handler) == FAILURE) { return; } 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)); - } - - ALLOC_ZVAL(EG(user_exception_handler)); - MAKE_COPY_ZVAL(&exception_handler, EG(user_exception_handler)); + 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)); + } + ALLOC_ZVAL(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 */ + FREE_ZVAL(EG(user_exception_handler)); EG(user_exception_handler) = NULL; RETURN_TRUE; } + + MAKE_COPY_ZVAL(&exception_handler, EG(user_exception_handler)) + + if (!had_orig_exception_handler) { + RETURN_NULL(); + } } /* }}} */ From c815dd74bc42c8f36ba35b910f45e85a645d7e3d Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 24 Mar 2012 12:52:15 +0100 Subject: [PATCH 2/5] Allow resetting the error handler This allows the error handler to be reset using set_error_handler(null). As the code suggests this behavior was already previously intended, but the callback check was done too strictly. --- Zend/tests/bug60738.phpt | 17 +++++++++++++++++ Zend/zend_builtin_functions.c | 14 ++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 Zend/tests/bug60738.phpt diff --git a/Zend/tests/bug60738.phpt b/Zend/tests/bug60738.phpt new file mode 100644 index 0000000000000..e0c9793fedf27 --- /dev/null +++ b/Zend/tests/bug60738.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #60738 Allow 'set_error_handler' to handle NULL +--FILE-- + +--EXPECTF-- +Intercepted error! + +Notice: Error! in %s on line %d diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index eab98ed944e77..204c7d3d0b889 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -1520,13 +1520,15 @@ ZEND_FUNCTION(set_error_handler) return; } - 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"); + 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"); + efree(error_handler_name); + return; + } efree(error_handler_name); - return; } - efree(error_handler_name); if (EG(user_error_handler)) { had_orig_error_handler = 1; @@ -1538,7 +1540,7 @@ ZEND_FUNCTION(set_error_handler) } ALLOC_ZVAL(EG(user_error_handler)); - if (!zend_is_true(error_handler)) { /* unset user-defined handler */ + if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */ FREE_ZVAL(EG(user_error_handler)); EG(user_error_handler) = NULL; RETURN_TRUE; From f28c128b207ae2c6ea4d8f6c2e66f5b709210a23 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 24 Mar 2012 13:10:51 +0100 Subject: [PATCH 3/5] Return previous error handler when resetting the error handler set_error_handler(null) and set_exception_handler(null) now return the previous error/exception handler instead of just returning bool(true). This is consistent with the behavior of these functions with non-null values. --- Zend/tests/bug60738.phpt | 9 +++++++-- Zend/tests/bug60738_variation.phpt | 23 +++++++++++++++++++++++ Zend/zend_builtin_functions.c | 4 ++-- 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 Zend/tests/bug60738_variation.phpt 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 204c7d3d0b889..fdfe3db8f83b9 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -1543,7 +1543,7 @@ ZEND_FUNCTION(set_error_handler) if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */ FREE_ZVAL(EG(user_error_handler)); EG(user_error_handler) = NULL; - RETURN_TRUE; + return; } EG(user_error_handler_error_reporting) = (int)error_type; @@ -1614,7 +1614,7 @@ ZEND_FUNCTION(set_exception_handler) if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */ FREE_ZVAL(EG(user_exception_handler)); EG(user_exception_handler) = NULL; - RETURN_TRUE; + return; } MAKE_COPY_ZVAL(&exception_handler, EG(user_exception_handler)) From 5c7dd7811ef34c8cbe8bdad333678809f649d383 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 24 Mar 2012 13:48:11 +0100 Subject: [PATCH 4/5] Simplify set_error_handler/set_exception_handler code --- Zend/zend_builtin_functions.c | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index fdfe3db8f83b9..698ef72e544be 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -1512,7 +1512,6 @@ ZEND_FUNCTION(trigger_error) ZEND_FUNCTION(set_error_handler) { zval *error_handler; - zend_bool had_orig_error_handler=0; char *error_handler_name = NULL; long error_type = E_ALL; @@ -1531,29 +1530,20 @@ ZEND_FUNCTION(set_error_handler) } 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); + RETVAL_ZVAL(EG(user_error_handler), 1, 0); + 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)); if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */ - FREE_ZVAL(EG(user_error_handler)); EG(user_error_handler) = NULL; 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; - *EG(user_error_handler) = *error_handler; - zval_copy_ctor(EG(user_error_handler)); - INIT_PZVAL(EG(user_error_handler)); - - if (!had_orig_error_handler) { - RETURN_NULL(); - } } /* }}} */ @@ -1587,7 +1577,6 @@ ZEND_FUNCTION(set_exception_handler) { zval *exception_handler; char *exception_handler_name = NULL; - zend_bool had_orig_exception_handler=0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &exception_handler) == FAILURE) { return; @@ -1604,24 +1593,18 @@ ZEND_FUNCTION(set_exception_handler) } if (EG(user_exception_handler)) { - had_orig_exception_handler = 1; - *return_value = *EG(user_exception_handler); - zval_copy_ctor(return_value); + RETVAL_ZVAL(EG(user_exception_handler), 1, 0); + zend_ptr_stack_push(&EG(user_exception_handlers), EG(user_exception_handler)); } - ALLOC_ZVAL(EG(user_exception_handler)); if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */ - FREE_ZVAL(EG(user_exception_handler)); EG(user_exception_handler) = NULL; return; } + ALLOC_ZVAL(EG(user_exception_handler)); MAKE_COPY_ZVAL(&exception_handler, EG(user_exception_handler)) - - if (!had_orig_exception_handler) { - RETURN_NULL(); - } } /* }}} */ From 5de79f9f0717d147b02033e270bea20561707db8 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 22 Sep 2012 21:54:59 +0200 Subject: [PATCH 5/5] Add NEWS/UPGRADING for previous change --- NEWS | 2 ++ UPGRADING | 4 ++++ 2 files changed, 6 insertions(+) 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