1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Convert "cannot add element" warning to exception

This commit is contained in:
Nikita Popov
2019-09-26 16:05:24 +02:00
parent 36eecc1076
commit f2b09969db
14 changed files with 129 additions and 100 deletions

View File

@@ -91,6 +91,10 @@ PHP 8.0 UPGRADE NOTES
the value into an object (if it was null, false or an empty string) or
ignored the write altogether (in all other cases).
RFC: Part of https://wiki.php.net/rfc/engine_warnings
. Attempting to append an element to an array for which the PHP_INT_MAX key
is already used will now result in an Error exception. Previously this was
a warning.
RFC: Part of https://wiki.php.net/rfc/engine_warnings
- COM:
. Removed the ability to import case-insensitive constants from type

View File

@@ -4,22 +4,21 @@ Next free element may overflow in array literals
<?php
$i = PHP_INT_MAX;
$array = [$i => 42, new stdClass];
var_dump($array);
try {
$array = [$i => 42, new stdClass];
var_dump($array);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
const FOO = [PHP_INT_MAX => 42, "foo"];
var_dump(FOO);
function test($x = [PHP_INT_MAX => 42, "foo"]) {}
try {
test();
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
array(1) {
[%d]=>
int(42)
}
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
array(1) {
[%d]=>
int(42)
}
--EXPECT--
Cannot add element to the array as the next element is already occupied
Cannot add element to the array as the next element is already occupied

View File

@@ -6,36 +6,28 @@ Appending to an array via unpack may fail
<?php
$arr = [1, 2, 3];
var_dump([PHP_INT_MAX-1 => 0, ...$arr]);
try {
var_dump([PHP_INT_MAX-1 => 0, ...$arr]);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump([PHP_INT_MAX-1 => 0, ...[1, 2, 3]]);
try {
var_dump([PHP_INT_MAX-1 => 0, ...[1, 2, 3]]);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
const ARR = [1, 2, 3];
const ARR2 = [PHP_INT_MAX-1 => 0, ...ARR];
var_dump(ARR2);
function test($x = [PHP_INT_MAX-1 => 0, ...ARR]) {}
try {
test();
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
array(2) {
[9223372036854775806]=>
int(0)
[9223372036854775807]=>
int(1)
}
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
array(2) {
[9223372036854775806]=>
int(0)
[9223372036854775807]=>
int(1)
}
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
array(2) {
[9223372036854775806]=>
int(0)
[9223372036854775807]=>
int(1)
}
--EXPECT--
Cannot add element to the array as the next element is already occupied
Cannot add element to the array as the next element is already occupied
Cannot add element to the array as the next element is already occupied

View File

@@ -7,12 +7,22 @@ function test() {
$array = [PHP_INT_MAX => 42];
$true = true;
var_dump($array[] = 123);
try {
var_dump($array[] = 123);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($array[[]] = 123);
var_dump($array[new stdClass] = 123);
var_dump($true[123] = 456);
var_dump($array[] += 123);
try {
var_dump($array[] += 123);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($array[[]] += 123);
var_dump($array[new stdClass] += 123);
var_dump($true[123] += 456);
@@ -33,8 +43,7 @@ test();
?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
NULL
Cannot add element to the array as the next element is already occupied
Warning: Illegal offset type in %s on line %d
NULL
@@ -44,9 +53,7 @@ NULL
Warning: Cannot use a scalar value as an array in %s on line %d
NULL
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
NULL
Cannot add element to the array as the next element is already occupied
Warning: Illegal offset type in %s on line %d
NULL

View File

@@ -9,17 +9,22 @@ function val() {
$var = 24;
$arr = [PHP_INT_MAX => "foo"];
var_dump($arr[] =& $var);
try {
var_dump($arr[] =& $var);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump(count($arr));
var_dump($arr[] =& val());
try {
var_dump($arr[] =& val());
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump(count($arr));
?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
NULL
--EXPECT--
Cannot add element to the array as the next element is already occupied
int(1)
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
NULL
Cannot add element to the array as the next element is already occupied
int(1)

View File

@@ -2,13 +2,13 @@
Bug #36303 (foreach on error_zval produces segfault)
--FILE--
<?php
$x=[PHP_INT_MAX=>"test"];
foreach ($x[] as &$v) {
$x = [];
foreach ($x[[]] as &$v) {
}
echo "ok\n";
?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
Warning: Illegal offset type in %s on line %d
Warning: Invalid argument supplied for foreach() in %s on line %d
ok

View File

@@ -4,12 +4,16 @@ Bug #47836 (array operator [] inconsistency when the array has PHP_INT_MAX index
<?php
$arr[PHP_INT_MAX] = 1;
$arr[] = 2;
try {
$arr[] = 2;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($arr);
?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %s on line 4
Cannot add element to the array as the next element is already occupied
array(1) {
[%d]=>
int(1)

View File

@@ -2,10 +2,10 @@
Bug #52237 (Crash when passing the reference of the property of a non-object)
--FILE--
<?php
$data = [PHP_INT_MAX => 'test'];
preg_match('//', '', $data[]);
$data = [];
preg_match('//', '', $data[[]]);
var_dump(count($data));
?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
int(1)
Warning: Illegal offset type in %s on line %d
int(0)

View File

@@ -16,14 +16,18 @@ class c1
c1::$a1[] = 1;
c1::$a2[] = 1;
c1::$a3[] = 1;
try {
c1::$a3[] = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump(c1::$a1);
var_dump(c1::$a2);
var_dump(c1::$a3);
?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %sbug69017.php on line %d
Cannot add element to the array as the next element is already occupied
array(2) {
[1]=>
string(3) "one"

View File

@@ -1,23 +1,33 @@
--TEST--
Bug #71841 (EG(error_zval) is not handled well)
--INI--
error_reporting=0
--FILE--
<?php
$z = unserialize('O:1:"A":0:{}');
var_dump($z->e.=0);
var_dump(++$z->x);
var_dump($z->y++);
@var_dump($z->e.=0);
@var_dump(++$z->x);
@var_dump($z->y++);
$y = array(PHP_INT_MAX => 0);
var_dump($y[] .= 0);
var_dump(++$y[]);
var_dump($y[]++);
try {
var_dump($y[] .= 0);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump(++$y[]);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($y[]++);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
NULL
NULL
NULL
NULL
NULL
NULL
Cannot add element to the array as the next element is already occupied
Cannot add element to the array as the next element is already occupied
Cannot add element to the array as the next element is already occupied

View File

@@ -411,9 +411,9 @@ static int zend_ast_add_array_element(zval *result, zval *offset, zval *expr)
switch (Z_TYPE_P(offset)) {
case IS_UNDEF:
if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
zend_error(E_WARNING,
zend_throw_error(NULL,
"Cannot add element to the array as the next element is already occupied");
zval_ptr_dtor_nogc(expr);
return FAILURE;
}
break;
case IS_STRING:
@@ -458,8 +458,9 @@ static int zend_ast_add_unpacked_element(zval *result, zval *expr) {
return FAILURE;
} else {
if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied");
break;
zend_throw_error(NULL,
"Cannot add element to the array as the next element is already occupied");
return FAILURE;
}
Z_TRY_ADDREF_P(val);
}

View File

@@ -1892,7 +1892,7 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(v
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
{
zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied");
zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
}
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_resource_as_offset(const zval *dim)

View File

@@ -1975,20 +1975,20 @@ static int zend_jit_cannot_add_element_stub(dasm_State **Dst)
| SET_Z_TYPE_INFO FP + r0, IS_NULL
|1:
|.if X64WIN
| mov CARG1, E_WARNING
| xor CARG1, CARG1
| LOAD_ADDR CARG2, "Cannot add element to the array as the next element is already occupied"
| EXT_CALL zend_error, r0
| EXT_CALL zend_throw_error, r0
| add r4, 0x28
|.elif X64
| mov CARG1, E_WARNING
| xor CARG1, CARG1
| LOAD_ADDR CARG2, "Cannot add element to the array as the next element is already occupied"
| EXT_CALL zend_error, r0
| EXT_CALL zend_throw_error, r0
| add r4, 8
|.else
| sub r4, 8
| push "Cannot add element to the array as the next element is already occupied"
| push E_WARNING
| EXT_CALL zend_error, r0
| push 0
| EXT_CALL zend_throw_error, r0
| add r4, 28
|.endif
| ret
@@ -4644,7 +4644,7 @@ static int zend_jit_assign_dim(dasm_State **Dst, const zend_op *opline, zend_op_
| jz >1
|.cold_code
|1:
| // zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied");
| // zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
| CANNOT_ADD_ELEMENT opline
| //ZEND_VM_C_GOTO(assign_dim_op_ret_null);
| jmp >9
@@ -4880,7 +4880,7 @@ static int zend_jit_assign_dim_op(dasm_State **Dst, const zend_op *opline, zend_
| jz >1
|.cold_code
|1:
| // zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied");
| // zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
| CANNOT_ADD_ELEMENT opline
| //ZEND_VM_C_GOTO(assign_dim_op_ret_null);
| jmp >9

View File

@@ -29,7 +29,11 @@ foo2();
function foo3() {
$array = array(PHP_INT_MAX => "dummy");
$array[] = array();
try {
$array[] = array();
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
$array = new ArrayObject();
$array[index()] = 1;
@@ -83,9 +87,8 @@ array(1) {
array(0) {
}
}
Warning: Cannot add element to the array as the next element is already occupied in %sassign_dim_002.php on line 22
object(ArrayObject)#1 (1) {
Cannot add element to the array as the next element is already occupied
object(ArrayObject)#%d (1) {
["storage":"ArrayObject":private]=>
array(2) {
[2]=>
@@ -104,7 +107,7 @@ array(1) {
}
}
Warning: Illegal offset type in %sassign_dim_002.php on line 47
Warning: Illegal offset type in %sassign_dim_002.php on line 51
array(1) {
[0]=>
array(2) {
@@ -124,5 +127,5 @@ array(1) {
}
}
Warning: Cannot use a scalar value as an array in %sassign_dim_002.php on line 57
Warning: Cannot use a scalar value as an array in %sassign_dim_002.php on line 61
int(1)