]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: fix CanBackquote for invalid UTF-8
authorVolker Dobler <dr.volker.dobler@gmail.com>
Tue, 15 Jul 2014 02:49:26 +0000 (19:49 -0700)
committerRob Pike <r@golang.org>
Tue, 15 Jul 2014 02:49:26 +0000 (19:49 -0700)
Make CanBackquote(invalid UTF-8) return false.

Also add two test which show that CanBackquote reports
true for strings containing a BOM.

Fixes #7572.

LGTM=r
R=golang-codereviews, bradfitz, r
CC=golang-codereviews
https://golang.org/cl/111780045

src/pkg/strconv/quote.go
src/pkg/strconv/quote_test.go

index aded7e5930c329992c1ed3b865e48d5bb2f5accc..89dda997504c1ef383f28f4c9172e289702cbb73 100644 (file)
@@ -143,9 +143,16 @@ func AppendQuoteRuneToASCII(dst []byte, r rune) []byte {
 // unchanged as a single-line backquoted string without control
 // characters other than space and tab.
 func CanBackquote(s string) bool {
-       for i := 0; i < len(s); i++ {
-               c := s[i]
-               if (c < ' ' && c != '\t') || c == '`' || c == '\u007F' {
+       for len(s) > 0 {
+               r, wid := utf8.DecodeRuneInString(s)
+               s = s[wid:]
+               if wid > 1 {
+                       continue // All multibyte runes are correctly encoded and assumed printable.
+               }
+               if r == utf8.RuneError {
+                       return false
+               }
+               if (r < ' ' && r != '\t') || r == '`' || r == '\u007F' {
                        return false
                }
        }
index e4b5b6b9fd2d0d2fb9e05ba5f9e1ba589d5b8458..24998191d7b1fd568e03b1b4dcc8b4f783773918 100644 (file)
@@ -146,6 +146,10 @@ var canbackquotetests = []canBackquoteTest{
        {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
        {`abcdefghijklmnopqrstuvwxyz`, true},
        {`☺`, true},
+       {"\x80", false},
+       {"a\xe0\xa0z", false},
+       {"\ufeffabc", true},
+       {"a\ufeffz", true},
 }
 
 func TestCanBackquote(t *testing.T) {