-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e26afd5
New Attempt to resolve Bug:37676
bp1222 cc3e7c9
fix tests
bp1222 173d425
add test for return from function, rather than through each
bp1222 fffea43
use dim_type rather than forcing tmp-var
bp1222 05cba57
remove suppression test
bp1222 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.