Skip to content

Convert "already occupied" and "illegal offset type" warnings to Error exceptions #4748

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 3 commits 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
21 changes: 13 additions & 8 deletions Zend/tests/036.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ Trying to use lambda in array offset
--FILE--
<?php

$test[function(){}] = 1;
$a{function() { }} = 1;
try {
$test[function(){}] = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
$a{function() { }} = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECTF--

Deprecated: Array and string offset access syntax with curly braces is deprecated in %s line %d

Warning: Illegal offset type in %s on line %d

Warning: Illegal offset type in %s on line %d
Deprecated: Array and string offset access syntax with curly braces is deprecated in %s on line %d
Illegal offset type
Illegal offset type
12 changes: 7 additions & 5 deletions Zend/tests/038.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ Trying to use lambda as array key
--FILE--
<?php

var_dump(array(function() { } => 1));
try {
var_dump(array(function() { } => 1));
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECTF--
Warning: Illegal offset type in %s on line %d
array(0) {
}
--EXPECT--
Illegal offset type
31 changes: 15 additions & 16 deletions Zend/tests/array_literal_next_element_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);

const FOO = [PHP_INT_MAX => 42, "foo"];
var_dump(FOO);

?>
--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)
try {
$array = [$i => 42, new stdClass];
var_dump($array);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
array(1) {
[%d]=>
int(42)
function test($x = [PHP_INT_MAX => 42, "foo"]) {}
try {
test();
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--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
48 changes: 20 additions & 28 deletions Zend/tests/array_unpack/already_occupied.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

var_dump([PHP_INT_MAX-1 => 0, ...[1, 2, 3]]);

const ARR = [1, 2, 3];
const ARR2 = [PHP_INT_MAX-1 => 0, ...ARR];
var_dump(ARR2);

?>
--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)
try {
var_dump([PHP_INT_MAX-1 => 0, ...$arr]);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

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)
try {
var_dump([PHP_INT_MAX-1 => 0, ...[1, 2, 3]]);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

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)
const ARR = [1, 2, 3];
function test($x = [PHP_INT_MAX-1 => 0, ...ARR]) {}
try {
test();
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--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
63 changes: 40 additions & 23 deletions Zend/tests/assign_dim_obj_null_return.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,42 @@ function test() {
$array = [PHP_INT_MAX => 42];
$true = true;

var_dump($array[] = 123);
var_dump($array[[]] = 123);
var_dump($array[new stdClass] = 123);
try {
var_dump($array[] = 123);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

try {
var_dump($array[[]] = 123);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

try {
var_dump($array[new stdClass] = 123);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($true[123] = 456);

var_dump($array[] += 123);
var_dump($array[[]] += 123);
var_dump($array[new stdClass] += 123);
try {
var_dump($array[] += 123);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

try {
var_dump($array[[]] += 123);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

try {
var_dump($array[new stdClass] += 123);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($true[123] += 456);

try {
Expand All @@ -33,26 +61,15 @@ test();

?>
--EXPECTF--
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
NULL

Warning: Illegal offset type in %s on line %d
NULL

Warning: Illegal offset type in %s on line %d
NULL
Cannot add element to the array as the next element is already occupied
Illegal offset type
Illegal offset type

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

Warning: Illegal offset type in %s on line %d
NULL

Warning: Illegal offset type in %s on line %d
NULL
Cannot add element to the array as the next element is already occupied
Illegal offset type
Illegal offset type

Warning: Cannot use a scalar value as an array in %s on line %d
NULL
Expand Down
21 changes: 13 additions & 8 deletions Zend/tests/assign_ref_error_var_handling.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 5 additions & 3 deletions Zend/tests/bug36303.phpt
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
--TEST--
Bug #36303 (foreach on error_zval produces segfault)
--XFAIL--
TODO: ERROR zval still possible?
--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
8 changes: 6 additions & 2 deletions Zend/tests/bug47836.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions Zend/tests/bug52237.phpt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
--TEST--
Bug #52237 (Crash when passing the reference of the property of a non-object)
--XFAIL--
TODO: ERROR zval still possible?
--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)
8 changes: 6 additions & 2 deletions Zend/tests/bug69017.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 21 additions & 11 deletions Zend/tests/bug71841.phpt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const C2 = [C1, [] => 1];

?>
--EXPECTF--
Fatal error: Uncaught Error: Illegal offset type in %s:%d
Fatal error: Uncaught TypeError: Illegal offset type in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
Loading