Skip to content

Commit

Permalink
Use ZPP callable check for Windows specific functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Aug 13, 2020
1 parent 9cb5221 commit dae83cd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
4 changes: 1 addition & 3 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,7 @@ function sapi_windows_cp_conv(int|string $in_codepage, int|string $out_codepage,

function sapi_windows_cp_is_utf8(): bool {}

/** @param callable|null $handler */
function sapi_windows_set_ctrl_handler($handler, bool $add = true): bool {}
function sapi_windows_set_ctrl_handler(?callable $handler, bool $add = true): bool {}

/** @param callable|null $handler */
function sapi_windows_generate_ctrl_event(int $event, int $pid = 0): bool {}
#endif
4 changes: 2 additions & 2 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 35cb2432b5deea7cff903c2014bce795b4f30209 */
* Stub hash: 269d4da84e4bc6fae246b90e4c50e48463b86f41 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)
Expand Down Expand Up @@ -2229,7 +2229,7 @@ ZEND_END_ARG_INFO()

#if defined(PHP_WIN32)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_sapi_windows_set_ctrl_handler, 0, 1, _IS_BOOL, 0)
ZEND_ARG_INFO(0, handler)
ZEND_ARG_TYPE_INFO(0, handler, IS_CALLABLE, 1)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add, _IS_BOOL, 0, "true")
ZEND_END_ARG_INFO()
#endif
Expand Down
19 changes: 8 additions & 11 deletions win32/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ static BOOL WINAPI php_win32_signal_system_ctrl_handler(DWORD evt)
/* {{{ Assigns a CTRL signal handler to a PHP function */
PHP_FUNCTION(sapi_windows_set_ctrl_handler)
{
zval *handler = NULL;
zend_fcall_info fci;
zend_fcall_info_cache fcc;
zend_bool add = 1;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &handler, &add) == FAILURE) {

/* callable argument corresponds to the CTRL handler */
if (zend_parse_parameters(ZEND_NUM_ARGS(), "f!|b", &fci, &fcc, &add) == FAILURE) {
RETURN_THROWS();
}

Expand All @@ -106,8 +109,7 @@ PHP_FUNCTION(sapi_windows_set_ctrl_handler)
RETURN_THROWS();
}


if (IS_NULL == Z_TYPE_P(handler)) {
if (!ZEND_FCI_INITIALIZED(fci)) {
zval_dtor(&ctrl_handler);
ZVAL_UNDEF(&ctrl_handler);
if (!SetConsoleCtrlHandler(NULL, add)) {
Expand All @@ -116,20 +118,15 @@ PHP_FUNCTION(sapi_windows_set_ctrl_handler)
RETURN_TRUE;
}

if (!zend_is_callable(handler, 0, NULL)) {
zend_argument_type_error(1, "must be a valid callable function name");
RETURN_THROWS();
}

if (!SetConsoleCtrlHandler(NULL, FALSE) || !SetConsoleCtrlHandler(php_win32_signal_system_ctrl_handler, add)) {
zend_string *func_name = zend_get_callable_name(handler);
zend_string *func_name = zend_get_callable_name(&fci.function_name);
php_error_docref(NULL, E_WARNING, "Unable to attach %s as a CTRL handler", ZSTR_VAL(func_name));
zend_string_release_ex(func_name, 0);
RETURN_FALSE;
}

zval_dtor(&ctrl_handler);
ZVAL_COPY(&ctrl_handler, handler);
ZVAL_COPY(&ctrl_handler, &fci.function_name);

RETURN_TRUE;
}/*}}}*/
Expand Down

0 comments on commit dae83cd

Please sign in to comment.