Skip to content

Promote warnings to errors in array_push() #4585

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
6 changes: 3 additions & 3 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3151,7 +3151,7 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H
}
/* }}} */

/* {{{ proto int|false array_push(array stack, mixed var [, mixed ...])
/* {{{ proto int array_push(array stack, mixed var [, mixed ...])
Pushes elements onto the end of the array */
PHP_FUNCTION(array_push)
{
Expand All @@ -3173,8 +3173,8 @@ PHP_FUNCTION(array_push)

if (zend_hash_next_index_insert(Z_ARRVAL_P(stack), &new_var) == NULL) {
Z_TRY_DELREF(new_var);
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");
Copy link
Member

Choose a reason for hiding this comment

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

So, this is actually a pretty generic warning, in particular also used by $foo[] = $x, see

zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied");
. I think whatever we do here, we should have a consistent behavior between $foo[] = $x and array_push($foo, $x).

return;
}
}

Expand Down
3 changes: 1 addition & 2 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ function stream_wrapper_restore(string $protocol): bool {}

/* array.c */

/** @return int|false */
function array_push(array &$stack, ...$args) {}
function array_push(array &$stack, ...$args): int {}

function krsort(array &$arg, int $sort_flags = SORT_REGULAR): bool {}

Expand Down
2 changes: 1 addition & 1 deletion ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ZEND_END_ARG_INFO()

#define arginfo_stream_wrapper_restore arginfo_stream_wrapper_unregister

ZEND_BEGIN_ARG_INFO_EX(arginfo_array_push, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_push, 0, 1, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(1, stack, IS_ARRAY, 0)
ZEND_ARG_VARIADIC_INFO(0, args)
ZEND_END_ARG_INFO()
Expand Down
11 changes: 6 additions & 5 deletions ext/standard/tests/array/array_push_error2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ Test array_push() function : error conditions - max int value as key
echo "*** Testing array_push() : error conditions ***\n";

$array = array(PHP_INT_MAX => 'max');

var_dump(array_push($array, 'new'));
try {
var_dump(array_push($array, 'new'));
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
var_dump($array);

echo "Done";
?>
--EXPECTF--
*** Testing array_push() : error conditions ***

Warning: array_push(): Cannot add element to the array as the next element is already occupied in %s on line %d
bool(false)
Cannot add element to the array as the next element is already occupied
array(1) {
[%d]=>
string(3) "max"
Expand Down