Skip to content

Commit c815dd7

Browse files
committed
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.
1 parent 4954aba commit c815dd7

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

Zend/tests/bug60738.phpt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #60738 Allow 'set_error_handler' to handle NULL
3+
--FILE--
4+
<?php
5+
6+
set_error_handler(function() { echo 'Intercepted error!', "\n"; });
7+
8+
trigger_error('Error!');
9+
10+
set_error_handler(null);
11+
12+
trigger_error('Error!');
13+
?>
14+
--EXPECTF--
15+
Intercepted error!
16+
17+
Notice: Error! in %s on line %d

Zend/zend_builtin_functions.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -1520,13 +1520,15 @@ ZEND_FUNCTION(set_error_handler)
15201520
return;
15211521
}
15221522

1523-
if (!zend_is_callable(error_handler, 0, &error_handler_name TSRMLS_CC)) {
1524-
zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1525-
get_active_function_name(TSRMLS_C), error_handler_name?error_handler_name:"unknown");
1523+
if (Z_TYPE_P(error_handler) != IS_NULL) { /* NULL == unset */
1524+
if (!zend_is_callable(error_handler, 0, &error_handler_name TSRMLS_CC)) {
1525+
zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1526+
get_active_function_name(TSRMLS_C), error_handler_name?error_handler_name:"unknown");
1527+
efree(error_handler_name);
1528+
return;
1529+
}
15261530
efree(error_handler_name);
1527-
return;
15281531
}
1529-
efree(error_handler_name);
15301532

15311533
if (EG(user_error_handler)) {
15321534
had_orig_error_handler = 1;
@@ -1538,7 +1540,7 @@ ZEND_FUNCTION(set_error_handler)
15381540
}
15391541
ALLOC_ZVAL(EG(user_error_handler));
15401542

1541-
if (!zend_is_true(error_handler)) { /* unset user-defined handler */
1543+
if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */
15421544
FREE_ZVAL(EG(user_error_handler));
15431545
EG(user_error_handler) = NULL;
15441546
RETURN_TRUE;

0 commit comments

Comments
 (0)