]> granicus.if.org Git - php/commitdiff
Add warning and convert to exception in string offset assignment:
authorGeorge Peter Banyard <girgias@php.net>
Tue, 7 Jan 2020 20:51:34 +0000 (21:51 +0100)
committerGeorge Peter Banyard <girgias@php.net>
Tue, 7 Jan 2020 20:54:42 +0000 (21:54 +0100)
Convert the empty string assignment to an Error as per RFC [1]
Add a warning that only the first byte will be assigned to the offset if provided
a needle that is longer than one byte.

[1] https://wiki.php.net/rfc/engine_warnings

Zend/tests/bug71572.phpt
Zend/tests/indexing_001.phpt
Zend/tests/str_offset_004.phpt
Zend/zend_execute.c
ext/opcache/jit/zend_jit_helpers.c
tests/lang/bug22592.phpt

index 063a98fcb917ec2b166d8492a9bd6286fd009161..f4079e55f89a03c2d19cc280d155d9851ae3db6b 100644 (file)
@@ -4,22 +4,31 @@ Bug #71572: String offset assignment from an empty string inserts null byte
 <?php
 
 $str = "abc";
-var_dump($str[0] = "");
-var_dump($str[1] = "");
-var_dump($str[3] = "");
-var_dump($str[10] = "");
+try {
+    var_dump($str[0] = "");
+} catch (\Error $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump($str[1] = "");
+} catch (\Error $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump($str[3] = "");
+} catch (\Error $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump($str[10] = "");
+} catch (\Error $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 var_dump($str);
 ?>
---EXPECTF--
-Warning: Cannot assign an empty string to a string offset in %s on line %d
-NULL
-
-Warning: Cannot assign an empty string to a string offset in %s on line %d
-NULL
-
-Warning: Cannot assign an empty string to a string offset in %s on line %d
-NULL
-
-Warning: Cannot assign an empty string to a string offset in %s on line %d
-NULL
+--EXPECT--
+Cannot assign an empty string to a string offset
+Cannot assign an empty string to a string offset
+Cannot assign an empty string to a string offset
+Cannot assign an empty string to a string offset
 string(3) "abc"
index 453b5ca86f80d24f33cecc70dc0c60d7ac726e3a..5a5fbef0546312f31d4a250295871d40e3cfda20 100644 (file)
@@ -51,8 +51,6 @@ foreach ($testvalues as $testvalue) {
     var_dump ($testvalue);
 }
 
-
-echo "\nDone";
 ?>
 --EXPECTF--
 *** Indexing - Testing value assignment with key ***
@@ -80,11 +78,15 @@ array(1) {
 Warning: Illegal string offset 'foo' in %s on line %d
 
 Warning: Array to string conversion in %s on line %d
+
+Warning: Only the first byte will be assigned to the string offset in %s on line %d
 string(1) "A"
 
 Warning: Illegal string offset 'foo' in %s on line %d
 
 Warning: Array to string conversion in %s on line %d
+
+Warning: Only the first byte will be assigned to the string offset in %s on line %d
 string(1) "A"
 Cannot use a scalar value as an array
 float(0.1)
@@ -187,5 +189,3 @@ array(1) {
     int(1)
   }
 }
-
-Done
index 435ab235fa61dc963873b248ff67eb965c524534..a0f89e9186129b41b97657c77ac9efdefa71549d 100644 (file)
@@ -45,5 +45,7 @@ Warning: Illegal string offset:  -20 in %sstr_offset_004.php on line %d
 string(15) "abCZefghijPQmno"
 string(15) "AbCZefghijPQmno"
 string(21) "AbCZefghijPQmno     N"
+
+Warning: Only the first byte will be assigned to the string offset in %s on line %d
 string(21) "AbCZefghijPQmno    UN"
 string(21) "AbCZefghijPQmno   nUN"
index 4821e2a1e4286a48e5d914663f964ac40568b71d..cae63b06c3eabee1b5745dc64630213bb524db97 100644 (file)
@@ -1597,14 +1597,18 @@ static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim,
                string_len = Z_STRLEN_P(value);
                c = (zend_uchar)Z_STRVAL_P(value)[0];
        }
-
-       if (string_len == 0) {
-               /* Error on empty input string */
-               zend_error(E_WARNING, "Cannot assign an empty string to a string offset");
-               if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
-                       ZVAL_NULL(EX_VAR(opline->result.var));
+       
+       if (string_len != 1) {
+               if (string_len == 0) {
+                       /* Error on empty input string */
+                       zend_throw_error(NULL, "Cannot assign an empty string to a string offset");
+                       if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
+                               ZVAL_NULL(EX_VAR(opline->result.var));
+                       }
+                       return;
                }
-               return;
+
+               zend_error(E_WARNING, "Only the first byte will be assigned to the string offset");
        }
 
        if (offset < 0) { /* Handle negative offset */
index e2ba36c9eee74391c01e87334cbffae55a2e6951..c04985110c5fda5ebc36d02c15c7e90e19dd8bce 100644 (file)
@@ -895,13 +895,18 @@ static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim,
                c = (zend_uchar)Z_STRVAL_P(value)[0];
        }
 
-       if (string_len == 0) {
-               /* Error on empty input string */
-               zend_error(E_WARNING, "Cannot assign an empty string to a string offset");
-               if (result) {
-                       ZVAL_NULL(result);
+
+       if (string_len != 1) {
+               if (string_len == 0) {
+                       /* Error on empty input string */
+                       zend_throw_error(NULL, "Cannot assign an empty string to a string offset");
+                       if (result) {
+                               ZVAL_NULL(result);
+                       }
+                       return;
                }
-               return;
+
+               zend_error(E_WARNING, "Only the first byte will be assigned to the string offset");
        }
 
        if (offset < 0) { /* Handle negative offset */
index 4614efc1692b768aa980926c6bb2a5117bcfc0ad..6999c0bccf2b5f8776952c1d23123a6e0009a654 100644 (file)
@@ -39,9 +39,11 @@ var_dump($result);
 string(5) "* *-*"
 string(7) "* *-* *"
 string(7) "*4*-* *"
+[Only the first byte will be assigned to the string offset]
 string(7) "*4*s* *"
 string(8) "*4*s* *0"
 string(8) "*-*-* *0"
+[Only the first byte will be assigned to the string offset]
 string(8) "*-*s*s*0"
 string(8) "4-4s4s*0"
 string(9) "4-4s4s505"