]> Cypherpunks.ru repositories - gostls13.git/commitdiff
strings: avoid utf8.RuneError mangling in Split
authorJoe Tsai <joetsai@digital-static.net>
Thu, 23 Jun 2022 03:57:50 +0000 (20:57 -0700)
committerJoseph Tsai <joetsai@digital-static.net>
Mon, 8 Aug 2022 20:44:51 +0000 (20:44 +0000)
Split should only split strings and not perform mangling
of invalid UTF-8 into ut8.RuneError.
The prior behavior is clearly a bug since mangling is not
performed in all other situations (e.g., separator is non-empty).

Fixes #53511

Change-Id: I112a2ef15ee46ddecda015ee14bca04cd76adfbf
Reviewed-on: https://go-review.googlesource.com/c/go/+/413715
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/bytes/bytes_test.go
src/strings/strings.go
src/strings/strings_test.go

index 985aa0b1472c4c1dd2e81105d2caa745e85a0d15..b407fe8a2d91e11aec4cd6c84731b3fdf74c7854 100644 (file)
@@ -755,6 +755,8 @@ var splittests = []SplitTest{
        {"123", "", 2, []string{"1", "23"}},
        {"123", "", 17, []string{"1", "2", "3"}},
        {"bT", "T", math.MaxInt / 4, []string{"b", ""}},
+       {"\xff-\xff", "", -1, []string{"\xff", "-", "\xff"}},
+       {"\xff-\xff", "-", -1, []string{"\xff", "\xff"}},
 }
 
 func TestSplit(t *testing.T) {
index 1dc4238522ad9def5a7da56c5b3c151381479e7c..013d71842647b7f188b3c6ddbe2f02f1fd3931e1 100644 (file)
@@ -15,7 +15,7 @@ import (
 
 // explode splits s into a slice of UTF-8 strings,
 // one string per Unicode character up to a maximum of n (n < 0 means no limit).
-// Invalid UTF-8 sequences become correct encodings of U+FFFD.
+// Invalid UTF-8 bytes are sliced individually.
 func explode(s string, n int) []string {
        l := utf8.RuneCountInString(s)
        if n < 0 || n > l {
@@ -23,12 +23,9 @@ func explode(s string, n int) []string {
        }
        a := make([]string, n)
        for i := 0; i < n-1; i++ {
-               ch, size := utf8.DecodeRuneInString(s)
+               _, size := utf8.DecodeRuneInString(s)
                a[i] = s[:size]
                s = s[size:]
-               if ch == utf8.RuneError {
-                       a[i] = string(utf8.RuneError)
-               }
        }
        if n > 0 {
                a[n-1] = s
index 9e7fb85ddf0c4455ca31ee6eea61178e3b3df93b..a1604c2c47328d2e853dfeb1d79c22fddc796cfa 100644 (file)
@@ -406,6 +406,8 @@ var splittests = []SplitTest{
        {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
        {"1 2", " ", 3, []string{"1", "2"}},
        {"", "T", math.MaxInt / 4, []string{""}},
+       {"\xff-\xff", "", -1, []string{"\xff", "-", "\xff"}},
+       {"\xff-\xff", "-", -1, []string{"\xff", "\xff"}},
 }
 
 func TestSplit(t *testing.T) {