Skip to content

Commit fcae164

Browse files
committed
Implemented FR #60738 (Allow 'set_error_handler' to handle NULL)
1 parent 9a87fe1 commit fcae164

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ PHP NEWS
2323
. Fixed bug #60825 (Segfault when running symfony 2 tests).
2424
(Dmitry, Laruence)
2525
. Fixed bug #60801 (strpbrk() mishandles NUL byte). (Adam)
26+
. Implemented FR #60738 (Allow 'set_error_handler' to handle NULL).
27+
(Laruence, Nikita Popov)
2628
. Fixed bug #60569 (Nullbyte truncates Exception $message). (Ilia)
2729
. Fixed bug #60227 (header() cannot detect the multi-line header with CR).
2830
(rui, Gustavo)

Zend/tests/bug60738.phpt

Lines changed: 17 additions & 0 deletions
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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,45 +1413,47 @@ ZEND_FUNCTION(trigger_error)
14131413
ZEND_FUNCTION(set_error_handler)
14141414
{
14151415
zval *error_handler;
1416-
zend_bool had_orig_error_handler=0;
14171416
char *error_handler_name = NULL;
14181417
long error_type = E_ALL | E_STRICT;
14191418

14201419
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &error_handler, &error_type) == FAILURE) {
14211420
return;
14221421
}
14231422

1424-
if (!zend_is_callable(error_handler, 0, &error_handler_name TSRMLS_CC)) {
1425-
zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1426-
get_active_function_name(TSRMLS_C), error_handler_name?error_handler_name:"unknown");
1423+
if (IS_NULL != Z_TYPE_P(error_handler)) {
1424+
zend_bool had_orig_error_handler = 0;
1425+
if (!zend_is_callable(error_handler, 0, &error_handler_name TSRMLS_CC)) {
1426+
zend_error(E_WARNING, "%s() expects the argument (%s) to be a valid callback",
1427+
get_active_function_name(TSRMLS_C), error_handler_name?error_handler_name:"unknown");
1428+
efree(error_handler_name);
1429+
return;
1430+
}
14271431
efree(error_handler_name);
1428-
return;
1429-
}
1430-
efree(error_handler_name);
14311432

1432-
if (EG(user_error_handler)) {
1433-
had_orig_error_handler = 1;
1434-
*return_value = *EG(user_error_handler);
1435-
zval_copy_ctor(return_value);
1436-
INIT_PZVAL(return_value);
1437-
zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting), sizeof(EG(user_error_handler_error_reporting)));
1438-
zend_ptr_stack_push(&EG(user_error_handlers), EG(user_error_handler));
1439-
}
1440-
ALLOC_ZVAL(EG(user_error_handler));
1433+
if (EG(user_error_handler)) {
1434+
had_orig_error_handler = 1;
1435+
*return_value = *EG(user_error_handler);
1436+
zval_copy_ctor(return_value);
1437+
INIT_PZVAL(return_value);
1438+
zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting), sizeof(EG(user_error_handler_error_reporting)));
1439+
zend_ptr_stack_push(&EG(user_error_handlers), EG(user_error_handler));
1440+
}
14411441

1442-
if (!zend_is_true(error_handler)) { /* unset user-defined handler */
1443-
FREE_ZVAL(EG(user_error_handler));
1444-
EG(user_error_handler) = NULL;
1445-
RETURN_TRUE;
1446-
}
1442+
ALLOC_ZVAL(EG(user_error_handler));
1443+
EG(user_error_handler_error_reporting) = (int)error_type;
1444+
MAKE_COPY_ZVAL(&error_handler, EG(user_error_handler));
14471445

1448-
EG(user_error_handler_error_reporting) = (int)error_type;
1449-
*EG(user_error_handler) = *error_handler;
1450-
zval_copy_ctor(EG(user_error_handler));
1451-
INIT_PZVAL(EG(user_error_handler));
1446+
if (!had_orig_error_handler) {
1447+
RETURN_NULL();
1448+
}
1449+
} else { /* unset user-defined handler */
1450+
if (EG(user_error_handler)) {
1451+
zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting), sizeof(EG(user_error_handler_error_reporting)));
1452+
zend_ptr_stack_push(&EG(user_error_handlers), EG(user_error_handler));
1453+
}
14521454

1453-
if (!had_orig_error_handler) {
1454-
RETURN_NULL();
1455+
EG(user_error_handler) = NULL;
1456+
RETURN_TRUE;
14551457
}
14561458
}
14571459
/* }}} */

0 commit comments

Comments
 (0)