Skip to content

Commit afed2bf

Browse files
committed
changes from feedback
1 parent 5f7e7ba commit afed2bf

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

ext/pcntl/pcntl.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,6 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
14871487
zend_long pid;
14881488
bool pid_is_null = 1;
14891489
cpu_set_t mask;
1490-
zend_ulong i, maxcpus;
14911490

14921491
ZEND_PARSE_PARAMETERS_START(0, 1)
14931492
Z_PARAM_OPTIONAL
@@ -1515,10 +1514,10 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
15151514
RETURN_EMPTY_ARRAY();
15161515
}
15171516

1518-
maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_ONLN);
1517+
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_ONLN);
15191518
array_init(return_value);
15201519

1521-
for (i = 0; i < maxcpus; i ++) {
1520+
for (zend_ulong i = 0; i < maxcpus; i ++) {
15221521
if (CPU_ISSET(i, &mask)) {
15231522
add_next_index_long(return_value, i);
15241523
}
@@ -1531,36 +1530,46 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15311530
bool pid_is_null = 1;
15321531
cpu_set_t mask;
15331532
zval *hmask = NULL, *cpu;
1534-
zend_ulong maxcpus;
15351533

15361534
ZEND_PARSE_PARAMETERS_START(0, 2)
15371535
Z_PARAM_OPTIONAL
15381536
Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
15391537
Z_PARAM_ARRAY(hmask)
15401538
ZEND_PARSE_PARAMETERS_END();
15411539

1542-
if (!hmask || Z_ARRVAL_P(hmask)->nNumOfElements == 0) {
1540+
if (!hmask || zend_hash_num_elements(Z_ARRVAL_P(hmask)) == 0) {
15431541
zend_argument_value_error(2, "must not be empty");
15441542
RETURN_THROWS();
15451543
}
15461544

15471545
// 0 == getpid in this context, we re just saving a syscall
15481546
pid = pid_is_null ? 0 : pid;
1549-
maxcpus = sysconf(_SC_NPROCESSORS_ONLN);
1547+
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_ONLN);
15501548
CPU_ZERO(&mask);
15511549

15521550
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), cpu) {
1553-
if (Z_TYPE_P(cpu) == IS_LONG && Z_LVAL_P(cpu) >= 0 &&
1554-
Z_LVAL_P(cpu) < maxcpus && !CPU_ISSET(Z_LVAL_P(cpu), &mask)) {
1551+
if (Z_TYPE_P(cpu) != IS_LONG) {
1552+
if (Z_TYPE_P(cpu) == IS_STRING) {
1553+
convert_to_long(cpu);
1554+
} else {
1555+
convert_to_string(cpu);
1556+
zend_value_error("cpu id invalid type (%s)", Z_STRVAL_P(cpu));
1557+
RETURN_THROWS();
1558+
}
1559+
}
1560+
1561+
ZVAL_DEREF(cpu);
1562+
1563+
if (Z_LVAL_P(cpu) < 0 || Z_LVAL_P(cpu) >= maxcpus) {
1564+
zend_value_error("cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, Z_LVAL_P(cpu));
1565+
RETURN_THROWS();
1566+
}
1567+
1568+
if (!CPU_ISSET(Z_LVAL_P(cpu), &mask)) {
15551569
CPU_SET(Z_LVAL_P(cpu), &mask);
15561570
}
15571571
} ZEND_HASH_FOREACH_END();
15581572

1559-
if (!CPU_COUNT(&mask)) {
1560-
zend_argument_value_error(2, "invalid cpu affinity mapping");
1561-
RETURN_THROWS();
1562-
}
1563-
15641573
if (sched_setaffinity(pid, sizeof(mask), &mask) != 0) {
15651574
PCNTL_G(last_error) = errno;
15661575
switch (errno) {
@@ -1570,6 +1579,8 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15701579
case EPERM:
15711580
php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
15721581
break;
1582+
default:
1583+
php_error_docref(NULL, E_WARNING, "Error %d", errno);
15731584
}
15741585
RETURN_FALSE;
15751586
} else {

ext/pcntl/tests/pcntl_cpuaffinity.phpt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $act_mask = pcntl_getcpuaffinity();
1414
var_dump(array_diff($mask, $act_mask));
1515
$n_act_mask = pcntl_getcpuaffinity();
1616
var_dump(array_diff($act_mask, $n_act_mask));
17+
var_dump(pcntl_setcpuaffinity(null, ["0", "1"]));
1718

1819
try {
1920
pcntl_setcpuaffinity(null, []);
@@ -45,14 +46,14 @@ try {
4546
echo $e->getMessage();
4647
}
4748
?>
48-
--EXPECT--
49+
--EXPECTF--
4950
bool(true)
5051
array(0) {
5152
}
5253
array(0) {
5354
}
55+
bool(true)
5456
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) must not be empty
55-
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) invalid cpu affinity mapping
56-
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) invalid cpu affinity mapping
57-
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) invalid cpu affinity mapping
57+
cpu id must be between 0 and %d (9223372036854775807)
58+
cpu id must be between 0 and %d (-1024)
5859
pcntl_getcpuaffinity(): Argument #1 ($process_id) invalid process (-1024)

0 commit comments

Comments
 (0)