if !contains(s, '\\') && !contains(s, quote) {
switch quote {
case '"':
- return s, nil
+ if utf8.ValidString(s) {
+ return s, nil
+ }
case '\'':
r, size := utf8.DecodeRuneInString(s)
if size == len(s) && (r != utf8.RuneError || size != 1) {
}
}
+// Issue 23685: invalid UTF-8 should not go through the fast path.
+func TestUnquoteInvalidUTF8(t *testing.T) {
+ tests := []struct {
+ in string
+
+ // one of:
+ want string
+ wantErr string
+ }{
+ {in: `"foo"`, want: "foo"},
+ {in: `"foo`, wantErr: "invalid syntax"},
+ {in: `"` + "\xc0" + `"`, want: "\xef\xbf\xbd"},
+ {in: `"a` + "\xc0" + `"`, want: "a\xef\xbf\xbd"},
+ {in: `"\t` + "\xc0" + `"`, want: "\t\xef\xbf\xbd"},
+ }
+ for i, tt := range tests {
+ got, err := Unquote(tt.in)
+ var gotErr string
+ if err != nil {
+ gotErr = err.Error()
+ }
+ if gotErr != tt.wantErr {
+ t.Errorf("%d. Unquote(%q) = err %v; want %q", i, tt.in, err, tt.wantErr)
+ }
+ if tt.wantErr == "" && err == nil && got != tt.want {
+ t.Errorf("%d. Unquote(%q) = %02x; want %02x", i, tt.in, []byte(got), []byte(tt.want))
+ }
+ }
+}
+
func BenchmarkUnquoteEasy(b *testing.B) {
for i := 0; i < b.N; i++ {
Unquote(`"Give me a rock, paper and scissors and I will move the world."`)