Skip to content

Improve error and exception handlers #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Zend/tests/bug60738.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Bug #60738 Allow 'set_error_handler' to handle NULL
--FILE--
<?php

var_dump(set_error_handler(
function() { echo 'Intercepted error!', "\n"; }
));

trigger_error('Error!');

var_dump(set_error_handler(null));

trigger_error('Error!');
?>
--EXPECTF--
NULL
Intercepted error!
object(Closure)#1 (0) {
}

Notice: Error! in %s on line %d
23 changes: 23 additions & 0 deletions Zend/tests/bug60738_variation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Bug #60738 Allow 'set_error_handler' to handle NULL
--FILE--
<?php

var_dump(set_exception_handler(
function() { echo 'Intercepted exception!', "\n"; }
));

var_dump(set_exception_handler(null));

throw new Exception('Exception!');
?>
--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

49 changes: 17 additions & 32 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,46 +1413,38 @@ 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 | E_STRICT;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &error_handler, &error_type) == FAILURE) {
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;
*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 (!zend_is_true(error_handler)) { /* unset user-defined handler */
FREE_ZVAL(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;
*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();
}
}
/* }}} */

Expand Down Expand Up @@ -1486,7 +1478,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;
Expand All @@ -1503,24 +1494,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_TRUE;
return;
}

ALLOC_ZVAL(EG(user_exception_handler));
MAKE_COPY_ZVAL(&exception_handler, EG(user_exception_handler))

if (!had_orig_exception_handler) {
RETURN_NULL();
}
}
/* }}} */

Expand Down