Skip to content

Commit 7ac9e9b

Browse files
committed
Use ZPP callable check in zend built in functions
1 parent 5191724 commit 7ac9e9b

File tree

5 files changed

+45
-33
lines changed

5 files changed

+45
-33
lines changed

Zend/tests/exception_handler_004.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ try {
1616

1717
?>
1818
--EXPECT--
19-
set_exception_handler(): Argument #1 ($exception_handler) must be a valid callback
20-
set_exception_handler(): Argument #1 ($exception_handler) must be a valid callback
19+
set_exception_handler(): Argument #1 ($exception_handler) must be a valid callback or null, function 'fo' not found or invalid function name
20+
set_exception_handler(): Argument #1 ($exception_handler) must be a valid callback or null, class '' not found

Zend/tests/exception_handler_007.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
exception handler tests - 7
3+
--FILE--
4+
<?php
5+
6+
set_exception_handler("foo");
7+
set_exception_handler(null);
8+
9+
function foo($e) {
10+
var_dump(__FUNCTION__."(): ".get_class($e)." thrown!");
11+
}
12+
13+
function foo1($e) {
14+
var_dump(__FUNCTION__."(): ".get_class($e)." thrown!");
15+
}
16+
17+
18+
throw new excEption();
19+
20+
echo "Done\n";
21+
?>
22+
--EXPECTF--
23+
Fatal error: Uncaught Exception in %s:%d
24+
Stack trace:
25+
#0 {main}
26+
thrown in %s on line %d

Zend/zend_builtin_functions.c

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,35 +1179,28 @@ ZEND_FUNCTION(trigger_error)
11791179
/* {{{ Sets a user-defined error handler function. Returns the previously defined error handler, or false on error */
11801180
ZEND_FUNCTION(set_error_handler)
11811181
{
1182-
zval *error_handler;
1182+
zend_fcall_info fci;
1183+
zend_fcall_info_cache fcc;
11831184
zend_long error_type = E_ALL;
11841185

1185-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &error_handler, &error_type) == FAILURE) {
1186+
/* callable argument corresponds to the error handler */
1187+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "f!|l", &fci, &fcc, &error_type) == FAILURE) {
11861188
RETURN_THROWS();
11871189
}
11881190

1189-
if (Z_TYPE_P(error_handler) != IS_NULL) { /* NULL == unset */
1190-
if (!zend_is_callable(error_handler, 0, NULL)) {
1191-
zend_string *error_handler_name = zend_get_callable_name(error_handler);
1192-
zend_argument_type_error(1, "must be a valid callback");
1193-
zend_string_release_ex(error_handler_name, 0);
1194-
RETURN_THROWS();
1195-
}
1196-
}
1197-
11981191
if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
11991192
ZVAL_COPY(return_value, &EG(user_error_handler));
12001193
}
12011194

12021195
zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
12031196
zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
12041197

1205-
if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */
1198+
if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
12061199
ZVAL_UNDEF(&EG(user_error_handler));
12071200
return;
12081201
}
12091202

1210-
ZVAL_COPY(&EG(user_error_handler), error_handler);
1203+
ZVAL_COPY(&EG(user_error_handler), &(fci.function_name));
12111204
EG(user_error_handler_error_reporting) = (int)error_type;
12121205
}
12131206
/* }}} */
@@ -1244,33 +1237,26 @@ ZEND_FUNCTION(restore_error_handler)
12441237
/* {{{ Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
12451238
ZEND_FUNCTION(set_exception_handler)
12461239
{
1247-
zval *exception_handler;
1240+
zend_fcall_info fci;
1241+
zend_fcall_info_cache fcc;
12481242

1249-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &exception_handler) == FAILURE) {
1243+
/* callable argument corresponds to the exception handler */
1244+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "f!", &fci, &fcc) == FAILURE) {
12501245
RETURN_THROWS();
12511246
}
12521247

1253-
if (Z_TYPE_P(exception_handler) != IS_NULL) { /* NULL == unset */
1254-
if (!zend_is_callable(exception_handler, 0, NULL)) {
1255-
zend_string *exception_handler_name = zend_get_callable_name(exception_handler);
1256-
zend_argument_type_error(1, "must be a valid callback");
1257-
zend_string_release_ex(exception_handler_name, 0);
1258-
RETURN_THROWS();
1259-
}
1260-
}
1261-
12621248
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
12631249
ZVAL_COPY(return_value, &EG(user_exception_handler));
12641250
}
12651251

12661252
zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
12671253

1268-
if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */
1254+
if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
12691255
ZVAL_UNDEF(&EG(user_exception_handler));
12701256
return;
12711257
}
12721258

1273-
ZVAL_COPY(&EG(user_exception_handler), exception_handler);
1259+
ZVAL_COPY(&EG(user_exception_handler), &(fci.function_name));
12741260
}
12751261
/* }}} */
12761262

Zend/zend_builtin_functions.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ function trigger_error(string $message, int $error_type = E_USER_NOTICE): bool {
6969
function user_error(string $message, int $error_type = E_USER_NOTICE): bool {}
7070

7171
/** @return string|array|object|null */
72-
function set_error_handler($error_handler, int $error_types = E_ALL) {}
72+
function set_error_handler(?callable $error_handler, int $error_types = E_ALL) {}
7373

7474
function restore_error_handler(): bool {}
7575

7676
/** @return string|array|object|null */
77-
function set_exception_handler($exception_handler) {}
77+
function set_exception_handler(?callable $exception_handler) {}
7878

7979
function restore_exception_handler(): bool {}
8080

Zend/zend_builtin_functions_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: f81f2b4cf552c4ee8406b91c437797feb1164be0 */
2+
* Stub hash: 0d3c035fc2b9f0dcdbf6efe3c740d8aa3805ec32 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_version, 0, 0, IS_STRING, 0)
55
ZEND_END_ARG_INFO()
@@ -127,15 +127,15 @@ ZEND_END_ARG_INFO()
127127
#define arginfo_user_error arginfo_trigger_error
128128

129129
ZEND_BEGIN_ARG_INFO_EX(arginfo_set_error_handler, 0, 0, 1)
130-
ZEND_ARG_INFO(0, error_handler)
130+
ZEND_ARG_TYPE_INFO(0, error_handler, IS_CALLABLE, 1)
131131
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, error_types, IS_LONG, 0, "E_ALL")
132132
ZEND_END_ARG_INFO()
133133

134134
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_restore_error_handler, 0, 0, _IS_BOOL, 0)
135135
ZEND_END_ARG_INFO()
136136

137137
ZEND_BEGIN_ARG_INFO_EX(arginfo_set_exception_handler, 0, 0, 1)
138-
ZEND_ARG_INFO(0, exception_handler)
138+
ZEND_ARG_TYPE_INFO(0, exception_handler, IS_CALLABLE, 1)
139139
ZEND_END_ARG_INFO()
140140

141141
#define arginfo_restore_exception_handler arginfo_restore_error_handler

0 commit comments

Comments
 (0)