Skip to content

Commit e3f2351

Browse files
committed
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 9788244 commit e3f2351

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ array(4) {
8484
[3]=>
8585
float(2)
8686
}
87-
array_pad(): Argument #2 ($length) must be less than or equal to 1048576
87+
array_pad(): Argument #2 ($length) is too large because it results in a padding of 1999997 elements, which is larger than the allowed 1048576 elements

0 commit comments

Comments
 (0)