From c42b7dd6d32b43304b76452add158a2ef325d494 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 10 Jul 2019 10:28:58 +0200 Subject: [PATCH] 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 --- UPGRADING | 4 +++- UPGRADING.INTERNALS | 1 + Zend/tests/024.phpt | 14 ++++++++---- Zend/tests/033.phpt | 30 ++++++++++++++++++++++++++ Zend/tests/assign_to_var_003.phpt | 3 ++- Zend/tests/call_user_func_007.phpt | 2 ++ Zend/tests/dereference_002.phpt | 2 ++ Zend/tests/dereference_010.phpt | 3 +++ Zend/tests/dereference_014.phpt | 4 ++++ Zend/tests/isset_003.phpt | 2 ++ Zend/tests/offset_bool.phpt | 19 +++++++++++++++- Zend/tests/offset_long.phpt | 19 +++++++++++++++- Zend/tests/offset_null.phpt | 19 +++++++++++++++- Zend/zend_execute.c | 6 +++++- ext/spl/tests/array_026.phpt | 6 ++++-- ext/spl/tests/bug62978.phpt | 2 ++ ext/standard/tests/array/bug31158.phpt | 2 ++ tests/lang/bug25922.phpt | 1 + tests/lang/passByReference_003.phpt | 6 ++++-- 19 files changed, 131 insertions(+), 14 deletions(-) diff --git a/UPGRADING b/UPGRADING index 711bdd1d60..a72e17c496 100644 --- a/UPGRADING +++ b/UPGRADING @@ -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 diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index b3d5e1d6a2..48e3c07de5 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -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 diff --git a/Zend/tests/024.phpt b/Zend/tests/024.phpt index 34dac0f1e3..9e647d4231 100644 --- a/Zend/tests/024.phpt +++ b/Zend/tests/024.phpt @@ -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: c in %s on line %d -Notice: Undefined variable: %s 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 diff --git a/Zend/tests/033.phpt b/Zend/tests/033.phpt index a76a322ccf..1e7fca1e0d 100644 --- a/Zend/tests/033.phpt +++ b/Zend/tests/033.phpt @@ -19,10 +19,40 @@ $arr[][]->bar = 2; --EXPECTF-- 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 diff --git a/Zend/tests/assign_to_var_003.phpt b/Zend/tests/assign_to_var_003.phpt index b915c7231e..bbe1b372bb 100644 --- a/Zend/tests/assign_to_var_003.phpt +++ b/Zend/tests/assign_to_var_003.phpt @@ -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 diff --git a/Zend/tests/call_user_func_007.phpt b/Zend/tests/call_user_func_007.phpt index f73f14b1ff..ed44320c3f 100644 --- a/Zend/tests/call_user_func_007.phpt +++ b/Zend/tests/call_user_func_007.phpt @@ -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) { } diff --git a/Zend/tests/dereference_002.phpt b/Zend/tests/dereference_002.phpt index d16e1bb483..7290df7714 100644 --- a/Zend/tests/dereference_002.phpt +++ b/Zend/tests/dereference_002.phpt @@ -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 diff --git a/Zend/tests/dereference_010.phpt b/Zend/tests/dereference_010.phpt index 981fe31160..c63f6acaf9 100644 --- a/Zend/tests/dereference_010.phpt +++ b/Zend/tests/dereference_010.phpt @@ -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 diff --git a/Zend/tests/dereference_014.phpt b/Zend/tests/dereference_014.phpt index f8910dd016..189dca7a38 100644 --- a/Zend/tests/dereference_014.phpt +++ b/Zend/tests/dereference_014.phpt @@ -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 diff --git a/Zend/tests/isset_003.phpt b/Zend/tests/isset_003.phpt index eac72f7e0a..06cbe3d51d 100644 --- a/Zend/tests/isset_003.phpt +++ b/Zend/tests/isset_003.phpt @@ -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) diff --git a/Zend/tests/offset_bool.phpt b/Zend/tests/offset_bool.phpt index a08dd5450c..8ed9f28e43 100644 --- a/Zend/tests/offset_bool.phpt +++ b/Zend/tests/offset_bool.phpt @@ -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 diff --git a/Zend/tests/offset_long.phpt b/Zend/tests/offset_long.phpt index 98b9b0f08b..4c6b3972d2 100644 --- a/Zend/tests/offset_long.phpt +++ b/Zend/tests/offset_long.phpt @@ -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 diff --git a/Zend/tests/offset_null.phpt b/Zend/tests/offset_null.phpt index c6ad6561db..ad0b1f3049 100644 --- a/Zend/tests/offset_null.phpt +++ b/Zend/tests/offset_null.phpt @@ -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 diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 0cda36e6ac..49f9003d31 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -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); } } diff --git a/ext/spl/tests/array_026.phpt b/ext/spl/tests/array_026.phpt index 9c79c57b66..8ff6aafb93 100644 --- a/ext/spl/tests/array_026.phpt +++ b/ext/spl/tests/array_026.phpt @@ -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"]=> diff --git a/ext/spl/tests/bug62978.phpt b/ext/spl/tests/bug62978.phpt index 5c55507ad9..972bd07ce1 100644 --- a/ext/spl/tests/bug62978.phpt +++ b/ext/spl/tests/bug62978.phpt @@ -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 diff --git a/ext/standard/tests/array/bug31158.phpt b/ext/standard/tests/array/bug31158.phpt index d9e65bdde9..62ba1cfaa3 100644 --- a/ext/standard/tests/array/bug31158.phpt +++ b/ext/standard/tests/array/bug31158.phpt @@ -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 diff --git a/tests/lang/bug25922.phpt b/tests/lang/bug25922.phpt index 41fb135e03..796ef6df82 100644 --- a/tests/lang/bug25922.phpt +++ b/tests/lang/bug25922.phpt @@ -20,4 +20,5 @@ test(); ?> --EXPECT-- Undefined variable: data +Trying to access array offset on value of type null Undefined index here: '' diff --git a/tests/lang/passByReference_003.phpt b/tests/lang/passByReference_003.phpt index be002031d7..ad9e1e39de 100644 --- a/tests/lang/passByReference_003.phpt +++ b/tests/lang/passByReference_003.phpt @@ -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 -- 2.40.0