Skip to content

Commit d5e9ef8

Browse files
Girgiasnikic
authored andcommitted
Promote warnings to error in array_flip()
Closes GH-4576.
1 parent d882795 commit d5e9ef8

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

ext/standard/array.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,7 @@ PHP_FUNCTION(compact)
26062606
}
26072607
/* }}} */
26082608

2609-
/* {{{ proto array|false array_fill(int start_key, int num, mixed val)
2609+
/* {{{ proto array array_fill(int start_key, int num, mixed val)
26102610
Create an array containing num elements starting with index start_key each initialized to val */
26112611
PHP_FUNCTION(array_fill)
26122612
{
@@ -2621,11 +2621,11 @@ PHP_FUNCTION(array_fill)
26212621

26222622
if (EXPECTED(num > 0)) {
26232623
if (sizeof(num) > 4 && UNEXPECTED(EXPECTED(num > 0x7fffffff))) {
2624-
php_error_docref(NULL, E_WARNING, "Too many elements");
2625-
RETURN_FALSE;
2624+
zend_throw_error(NULL, "Too many elements");
2625+
return;
26262626
} else if (UNEXPECTED(start_key > ZEND_LONG_MAX - num + 1)) {
2627-
php_error_docref(NULL, E_WARNING, "Cannot add element to the array as the next element is already occupied");
2628-
RETURN_FALSE;
2627+
zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
2628+
return;
26292629
} else if (EXPECTED(start_key >= 0) && EXPECTED(start_key < num)) {
26302630
/* create packed array */
26312631
Bucket *p;
@@ -2670,8 +2670,8 @@ PHP_FUNCTION(array_fill)
26702670
} else if (EXPECTED(num == 0)) {
26712671
RETURN_EMPTY_ARRAY();
26722672
} else {
2673-
php_error_docref(NULL, E_WARNING, "Number of elements can't be negative");
2674-
RETURN_FALSE;
2673+
zend_throw_error(NULL, "Number of elements can't be negative");
2674+
return;
26752675
}
26762676
}
26772677
/* }}} */

ext/standard/tests/array/array_fill_error.phpt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ echo "*** Testing array_fill() : error conditions ***\n";
1414
$start_key = 0;
1515
$num = -1;
1616
$val = 1;
17-
var_dump( array_fill($start_key,$num,$val) );
1817

19-
echo "Done";
18+
try {
19+
var_dump( array_fill($start_key,$num,$val) );
20+
} catch (Error $e) {
21+
echo $e->getMessage() . "\n";
22+
}
23+
2024
?>
25+
26+
DONE
2127
--EXPECTF--
2228
*** Testing array_fill() : error conditions ***
29+
Number of elements can't be negative
2330

24-
Warning: array_fill(): Number of elements can't be negative in %s on line %d
25-
bool(false)
26-
Done
31+
DONE

ext/standard/tests/array/bug61058.phpt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
Bug #61058 (array_fill leaks if start index is PHP_INT_MAX)
33
--FILE--
44
<?php
5-
array_fill(PHP_INT_MAX, 2, '*');
5+
6+
try {
7+
array_fill(PHP_INT_MAX, 2, '*');
8+
} catch (\Error $e) {
9+
echo $e->getMessage() . "\n";
10+
}
611
?>
7-
--EXPECTF--
8-
Warning: array_fill(): Cannot add element to the array as the next element is already occupied in %sbug61058.php on line %d
12+
--EXPECT--
13+
Cannot add element to the array as the next element is already occupied

0 commit comments

Comments
 (0)