Skip to content

Commit 6ab5038

Browse files
authored
Make array_pad's $length warning less confusing (#10149)
Remove array_pad's arbitrary length restriction The error message was wrong; it *is* possible to use a larger length. Furthermore, there is an arbitrary restriction on the new array's length. Fix both by checking the length against HT_MAX_SIZE.
1 parent 690db97 commit 6ab5038

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

ext/standard/array.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4344,13 +4344,14 @@ PHP_FUNCTION(array_pad)
43444344
Z_PARAM_ZVAL(pad_value)
43454345
ZEND_PARSE_PARAMETERS_END();
43464346

4347+
if (pad_size < Z_L(-HT_MAX_SIZE) || pad_size > Z_L(HT_MAX_SIZE)) {
4348+
zend_argument_value_error(2, "must not exceed the maximum allowed array size");
4349+
RETURN_THROWS();
4350+
}
4351+
43474352
/* Do some initial calculations */
43484353
input_size = zend_hash_num_elements(Z_ARRVAL_P(input));
43494354
pad_size_abs = ZEND_ABS(pad_size);
4350-
if (pad_size_abs < 0 || pad_size_abs - input_size > Z_L(1048576)) {
4351-
zend_argument_value_error(2, "must be less than or equal to 1048576");
4352-
RETURN_THROWS();
4353-
}
43544355

43554356
if (input_size >= pad_size_abs) {
43564357
/* Copy the original array */

ext/standard/tests/array/array_pad.phpt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ var_dump(array_pad(array("", -1, 2.0), 2, array()));
1313
var_dump(array_pad(array("", -1, 2.0), -3, array()));
1414
var_dump(array_pad(array("", -1, 2.0), -4, array()));
1515

16-
try {
17-
var_dump(array_pad(array("", -1, 2.0), 2000000, 0));
18-
} catch (\ValueError $e) {
19-
echo $e->getMessage() . "\n";
20-
}
21-
2216
?>
2317
--EXPECT--
2418
array(1) {
@@ -84,4 +78,3 @@ array(4) {
8478
[3]=>
8579
float(2)
8680
}
87-
array_pad(): Argument #2 ($length) must be less than or equal to 1048576
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
array_pad() with too large padding should fail
3+
--FILE--
4+
<?php
5+
6+
function test($length) {
7+
try {
8+
var_dump(array_pad(array("", -1, 2.0), $length, 0));
9+
} catch (\ValueError $e) {
10+
echo $e->getMessage() . "\n";
11+
}
12+
}
13+
14+
test(PHP_INT_MIN);
15+
test(PHP_INT_MAX);
16+
17+
?>
18+
--EXPECT--
19+
array_pad(): Argument #2 ($length) must not exceed the maximum allowed array size
20+
array_pad(): Argument #2 ($length) must not exceed the maximum allowed array size

0 commit comments

Comments
 (0)