Skip to content

Fix #37676 : Add E_WARNING when using array-index on non valid container #2031

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 5 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
3 changes: 2 additions & 1 deletion Zend/tests/assign_to_var_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var_dump($var1);

echo "Done\n";
?>
--EXPECT--
--EXPECTF--
Warning: Variable of type float does not accept array offsets in %s on line %d
NULL
NULL
Done
33 changes: 33 additions & 0 deletions Zend/tests/bug37676/1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Bug #37676 (Simple warnings)
--FILE--
<?php
$a = false;
$a[0];
@$a[0];
isset($a[0]);

$a = 1;
$a[0];
@$a[0];
isset($a[0]);

$a = null;
$a[0];
@$a[0];
isset($a[0]);

$a = 1;
$b = 2;
$a[0] + $b[0];
?>
--EXPECTF--
Warning: Variable of type bool does not accept array offsets in %s on line %d

Warning: Variable of type int does not accept array offsets in %s on line %d

Warning: Variable of type null does not accept array offsets in %s on line %d

Warning: Variable of type int does not accept array offsets in %s on line %d

Warning: Variable of type int does not accept array offsets in %s on line %d
9 changes: 9 additions & 0 deletions Zend/tests/bug37676/10.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
Bug #37676 (Coalesce non-warning)
--FILE--
<?php
$a = false;
var_dump($a[0][1] ?? 42); // Coalesce is fetched as an isset, which suppresses warnings.
?>
--EXPECTF--
int(42)
13 changes: 13 additions & 0 deletions Zend/tests/bug37676/11.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Bug #37676 (AST Const Access)
--FILE--
<?php
var_dump(null[0][1]);
var_dump(true[0][1]);
?>
--EXPECTF--
Warning: Variable of type null does not accept array offsets in %s on line %d
NULL

Warning: Variable of type bool does not accept array offsets in %s on line %d
NULL
27 changes: 27 additions & 0 deletions Zend/tests/bug37676/2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Bug #37676 (Chained warnings)
--FILE--
<?php
$a = false;
$a[0][1][2];

$a = 1;
$a[0][1][2];

$a = null;
$a[0][1][2];

$a = 1;
$b = 2;
$a[0][1][2] + $b[0][1][2];
?>
--EXPECTF--
Warning: Variable of type bool does not accept array offsets in %s on line %d

Warning: Variable of type int does not accept array offsets in %s on line %d

Warning: Variable of type null does not accept array offsets in %s on line %d

Warning: Variable of type int does not accept array offsets in %s on line %d

Warning: Variable of type int does not accept array offsets in %s on line %d
21 changes: 21 additions & 0 deletions Zend/tests/bug37676/3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Bug #37676 (Argument warnings)
--FILE--
<?php
function f ($a) {}

$a = false;
f($a[0][1][2]);

$a = 1;
f($a[0][1][2]);

$a = null;
f($a[0][1][2]);
?>
--EXPECTF--
Warning: Variable of type bool does not accept array offsets in %s on line %d

Warning: Variable of type int does not accept array offsets in %s on line %d

Warning: Variable of type null does not accept array offsets in %s on line %d
32 changes: 32 additions & 0 deletions Zend/tests/bug37676/4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Bug #37676 (List and Loop warnings)
--FILE--
<?php
$a = array('one', 'two', 3);

$a[0][1]; // Valid

$a[2][0]; // Integer Error

list($j, $k, list($l)) = $a; // Integer Error

while (list($k, $v) = each($a)) { // each dep, boolean err
var_dump($k, $v);
}
?>
--EXPECTF--
Warning: Variable of type int does not accept array offsets in %s on line %d

Warning: Variable of type int does not accept array offsets in %s on line %d

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in %s on line %d
int(0)
string(3) "one"
int(1)
string(3) "two"
int(2)
int(3)

Warning: Variable of type bool does not accept array offsets in %s on line %d

Warning: Variable of type bool does not accept array offsets in %s on line %d
19 changes: 19 additions & 0 deletions Zend/tests/bug37676/5.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Bug #37676 (Function warnings)
--FILE--
<?php
function foo() {
return null;
}

function foo_array() {
return [null];
}

foo()[0];
foo()[0][1];
?>
--EXPECTF--
Warning: Variable of type null does not accept array offsets in %s on line %d

Warning: Variable of type null does not accept array offsets in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/bug37676/6.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Bug #37676 (Complex warnings)
--FILE--
<?php
$a = [0 => null];
$b = [1 => 0];
$c = [2 => 1];
$d = [3 => $b];

$a[$b[$c[2]]][0];
$a[$d[3][1]][$b[1]];
?>
--EXPECTF--
Warning: Variable of type null does not accept array offsets in %s on line %d

Warning: Variable of type null does not accept array offsets in %s on line %d
9 changes: 9 additions & 0 deletions Zend/tests/bug37676/7.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
Bug #37676 (Constant warnings)
--FILE--
<?php
const FOO = null;
const BAR = FOO[0][1];
?>
--EXPECTF--
Warning: Variable of type null does not accept array offsets in %s on line %d
Copy link
Member

@nikic nikic Oct 16, 2017

Choose a reason for hiding this comment

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

Looks like this test is segfaulting on Travis.

I'd recommend running the newly added tests through valgrind, by using sapi/cli/php run-tests.php -P -m Zend/tests/bug37676 and see if anything else breaks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am unsure why this is happening on travis. I ran with valgrind on my mac, and all tests LEAK&FAIL, not just for my bug37676 directory (may be problems with git version of valgrind for 10.13) . I did detect a leak in linux valgrind, which I pushed.

23 changes: 23 additions & 0 deletions Zend/tests/bug37676/8.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Bug #37676 (Object warnings)
--FILE--
<?php
class FooArray implements ArrayAccess {
private $s = [];
function __construct(array $a) { $this->s = $a; }
function offsetSet ($k, $v) { $this->s[$k] = $v; }
function &offsetGet ($k) { return $this->s[$k]; }
function offsetExists ($k) { return isset($this->s[$k]); }
function offsetUnset ($k) { unset($this->s[$k]); }
}

$a = [1, 2, 3];
$fa = new FooArray($a);

$fa[0][1];
$fa[4][1];
?>
--EXPECTF--
Warning: Variable of type int does not accept array offsets in %s on line %d

Warning: Variable of type null does not accept array offsets in %s on line %d
82 changes: 82 additions & 0 deletions Zend/tests/bug37676/9.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
--TEST--
Bug #37676 (Reference warnings)
--FILE--
<?php
$int=5;
$bool=true;
$null=null;
$float=5.1;

$index_v=5;
$index_r=6;

$null_v = $null[$index_v];
$null_r =& $null[$index_r];
var_dump($null, $null_v, $null_r);

$int_v = $int[$index_v];
$int_r =& $int[$index_r];
var_dump($int, $int_v, $int_r);

$bool_v = $bool[$index_v];
$bool_r =& $bool[$index_r];
var_dump($bool, $bool_v, $bool_r);

$float_v = $float[$index_v];
$float_r =& $float[$index_r];
var_dump($float, $float_v, $float_r);

$null2 = null;
$null2_v = $null2[1][2][3];
$null2_r =& $null2[1][2][3];
var_dump($null2, $null2_v, $null2_r);
?>
--EXPECTF--
Warning: Variable of type null does not accept array offsets in %s on line %d
array(1) {
[6]=>
&NULL
}
NULL
NULL

Warning: Variable of type int does not accept array offsets in %s on line %d

Warning: Cannot use a scalar value as an array in %s on line %d

Notice: Undefined variable: int_r in %s on line %d
int(5)
NULL
NULL

Warning: Variable of type bool does not accept array offsets in %s on line %d

Warning: Cannot use a scalar value as an array in %s on line %d

Notice: Undefined variable: bool_r in %s on line %d
bool(true)
NULL
NULL

Warning: Variable of type float does not accept array offsets in %s on line %d

Warning: Cannot use a scalar value as an array in %s on line %d

Notice: Undefined variable: float_r in %s on line %d
float(5.1)
NULL
NULL

Warning: Variable of type null does not accept array offsets in %s on line %d
array(1) {
[1]=>
array(1) {
[2]=>
array(1) {
[3]=>
&NULL
}
}
}
NULL
NULL
4 changes: 4 additions & 0 deletions Zend/tests/bug39304.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ Bug #39304 (Segmentation fault with list unpacking of string offset)
?>
--EXPECTF--
Notice: Uninitialized string offset: 0 in %sbug39304.php on line %d

Warning: Variable of type string does not accept array offsets in %s on line %d

Warning: Variable of type string does not accept array offsets in %s on line %d
NULL
NULL
2 changes: 2 additions & 0 deletions Zend/tests/call_user_func_007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var_dump($a);
--EXPECTF--
Notice: Undefined offset: 0 in %s on line %d

Warning: Variable of type null does not accept array offsets in %s on line %d

Warning: Parameter 1 to foo() expected to be a reference, value given in %s on line %d
array(0) {
}
2 changes: 2 additions & 0 deletions Zend/tests/dereference_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ array(2) {
int(5)
}
int(1)

Warning: Variable of type int does not accept array offsets in %s on line %d
NULL

Notice: Undefined offset: 4 in %s on line %d
Expand Down
3 changes: 3 additions & 0 deletions Zend/tests/dereference_010.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ var_dump(b()[1]);

?>
--EXPECTF--
Warning: Variable of type int does not accept array offsets in %s on line %d
NULL

Warning: Variable of type int does not accept array offsets in %s on line %d
NULL

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in %s:%d
Expand Down
4 changes: 4 additions & 0 deletions Zend/tests/dereference_014.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ var_dump($h);

?>
--EXPECTF--
Warning: Variable of type null does not accept array offsets in %s on line %d

Notice: Trying to get property 'a' of non-object in %s on line %d
NULL

Warning: Variable of type null does not accept array offsets in %s on line %d

Notice: Trying to get property 'b' of non-object in %s on line %d
NULL
6 changes: 5 additions & 1 deletion Zend/tests/foreach_list_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ foreach($array as list(, $a)) {
}

?>
--EXPECT--
--EXPECTF--
int(1)
int(3)
string(1) "b"

Warning: Variable of type string does not accept array offsets in %s on line %d
NULL

Warning: Variable of type string does not accept array offsets in %s on line %d
NULL
2 changes: 2 additions & 0 deletions Zend/tests/list/list_reference_006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ object(StorageNoRef)#1 (1) {
}

Notice: Undefined offset: 2 in %s on line %d

Warning: Variable of type null does not accept array offsets in %s on line %d
object(StorageNoRef)#2 (1) {
["s":"StorageNoRef":private]=>
array(2) {
Expand Down
4 changes: 3 additions & 1 deletion Zend/tests/list/list_reference_007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $a = new StorageRef(['one' => 1, 'two' => 2]);
var_dump($a);

?>
--EXPECT--
--EXPECTF--
object(StorageRef)#1 (1) {
["s":"StorageRef":private]=>
array(2) {
Expand All @@ -39,6 +39,8 @@ object(StorageRef)#1 (1) {
int(2)
}
}

Warning: Variable of type null does not accept array offsets in %s on line %d
object(StorageRef)#2 (1) {
["s":"StorageRef":private]=>
array(3) {
Expand Down
Loading