Skip to content

Promote warnings to error in array_fill() #4576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2602,7 +2602,7 @@ PHP_FUNCTION(compact)
}
/* }}} */

/* {{{ proto array|false array_fill(int start_key, int num, mixed val)
/* {{{ proto array array_fill(int start_key, int num, mixed val)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stub return type also needs to be updated.

Create an array containing num elements starting with index start_key each initialized to val */
PHP_FUNCTION(array_fill)
{
Expand All @@ -2617,11 +2617,11 @@ PHP_FUNCTION(array_fill)

if (EXPECTED(num > 0)) {
if (sizeof(num) > 4 && UNEXPECTED(EXPECTED(num > 0x7fffffff))) {
php_error_docref(NULL, E_WARNING, "Too many elements");
RETURN_FALSE;
zend_throw_error(NULL, "Too many elements");
return;
} else if (UNEXPECTED(start_key > ZEND_LONG_MAX - num + 1)) {
php_error_docref(NULL, E_WARNING, "Cannot add element to the array as the next element is already occupied");
RETURN_FALSE;
zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
return;
} else if (EXPECTED(start_key >= 0) && EXPECTED(start_key < num)) {
/* create packed array */
Bucket *p;
Expand Down Expand Up @@ -2666,8 +2666,8 @@ PHP_FUNCTION(array_fill)
} else if (EXPECTED(num == 0)) {
RETURN_EMPTY_ARRAY();
} else {
php_error_docref(NULL, E_WARNING, "Number of elements can't be negative");
RETURN_FALSE;
zend_throw_error(NULL, "Number of elements can't be negative");
return;
}
}
/* }}} */
Expand Down
15 changes: 10 additions & 5 deletions ext/standard/tests/array/array_fill_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ echo "*** Testing array_fill() : error conditions ***\n";
$start_key = 0;
$num = -1;
$val = 1;
var_dump( array_fill($start_key,$num,$val) );

echo "Done";
try {
var_dump( array_fill($start_key,$num,$val) );
} catch (Error $e) {
echo $e->getMessage() . "\n";
}

?>

DONE
--EXPECTF--
*** Testing array_fill() : error conditions ***
Number of elements can't be negative

Warning: array_fill(): Number of elements can't be negative in %s on line %d
bool(false)
Done
DONE
11 changes: 8 additions & 3 deletions ext/standard/tests/array/bug61058.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
Bug #61058 (array_fill leaks if start index is PHP_INT_MAX)
--FILE--
<?php
array_fill(PHP_INT_MAX, 2, '*');

try {
array_fill(PHP_INT_MAX, 2, '*');
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
?>
--EXPECTF--
Warning: array_fill(): Cannot add element to the array as the next element is already occupied in %sbug61058.php on line %d
--EXPECT--
Cannot add element to the array as the next element is already occupied