Skip to content

Commit 10df271

Browse files
committed
Make array_pad's $length warning less confusing
The error message was wrong; it *is* possible to use a larger length. The error message should be clear it's about how much padding is required. This patch also removes a redundant check on pad_size_abs.
1 parent adc2382 commit 10df271

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

ext/standard/array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,8 +4227,9 @@ PHP_FUNCTION(array_pad)
42274227
/* Do some initial calculations */
42284228
input_size = zend_hash_num_elements(Z_ARRVAL_P(input));
42294229
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");
4230+
num_pads = pad_size_abs - input_size;
4231+
if (num_pads > Z_L(1048576)) {
4232+
zend_argument_value_error(2, "is too large because it results in a padding of %" PRId64 " elements, which is larger than the allowed 1048576 elements", num_pads);
42324233
RETURN_THROWS();
42334234
}
42344235

@@ -4238,7 +4239,6 @@ PHP_FUNCTION(array_pad)
42384239
return;
42394240
}
42404241

4241-
num_pads = pad_size_abs - input_size;
42424242
if (Z_REFCOUNTED_P(pad_value)) {
42434243
GC_ADDREF_EX(Z_COUNTED_P(pad_value), num_pads);
42444244
}

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)