Skip to content

Commit 9f2a1d7

Browse files
committed
Promote warnings to exceptions in ext/pcntl
1 parent 517c993 commit 9f2a1d7

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

ext/pcntl/pcntl.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -892,9 +892,12 @@ PHP_FUNCTION(pcntl_signal)
892892
RETURN_THROWS();
893893
}
894894

895-
if (signo < 1 || signo >= NSIG) {
896-
php_error_docref(NULL, E_WARNING, "Invalid signal");
897-
RETURN_FALSE;
895+
if (signo < 1) {
896+
zend_argument_value_error(1, "must be greater than or equal to 1");
897+
RETURN_THROWS();
898+
} else if (signo >= NSIG) {
899+
zend_argument_value_error(1, "must be less than %d", NSIG);
900+
RETURN_THROWS();
898901
}
899902

900903
if (!PCNTL_G(spares)) {
@@ -920,8 +923,8 @@ PHP_FUNCTION(pcntl_signal)
920923
/* Special long value case for SIG_DFL and SIG_IGN */
921924
if (Z_TYPE_P(handle) == IS_LONG) {
922925
if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) {
923-
php_error_docref(NULL, E_WARNING, "Invalid value for handle argument specified");
924-
RETURN_FALSE;
926+
zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given");
927+
RETURN_THROWS();
925928
}
926929
if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) {
927930
PCNTL_G(last_error) = errno;
@@ -935,10 +938,11 @@ PHP_FUNCTION(pcntl_signal)
935938
if (!zend_is_callable_ex(handle, NULL, 0, NULL, NULL, &error)) {
936939
zend_string *func_name = zend_get_callable_name(handle);
937940
PCNTL_G(last_error) = EINVAL;
938-
php_error_docref(NULL, E_WARNING, "Specified handler \"%s\" is not callable (%s)", ZSTR_VAL(func_name), error);
941+
942+
zend_argument_type_error(2, "must be a valid callback");
939943
zend_string_release_ex(func_name, 0);
940944
efree(error);
941-
RETURN_FALSE;
945+
RETURN_THROWS();
942946
}
943947
ZEND_ASSERT(!error);
944948

@@ -966,8 +970,8 @@ PHP_FUNCTION(pcntl_signal_get_handler)
966970
}
967971

968972
if (signo < 1 || signo > 32) {
969-
php_error_docref(NULL, E_WARNING, "Invalid signal");
970-
RETURN_FALSE;
973+
zend_argument_value_error(1, "must be between 1 and 32");
974+
RETURN_THROWS();
971975
}
972976

973977
if ((prev_handle = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
@@ -1197,8 +1201,8 @@ PHP_FUNCTION(pcntl_getpriority)
11971201
php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
11981202
break;
11991203
case EINVAL:
1200-
php_error_docref(NULL, E_WARNING, "Error %d: Invalid identifier flag", errno);
1201-
break;
1204+
zend_argument_value_error(2, "must be a valid identifier flag");
1205+
RETURN_THROWS();
12021206
default:
12031207
php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
12041208
break;
@@ -1231,7 +1235,8 @@ PHP_FUNCTION(pcntl_setpriority)
12311235
php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
12321236
break;
12331237
case EINVAL:
1234-
php_error_docref(NULL, E_WARNING, "Error %d: Invalid identifier flag", errno);
1238+
zend_argument_value_error(3, "must be a valid identifier flag");
1239+
RETURN_THROWS();
12351240
break;
12361241
case EPERM:
12371242
php_error_docref(NULL, E_WARNING, "Error %d: A process was located, but neither its effective nor real user ID matched the effective user ID of the caller", errno);
@@ -1400,19 +1405,18 @@ PHP_FUNCTION(pcntl_async_signals)
14001405
PHP_FUNCTION(pcntl_unshare)
14011406
{
14021407
zend_long flags;
1403-
int ret;
14041408

14051409
ZEND_PARSE_PARAMETERS_START(1, 1)
14061410
Z_PARAM_LONG(flags)
14071411
ZEND_PARSE_PARAMETERS_END();
14081412

1409-
ret = unshare(flags);
1410-
if (ret == -1) {
1413+
if (unshare(flags) == -1) {
14111414
PCNTL_G(last_error) = errno;
14121415
switch (errno) {
14131416
#ifdef EINVAL
14141417
case EINVAL:
1415-
php_error_docref(NULL, E_WARNING, "Error %d: Invalid flag specified", errno);
1418+
zend_argument_value_error(1, "must be a valid identifier flag");
1419+
RETURN_THROWS();
14161420
break;
14171421
#endif
14181422
#ifdef ENOMEM

ext/pcntl/pcntl.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ function pcntl_wait(&$status, int $options = 0, &$rusage = []): int {}
1919
/** @param callable|int $handler */
2020
function pcntl_signal(int $signo, $handler, bool $restart_syscalls = true): bool {}
2121

22-
/** @return mixed */
23-
function pcntl_signal_get_handler(int $signo) {}
22+
function pcntl_signal_get_handler(int $signo): mixed {}
2423

2524
function pcntl_signal_dispatch(): bool {}
2625

ext/pcntl/pcntl_arginfo.h

Lines changed: 2 additions & 2 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: df744f88533ce9b84864fa2aa4dd7a5b7373231d */
2+
* Stub hash: 79f0005cda5f8bc55a913c8d2c8791699abc2f0f */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0)
55
ZEND_END_ARG_INFO()
@@ -23,7 +23,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_signal, 0, 2, _IS_BOOL, 0)
2323
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, restart_syscalls, _IS_BOOL, 0, "true")
2424
ZEND_END_ARG_INFO()
2525

26-
ZEND_BEGIN_ARG_INFO_EX(arginfo_pcntl_signal_get_handler, 0, 0, 1)
26+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_signal_get_handler, 0, 1, IS_MIXED, 0)
2727
ZEND_ARG_TYPE_INFO(0, signo, IS_LONG, 0)
2828
ZEND_END_ARG_INFO()
2929

ext/pcntl/tests/pcntl_signal.phpt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,24 @@ posix_kill(posix_getpid(), SIGUSR1);
1818
pcntl_signal_dispatch();
1919

2020
var_dump(pcntl_signal(SIGALRM, SIG_IGN));
21-
var_dump(pcntl_signal(-1, -1));
22-
var_dump(pcntl_signal(-1, function(){}));
23-
var_dump(pcntl_signal(SIGALRM, "not callable"));
2421

22+
try {
23+
pcntl_signal(-1, -1);
24+
} catch (ValueError $exception) {
25+
echo $exception->getMessage() . "\n";
26+
}
27+
28+
try {
29+
pcntl_signal(-1, function(){});
30+
} catch (ValueError $exception) {
31+
echo $exception->getMessage() . "\n";
32+
}
33+
34+
try {
35+
pcntl_signal(SIGALRM, "not callable");
36+
} catch (TypeError $exception) {
37+
echo $exception->getMessage() . "\n";
38+
}
2539

2640
/* test freeing queue in RSHUTDOWN */
2741
posix_kill(posix_getpid(), SIGTERM);
@@ -31,13 +45,7 @@ echo "ok\n";
3145
signal dispatched
3246
got signal from %r\d+|nobody%r
3347
bool(true)
34-
35-
Warning: pcntl_signal(): Invalid signal %s
36-
bool(false)
37-
38-
Warning: pcntl_signal(): Invalid signal %s
39-
bool(false)
40-
41-
Warning: pcntl_signal(): Specified handler "not callable" is not callable (%s) in %s
42-
bool(false)
48+
pcntl_signal(): Argument #1 ($signo) must be greater than or equal to 1
49+
pcntl_signal(): Argument #1 ($signo) must be greater than or equal to 1
50+
pcntl_signal(): Argument #2 ($handler) must be a valid callback
4351
ok

0 commit comments

Comments
 (0)