]> granicus.if.org Git - php/commitdiff
Throw notice on array access on illegal type
authorNikita Popov <nikita.ppv@gmail.com>
Wed, 10 Jul 2019 08:28:58 +0000 (10:28 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 10 Jul 2019 10:02:14 +0000 (12:02 +0200)
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

19 files changed:
UPGRADING
UPGRADING.INTERNALS
Zend/tests/024.phpt
Zend/tests/033.phpt
Zend/tests/assign_to_var_003.phpt
Zend/tests/call_user_func_007.phpt
Zend/tests/dereference_002.phpt
Zend/tests/dereference_010.phpt
Zend/tests/dereference_014.phpt
Zend/tests/isset_003.phpt
Zend/tests/offset_bool.phpt
Zend/tests/offset_long.phpt
Zend/tests/offset_null.phpt
Zend/zend_execute.c
ext/spl/tests/array_026.phpt
ext/spl/tests/bug62978.phpt
ext/standard/tests/array/bug31158.phpt
tests/lang/bug25922.phpt
tests/lang/passByReference_003.phpt

index 711bdd1d60d4bb650eab8b8d77372282e54ee0e0..a72e17c496affbc38e1e9dbd994e64abc75b9dd4 100644 (file)
--- 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
index b3d5e1d6a28e0dcfae0e7f7b07b93f78e3490430..48e3c07de5548151aa7c023ad9f82bc9269a237a 100644 (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
index 34dac0f1e36e5eeaf35d044d3f6a8cd1c4a10d9c..9e647d4231d0ae643eeeb5bd936d5cedc3ef8543 100644 (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: 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
index a76a322ccf30297f5311cf90f168509a50d31c33..1e7fca1e0d73e99df37ee1ae1e4dadcdddb425a0 100644 (file)
@@ -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
index b915c7231e1ebe5e4cbf78280601a8bad2a3d004..bbe1b372bb6244672fb0d109c0651719aad43ac6 100644 (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
index f73f14b1ff2450782c7d2be40c5dc27d2d1ed5c3..ed44320c3fd3c1e6ccc6c4f99f02a81a9f7b7cd6 100644 (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) {
 }
index d16e1bb483037352007e11993b5b407d71806e1e..7290df7714df2ec169d3d5edcc68395ce41c844f 100644 (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
index 981fe3116082eaa6bcda664d7a305021f40184f0..c63f6acaf96e613e95f291e06e899888c7413641 100644 (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
index f8910dd016c2952645374a92aa75774e8171b6da..189dca7a3896193d5534988c22cf6183cc39d3c2 100644 (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
index eac72f7e0a695f2d84356ab2c48b3254e180ccd3..06cbe3d51d629e0fad3d303958ccb19fbfdb27e7 100644 (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)
index a08dd5450c21b2412f702191c8b2cd838aa0d4f4..8ed9f28e433ca6c172db20eb7e1932f16effed3e 100644 (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
index 98b9b0f08b9754a27327d2f8789000da689e9958..4c6b3972d220eeb86889b50b2460b08e9f17b355 100644 (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
index c6ad6561dbdad1dc8bab3a803248b60ae9c2473c..ad0b1f30491297c5142f6ec7be67b056cee15bf8 100644 (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
index 0cda36e6ac481e3f8327e113c11d1268af2b5836..49f9003d31e6a542a76ae9a9dcb69d80a448b5e4 100644 (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);
        }
 }
index 9c79c57b660f42a59bb2368b138d916f9a7ffb83..8ff6aafb93c5d0cee9832350736a9247dbf0ed42 100644 (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"]=>
index 5c55507ad9906510f9bec72cb1a23a81154f3e6f..972bd07ce1ecdfc9d41783b847d470c912d8db61 100644 (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
index d9e65bdde9a9fa9b08ef5b3775bc19194a20ff21..62ba1cfaa3c6aa53193b10bef1e338659c509730 100644 (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
index 41fb135e03b2813f1409a64317f338a4dcf19859..796ef6df828f6d28ccb5448f8a1a2e675e0d6ed1 100644 (file)
@@ -20,4 +20,5 @@ test();
 ?>
 --EXPECT--
 Undefined variable: data
+Trying to access array offset on value of type null
 Undefined index here: ''
index be002031d79442efa825cde6dda1fac8f90312ae..ad9e1e39dec55c5f2ec59c83dd0887d5d88b847f 100644 (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