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.
This commit is contained in:
Niels 2023-01-14 12:15:56 +01:00 committed by GitHub
parent 690db97c6d
commit 6ab503814d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View file

@ -4344,13 +4344,14 @@ PHP_FUNCTION(array_pad)
Z_PARAM_ZVAL(pad_value)
ZEND_PARSE_PARAMETERS_END();
if (pad_size < Z_L(-HT_MAX_SIZE) || pad_size > Z_L(HT_MAX_SIZE)) {
zend_argument_value_error(2, "must not exceed the maximum allowed array size");
RETURN_THROWS();
}
/* Do some initial calculations */
input_size = zend_hash_num_elements(Z_ARRVAL_P(input));
pad_size_abs = ZEND_ABS(pad_size);
if (pad_size_abs < 0 || pad_size_abs - input_size > Z_L(1048576)) {
zend_argument_value_error(2, "must be less than or equal to 1048576");
RETURN_THROWS();
}
if (input_size >= pad_size_abs) {
/* Copy the original array */

View file

@ -13,12 +13,6 @@ var_dump(array_pad(array("", -1, 2.0), 2, array()));
var_dump(array_pad(array("", -1, 2.0), -3, array()));
var_dump(array_pad(array("", -1, 2.0), -4, array()));
try {
var_dump(array_pad(array("", -1, 2.0), 2000000, 0));
} catch (\ValueError $e) {
echo $e->getMessage() . "\n";
}
?>
--EXPECT--
array(1) {
@ -84,4 +78,3 @@ array(4) {
[3]=>
float(2)
}
array_pad(): Argument #2 ($length) must be less than or equal to 1048576

View file

@ -0,0 +1,20 @@
--TEST--
array_pad() with too large padding should fail
--FILE--
<?php
function test($length) {
try {
var_dump(array_pad(array("", -1, 2.0), $length, 0));
} catch (\ValueError $e) {
echo $e->getMessage() . "\n";
}
}
test(PHP_INT_MIN);
test(PHP_INT_MAX);
?>
--EXPECT--
array_pad(): Argument #2 ($length) must not exceed the maximum allowed array size
array_pad(): Argument #2 ($length) must not exceed the maximum allowed array size