From: Quentin Smith Date: Fri, 14 Oct 2016 19:13:30 +0000 (-0400) Subject: strconv: strip \r in raw strings passed to Unquote X-Git-Tag: go1.8beta1~836 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7c46f0349844f950cc811727ded2393cff7e0369;p=gostls13.git strconv: strip \r in raw strings passed to Unquote 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 --- diff --git a/src/strconv/quote.go b/src/strconv/quote.go index becfe1df07..76c5c2a1cb 100644 --- a/src/strconv/quote.go +++ b/src/strconv/quote.go @@ -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 != '\'' { diff --git a/src/strconv/quote_test.go b/src/strconv/quote_test.go index 10735e316c..a4b5804fc8 100644 --- a/src/strconv/quote_test.go +++ b/src/strconv/quote_test.go @@ -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) } }