Allow $num=0 in array_fill()

Implemented request #49824.
This commit is contained in:
Nikita Popov 2013-11-12 21:56:50 +01:00
parent 1205a6c7dc
commit dc4bf922d9
5 changed files with 62 additions and 89 deletions

2
NEWS
View File

@ -67,6 +67,8 @@ PHP NEWS
1.1). (Adam)
. Implemented Change crypt() behavior w/o salt RFC. (Yasuo)
https://wiki.php.net/rfc/crypt_function_salt
. Implemented request #49824 (Change array_fill() to allow creating empty
array). (Nikita)
- XMLReader:
. Fixed bug #55285 (XMLReader::getAttribute/No/Ns methods inconsistency).

View File

@ -1521,14 +1521,18 @@ PHP_FUNCTION(array_fill)
return;
}
if (num < 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements must be positive");
if (num < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements can't be negative");
RETURN_FALSE;
}
/* allocate an array for return */
array_init_size(return_value, num);
if (num == 0) {
return;
}
num--;
zend_hash_index_update(Z_ARRVAL_P(return_value), start_key, &val, sizeof(zval *), NULL);
zval_add_ref(&val);

View File

@ -23,34 +23,28 @@ echo '== Done ==';
--EXPECTF--
===========================
start: 0 num: 0 value: 1
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 0 num: 0 value:
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 0 num: 0 value:
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 0 num: 0 value: d
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 0 num: 0 value: e
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 0 num: 0 value: f
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 0 num: 1 value: 1
array(1) {
@ -137,34 +131,28 @@ array(2) {
}
===========================
start: 1 num: 0 value: 1
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 1 num: 0 value:
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 1 num: 0 value:
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 1 num: 0 value: d
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 1 num: 0 value: e
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 1 num: 0 value: f
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 1 num: 1 value: 1
array(1) {
@ -251,34 +239,28 @@ array(2) {
}
===========================
start: 2.5 num: 0 value: 1
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 2.5 num: 0 value:
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 2.5 num: 0 value:
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 2.5 num: 0 value: d
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 2.5 num: 0 value: e
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 2.5 num: 0 value: f
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
===========================
start: 2.5 num: 1 value: 1
array(1) {

View File

@ -32,10 +32,6 @@ var_dump( array_fill($start_key,$num) );
$num = -1;
var_dump( array_fill($start_key,$num,$val) );
//callin array_fill with 'num' equal to zero value
$num = 0;
var_dump( array_fill($start_key,$num,$val) );
echo "Done";
?>
--EXPECTF--
@ -53,9 +49,6 @@ NULL
Warning: array_fill() expects exactly 3 parameters, 2 given in %s on line %d
NULL
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
Warning: array_fill(): Number of elements must be positive in %s on line %d
Warning: array_fill(): Number of elements can't be negative in %s on line %d
bool(false)
Done

View File

@ -106,7 +106,7 @@ array(2) {
}
-- Iteration 2 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
Warning: array_fill(): Number of elements can't be negative in %s on line %d
bool(false)
-- Iteration 3 --
array(5) {
@ -122,13 +122,11 @@ array(5) {
int(100)
}
-- Iteration 4 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
-- Iteration 5 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
-- Iteration 6 --
Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
@ -150,31 +148,27 @@ NULL
Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
NULL
-- Iteration 11 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
-- Iteration 12 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
-- Iteration 13 --
array(1) {
[0]=>
int(100)
}
-- Iteration 14 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
-- Iteration 15 --
array(1) {
[0]=>
int(100)
}
-- Iteration 16 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
-- Iteration 17 --
Warning: array_fill() expects parameter 2 to be long, string given in %s on line %d
@ -196,11 +190,9 @@ NULL
Warning: array_fill() expects parameter 2 to be long, object given in %s on line %d
NULL
-- Iteration 22 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
-- Iteration 23 --
Warning: array_fill(): Number of elements must be positive in %s on line %d
bool(false)
array(0) {
}
Done