]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: strip \r in raw strings passed to Unquote
authorQuentin Smith <quentin@golang.org>
Fri, 14 Oct 2016 19:13:30 +0000 (15:13 -0400)
committerQuentin Smith <quentin@golang.org>
Mon, 17 Oct 2016 18:34:02 +0000 (18:34 +0000)
To match the language spec, strconv.Unquote needs to strip carriage
returns from the raw string.

Also fixes TestUnquote to not be a noop.

Fixes #15997

Change-Id: I2456f50f2ad3830f37e545f4f6774ced9fe609d7
Reviewed-on: https://go-review.googlesource.com/31210
Reviewed-by: Robert Griesemer <gri@golang.org>
src/strconv/quote.go
src/strconv/quote_test.go

index becfe1df075669aa845e1904398895729c5fe7e3..76c5c2a1cbb4d54f745952622c2b3fcbf8bcffe4 100644 (file)
@@ -362,6 +362,16 @@ func Unquote(s string) (string, error) {
                if contains(s, '`') {
                        return "", ErrSyntax
                }
+               if contains(s, '\r') {
+                       // -1 because we know there is at least one \r to remove.
+                       buf := make([]byte, 0, len(s)-1)
+                       for i := 0; i < len(s); i++ {
+                               if s[i] != '\r' {
+                                       buf = append(buf, s[i])
+                               }
+                       }
+                       return string(buf), nil
+               }
                return s, nil
        }
        if quote != '"' && quote != '\'' {
index 10735e316c7dd830da71738fadca5dea336b82d9..a4b5804fc89c9ca1cfdf49e10a990a71a0c2a1e4 100644 (file)
@@ -274,6 +274,7 @@ var unquotetests = []unQuoteTest{
        {"`\n`", "\n"},
        {"`     `", `   `},
        {"` `", ` `},
+       {"`a\rb`", "ab"},
 }
 
 var misquoted = []string{
@@ -306,7 +307,7 @@ var misquoted = []string{
 
 func TestUnquote(t *testing.T) {
        for _, tt := range unquotetests {
-               if out, err := Unquote(tt.in); err != nil && out != tt.out {
+               if out, err := Unquote(tt.in); err != nil || out != tt.out {
                        t.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt.in, out, err, tt.out)
                }
        }