Skip to content

Commit f39a3d3

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 adc2382 commit f39a3d3

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
@@ -4224,13 +4224,14 @@ PHP_FUNCTION(array_pad)
42244224
Z_PARAM_ZVAL(pad_value)
42254225
ZEND_PARSE_PARAMETERS_END();
42264226

4227+
if (pad_size < Z_L(-HT_MAX_SIZE) || pad_size > Z_L(HT_MAX_SIZE)) {
4228+
zend_argument_value_error(2, "must not exceed the maximum allowed array size");
4229+
RETURN_THROWS();
4230+
}
4231+
42274232
/* Do some initial calculations */
42284233
input_size = zend_hash_num_elements(Z_ARRVAL_P(input));
42294234
pad_size_abs = ZEND_ABS(pad_size);
4230-
if (pad_size_abs < 0 || pad_size_abs - input_size > Z_L(1048576)) {
4231-
zend_argument_value_error(2, "must be less than or equal to 1048576");
4232-
RETURN_THROWS();
4233-
}
42344235

42354236
if (input_size >= pad_size_abs) {
42364237
/* 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)