Throw notice on array access on illegal type

No notice is thrown for list() accesses, because we did not come
to an agreement regarding patterns like

    while ([$key, $value] = yield $it->next()) { ... }

where silent null access may be desirable.

No effort is made to suppress multiple notices in access chains
likes $x[0][0][0], because the technical complexity this causes
does not seem worthwhile.

RFC: https://wiki.php.net/rfc/notice-for-non-valid-array-container
This commit is contained in:
Nikita Popov 2019-07-10 10:28:58 +02:00
parent a63422029e
commit c42b7dd6d3
19 changed files with 132 additions and 15 deletions

View File

@ -22,6 +22,9 @@ PHP 7.4 UPGRADE NOTES
========================================
- Core:
. Trying to use values of type null, bool, int, float or resource as an
array (such as $null["key"]) will now generate a notice. This does not
affect array accesses performed by list().
. get_declared_classes() no longer returns anonymous classes that haven't
been instantiated yet.
. "fn" is now a reserved keyword. In particular it can no longer be used as a
@ -30,7 +33,6 @@ PHP 7.4 UPGRADE NOTES
. Passing the result of a (non-reference) list() assignment by reference is
consistently disallowed now. Previously this worked if the right hand side
was a simple (CV) variable and did not occur as part of the list().
. Added --ini-path and --ini-dir options to php-config.
- BCMath:
. BCMath functions will now warn if a non well-formed number is passed, such

View File

@ -249,6 +249,7 @@ PHP 7.4 INTERNALS UPGRADE NOTES
PHP_LDAP_LFLAGS, PHP_LDAP_INCLUDE, PHP_LDAP_LIBS.
b. Unix build system changes
- Added --ini-path and --ini-dir options to php-config.
- configure --help now also outputs --program-suffix and --program-prefix
information by using the Autoconf AC_ARG_PROGRAM macro.
- Obsolescent macros AC_FUNC_VPRINTF and AC_FUNC_UTIME_NULL have been

View File

@ -16,19 +16,23 @@ var_dump($a->$b->{$c[1]});
?>
--EXPECTF--
Notice: Undefined variable: a in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Undefined variable: %s in %s on line %d
Notice: Undefined variable: a in %s on line %d
Notice: Undefined variable: %s in %s on line %d
Notice: Undefined variable: c in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Undefined variable: a in %s on line %d
int(1)
Notice: Undefined variable: %s in %s on line %d
Notice: Undefined variable: a in %s on line %d
Notice: Undefined variable: %s in %s on line %d
Notice: Undefined variable: b in %s on line %d
int(0)
Notice: Undefined variable: a in %s on line %d
@ -45,6 +49,8 @@ NULL
Notice: Undefined variable: c in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to get property '1' of non-object in %s on line %d
Notice: Trying to get property '' of non-object in %s on line %d

View File

@ -19,10 +19,40 @@ $arr[][]->bar = 2;
--EXPECTF--
Notice: Undefined variable: arr in %s on line %d
Notice: Undefined variable: arr in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Undefined variable: arr in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Undefined variable: arr in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to get property 'foo' of non-object in %s on line %d
Warning: Creating default object from empty value in %s on line %d

View File

@ -12,7 +12,8 @@ var_dump($var1);
echo "Done\n";
?>
--EXPECT--
--EXPECTF--
Notice: Trying to access array offset on value of type float in %s on line %d
NULL
NULL
Done

View File

@ -13,6 +13,8 @@ var_dump($a);
--EXPECTF--
Notice: Undefined offset: 0 in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Warning: Parameter 1 to foo() expected to be a reference, value given in %s on line %d
array(0) {
}

View File

@ -69,6 +69,8 @@ array(2) {
int(5)
}
int(1)
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Undefined offset: 4 in %s on line %d

View File

@ -21,7 +21,10 @@ var_dump(b()[1]);
?>
--EXPECTF--
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in %s:%d

View File

@ -27,8 +27,12 @@ var_dump($h);
?>
--EXPECTF--
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to get property 'a' of non-object in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to get property 'b' of non-object in %s on line %d
NULL

View File

@ -33,6 +33,8 @@ Notice: Undefined variable: c in %s on line %d
Notice: Undefined variable: d in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Notice: Trying to get property '' of non-object in %s on line %d
bool(false)
bool(true)

View File

@ -24,14 +24,31 @@ var_dump($bool[$arr]);
echo "Done\n";
?>
--EXPECT--
--EXPECTF--
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Notice: Trying to access array offset on value of type bool in %s on line %d
NULL
Done

View File

@ -24,14 +24,31 @@ var_dump($long[$arr]);
echo "Done\n";
?>
--EXPECT--
--EXPECTF--
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Notice: Trying to access array offset on value of type int in %s on line %d
NULL
Done

View File

@ -24,14 +24,31 @@ var_dump($null[$arr]);
echo "Done\n";
?>
--EXPECT--
--EXPECTF--
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Done

View File

@ -2390,11 +2390,15 @@ try_string_offset:
}
} else {
if (type != BP_VAR_IS && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
ZVAL_UNDEFINED_OP1();
container = ZVAL_UNDEFINED_OP1();
}
if (/*dim_type == IS_CV &&*/ UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
ZVAL_UNDEFINED_OP2();
}
if (!is_list && type != BP_VAR_IS) {
zend_error(E_NOTICE, "Trying to access array offset on value of type %s",
zend_zval_type_name(container));
}
ZVAL_NULL(result);
}
}

View File

@ -8,8 +8,10 @@ $test['d1']['d3'] = 'world';
var_dump($test, $test3['mmmmm']);
?>
--EXPECTF--
Notice: Undefined variable: test3 in %s%earray_026.php on line %d
object(ArrayObject)#%d (1) {
Notice: Undefined variable: test3 in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
object(ArrayObject)#1 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["d1"]=>

View File

@ -32,6 +32,8 @@ Notice: Undefined index: epic_magic in %sbug62978.php on line %d
NULL
Notice: Undefined variable: c in %sbug62978.php on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
NULL
Notice: Undefined index: epic_magic in %sbug62978.php on line %d

View File

@ -15,4 +15,6 @@ echo "ok\n";
?>
--EXPECTF--
Notice: Undefined variable: GLOBALS in %sbug31158.php on line 6
Notice: Trying to access array offset on value of type null in %sbug31158.php on line 6
ok

View File

@ -20,4 +20,5 @@ test();
?>
--EXPECT--
Undefined variable: data
Trying to access array offset on value of type null
Undefined index here: ''

View File

@ -25,14 +25,16 @@ var_dump($undef2)
--EXPECTF--
Passing undefined by value
Notice: Undefined variable: undef1 in %s on line 13
Notice: Undefined variable: undef1 in %s on line %d
Notice: Trying to access array offset on value of type null in %s on line %d
Inside passbyVal call:
NULL
After call
Notice: Undefined variable: undef1 in %s on line 15
Notice: Undefined variable: undef1 in %s on line %d
NULL
Passing undefined by reference