Skip to content

Commit 7b9d1d9

Browse files
committed
changes from feedback
1 parent 5f7e7ba commit 7b9d1d9

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

ext/pcntl/pcntl.c

Lines changed: 28 additions & 16 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
}
@@ -1530,36 +1529,47 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15301529
zend_long pid;
15311530
bool pid_is_null = 1;
15321531
cpu_set_t mask;
1533-
zval *hmask = NULL, *cpu;
1534-
zend_ulong maxcpus;
1532+
zval *hmask = NULL, *ncpu;
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

1552-
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)) {
1555-
CPU_SET(Z_LVAL_P(cpu), &mask);
1550+
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), ncpu) {
1551+
ZVAL_DEREF(ncpu);
1552+
zval cpu;
1553+
ZVAL_COPY_VALUE(&cpu, ncpu);
1554+
if (Z_TYPE(cpu) != IS_LONG) {
1555+
if (Z_TYPE(cpu) == IS_STRING) {
1556+
convert_to_long(&cpu);
1557+
} else {
1558+
convert_to_string(&cpu);
1559+
zend_value_error("cpu id invalid type (%s)", Z_STRVAL(cpu));
1560+
RETURN_THROWS();
1561+
}
15561562
}
1557-
} ZEND_HASH_FOREACH_END();
15581563

1559-
if (!CPU_COUNT(&mask)) {
1560-
zend_argument_value_error(2, "invalid cpu affinity mapping");
1561-
RETURN_THROWS();
1562-
}
1564+
if (Z_LVAL(cpu) < 0 || Z_LVAL(cpu) >= maxcpus) {
1565+
zend_value_error("cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, Z_LVAL(cpu));
1566+
RETURN_THROWS();
1567+
}
1568+
1569+
if (!CPU_ISSET(Z_LVAL(cpu), &mask)) {
1570+
CPU_SET(Z_LVAL(cpu), &mask);
1571+
}
1572+
} ZEND_HASH_FOREACH_END();
15631573

15641574
if (sched_setaffinity(pid, sizeof(mask), &mask) != 0) {
15651575
PCNTL_G(last_error) = errno;
@@ -1570,6 +1580,8 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15701580
case EPERM:
15711581
php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
15721582
break;
1583+
default:
1584+
php_error_docref(NULL, E_WARNING, "Error %d", errno);
15731585
}
15741586
RETURN_FALSE;
15751587
} 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)