Skip to content
Open
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
7 changes: 4 additions & 3 deletions ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
}

if (cpu < 0 || cpu >= maxcpus) {
zend_argument_value_error(2, "cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, cpu);
zend_argument_value_error(2, "cpu id must be between 0 and " ZEND_LONG_FMT " (" ZEND_LONG_FMT ")", maxcpus - 1, cpu);
PCNTL_CPU_DESTROY(mask);
RETURN_THROWS();
}
Expand Down Expand Up @@ -1880,9 +1880,10 @@ PHP_FUNCTION(pcntl_getqos_class)

PHP_FUNCTION(pcntl_setqos_class)
{
zend_enum_Pcntl_QosClass qos;
zend_enum_Pcntl_QosClass qos = ZEND_ENUM_Pcntl_QosClass_Default;

ZEND_PARSE_PARAMETERS_START(1, 1)
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_ENUM(qos, QosClass_ce)
ZEND_PARSE_PARAMETERS_END();

Expand Down
46 changes: 46 additions & 0 deletions ext/pcntl/tests/pcntl_cpuaffinity_bound.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Comment thread
lacatoire marked this conversation as resolved.
pcntl_setcpuaffinity(): the upper bound the error advertises is itself a valid cpu id
--EXTENSIONS--
pcntl
--SKIPIF--
<?php
if (PHP_OS_FAMILY === 'Solaris') {
die("skip broken pset_create()");
}
if (!function_exists("pcntl_setcpuaffinity")) die("skip pcntl_setcpuaffinity is not available");
?>
--FILE--
<?php
$pid = getmypid();
$prefix = 'pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id must be between 0 and ';

/* read the advertised bound out of the message itself */
try {
pcntl_setcpuaffinity($pid, [PHP_INT_MAX]);
exit("PHP_INT_MAX was accepted as a cpu id" . PHP_EOL);
} catch (ValueError $e) {
if (!preg_match('/must be between 0 and (\d+) \(/', $e->getMessage(), $m)) {
exit("unexpected message: " . $e->getMessage() . PHP_EOL);
}
}
$bound = (int) $m[1];

/* Every id is range checked before any syscall runs, so pairing the advertised
bound with an out of range id shows which of the two the check rejects,
without ever changing the process affinity. */
try {
pcntl_setcpuaffinity($pid, [$bound, PHP_INT_MAX]);
} catch (ValueError $e) {
var_dump($e->getMessage() === $prefix . $bound . ' (' . PHP_INT_MAX . ')');
}

/* and the first id past the bound is rejected, naming itself */
try {
pcntl_setcpuaffinity($pid, [$bound + 1]);
} catch (ValueError $e) {
var_dump($e->getMessage() === $prefix . $bound . ' (' . ($bound + 1) . ')');
}
?>
--EXPECT--
bool(true)
bool(true)
5 changes: 5 additions & 0 deletions ext/pcntl/tests/pcntl_qosclass.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ pcntl_setqos_class(Pcntl\QosClass::Default);
var_dump(Pcntl\QosClass::Default === pcntl_getqos_class());
pcntl_setqos_class(Pcntl\QosClass::Background);
var_dump(Pcntl\QosClass::Background == pcntl_getqos_class());

/* the parameter is optional, and omitting it applies the declared default */
pcntl_setqos_class();
var_dump(Pcntl\QosClass::Default === pcntl_getqos_class());
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
Loading