]> granicus.if.org Git - php/commitdiff
Use ZPP callable check in zend built in functions
authorGeorge Peter Banyard <girgias@php.net>
Wed, 8 Jul 2020 16:36:46 +0000 (18:36 +0200)
committerGeorge Peter Banyard <girgias@php.net>
Wed, 8 Jul 2020 17:38:46 +0000 (19:38 +0200)
Zend/tests/exception_handler_004.phpt
Zend/tests/exception_handler_007.phpt [new file with mode: 0644]
Zend/zend_builtin_functions.c
Zend/zend_builtin_functions.stub.php
Zend/zend_builtin_functions_arginfo.h

index f07970d3377f7ac64dde18f29bb6263a50eaff59..6935c2744a7335d1bfa4f8da073a3c443a31681e 100644 (file)
@@ -16,5 +16,5 @@ try {
 
 ?>
 --EXPECT--
-set_exception_handler(): Argument #1 ($exception_handler) must be a valid callback
-set_exception_handler(): Argument #1 ($exception_handler) must be a valid callback
+set_exception_handler(): Argument #1 ($exception_handler) must be a valid callback or null, function 'fo' not found or invalid function name
+set_exception_handler(): Argument #1 ($exception_handler) must be a valid callback or null, class '' not found
diff --git a/Zend/tests/exception_handler_007.phpt b/Zend/tests/exception_handler_007.phpt
new file mode 100644 (file)
index 0000000..757154d
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+exception handler tests - 7
+--FILE--
+<?php
+
+set_exception_handler("foo");
+set_exception_handler(null);
+
+function foo($e) {
+    var_dump(__FUNCTION__."(): ".get_class($e)." thrown!");
+}
+
+function foo1($e) {
+    var_dump(__FUNCTION__."(): ".get_class($e)." thrown!");
+}
+
+
+throw new excEption();
+
+echo "Done\n";
+?>
+--EXPECTF--
+Fatal error: Uncaught Exception in %s:%d
+Stack trace:
+#0 {main}
+  thrown in %s on line %d
index f7662273433f83527aaa13ccb22eff116f12aeb4..4a3c2f4bbf556478c560fe39e02e716e72c2b13e 100644 (file)
@@ -1179,22 +1179,15 @@ ZEND_FUNCTION(trigger_error)
 /* {{{ Sets a user-defined error handler function.  Returns the previously defined error handler, or false on error */
 ZEND_FUNCTION(set_error_handler)
 {
-       zval *error_handler;
+       zend_fcall_info fci;
+       zend_fcall_info_cache fcc;
        zend_long error_type = E_ALL;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &error_handler, &error_type) == FAILURE) {
+       /* callable argument corresponds to the error handler */
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "f!|l", &fci, &fcc, &error_type) == FAILURE) {
                RETURN_THROWS();
        }
 
-       if (Z_TYPE_P(error_handler) != IS_NULL) { /* NULL == unset */
-               if (!zend_is_callable(error_handler, 0, NULL)) {
-                       zend_string *error_handler_name = zend_get_callable_name(error_handler);
-                       zend_argument_type_error(1, "must be a valid callback");
-                       zend_string_release_ex(error_handler_name, 0);
-                       RETURN_THROWS();
-               }
-       }
-
        if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
                ZVAL_COPY(return_value, &EG(user_error_handler));
        }
@@ -1202,12 +1195,12 @@ ZEND_FUNCTION(set_error_handler)
        zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
        zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
 
-       if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */
+       if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
                ZVAL_UNDEF(&EG(user_error_handler));
                return;
        }
 
-       ZVAL_COPY(&EG(user_error_handler), error_handler);
+       ZVAL_COPY(&EG(user_error_handler), &(fci.function_name));
        EG(user_error_handler_error_reporting) = (int)error_type;
 }
 /* }}} */
@@ -1244,33 +1237,26 @@ ZEND_FUNCTION(restore_error_handler)
 /* {{{ Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
 ZEND_FUNCTION(set_exception_handler)
 {
-       zval *exception_handler;
+       zend_fcall_info fci;
+       zend_fcall_info_cache fcc;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &exception_handler) == FAILURE) {
+       /* callable argument corresponds to the exception handler */
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "f!", &fci, &fcc) == FAILURE) {
                RETURN_THROWS();
        }
 
-       if (Z_TYPE_P(exception_handler) != IS_NULL) { /* NULL == unset */
-               if (!zend_is_callable(exception_handler, 0, NULL)) {
-               zend_string *exception_handler_name = zend_get_callable_name(exception_handler);
-                       zend_argument_type_error(1, "must be a valid callback");
-                       zend_string_release_ex(exception_handler_name, 0);
-                       RETURN_THROWS();
-               }
-       }
-
        if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
                ZVAL_COPY(return_value, &EG(user_exception_handler));
        }
 
        zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
 
-       if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */
+       if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
                ZVAL_UNDEF(&EG(user_exception_handler));
                return;
        }
 
-       ZVAL_COPY(&EG(user_exception_handler), exception_handler);
+       ZVAL_COPY(&EG(user_exception_handler), &(fci.function_name));
 }
 /* }}} */
 
index 18b64bfa0343dd5fc21421a510a476561f11ca66..8e1bb0b94cea548d35c4c029ec489a2aa669ab72 100644 (file)
@@ -69,12 +69,12 @@ function trigger_error(string $message, int $error_type = E_USER_NOTICE): bool {
 function user_error(string $message, int $error_type = E_USER_NOTICE): bool {}
 
 /** @return string|array|object|null */
-function set_error_handler($error_handler, int $error_types = E_ALL) {}
+function set_error_handler(?callable $error_handler, int $error_types = E_ALL) {}
 
 function restore_error_handler(): bool {}
 
 /** @return string|array|object|null */
-function set_exception_handler($exception_handler) {}
+function set_exception_handler(?callable $exception_handler) {}
 
 function restore_exception_handler(): bool {}
 
index 1964c121c978e9edea496db099cfa5110873f169..1fa189410c791eb813591bd0d4626ea5a78db104 100644 (file)
@@ -1,5 +1,5 @@
 /* This is a generated file, edit the .stub.php file instead.
- * Stub hash: f81f2b4cf552c4ee8406b91c437797feb1164be0 */
+ * Stub hash: 0d3c035fc2b9f0dcdbf6efe3c740d8aa3805ec32 */
 
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_version, 0, 0, IS_STRING, 0)
 ZEND_END_ARG_INFO()
@@ -127,7 +127,7 @@ ZEND_END_ARG_INFO()
 #define arginfo_user_error arginfo_trigger_error
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_set_error_handler, 0, 0, 1)
-       ZEND_ARG_INFO(0, error_handler)
+       ZEND_ARG_TYPE_INFO(0, error_handler, IS_CALLABLE, 1)
        ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, error_types, IS_LONG, 0, "E_ALL")
 ZEND_END_ARG_INFO()
 
@@ -135,7 +135,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_restore_error_handler, 0, 0, _IS
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_set_exception_handler, 0, 0, 1)
-       ZEND_ARG_INFO(0, exception_handler)
+       ZEND_ARG_TYPE_INFO(0, exception_handler, IS_CALLABLE, 1)
 ZEND_END_ARG_INFO()
 
 #define arginfo_restore_exception_handler arginfo_restore_error_handler