From 29a96735397827a2923c9b669ee3188d601d9153 Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Sun, 17 Jul 2016 15:16:15 -0400 Subject: [PATCH] Fix parsing single quote within double quotes Found with go-fuzz Signed-off-by: Jonathan Rudenberg --- decode.go | 3 ++- json5_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/decode.go b/decode.go index 2b937e1..fa359d0 100644 --- a/decode.go +++ b/decode.go @@ -1189,6 +1189,7 @@ func unquoteBytes(s []byte) (t []byte, ok bool) { if len(s) < 2 || (s[0] != '"' && s[0] != '\'') || (s[len(s)-1] != '"' && s[len(s)-1] != '\'') { return } + orig := s s = s[1 : len(s)-1] // Check for unusual characters. If there are none, @@ -1287,7 +1288,7 @@ func unquoteBytes(s []byte) (t []byte, ok bool) { } // Quote, control characters are invalid. - case s[0] == '"' && c == '"', s[0] == '\'' && c == '\'', c < ' ': + case orig[0] == '"' && c == '"', orig[0] == '\'' && c == '\'', c < ' ': return // ASCII diff --git a/json5_test.go b/json5_test.go index 333537d..7f0bbcb 100644 --- a/json5_test.go +++ b/json5_test.go @@ -130,3 +130,16 @@ func TestJSON5Decode(t *testing.T) { return nil }) } + +// found with go-fuzz +func TestQuotedQuote(t *testing.T) { + var v struct { + E string + } + if err := Unmarshal([]byte(`{e:"'"}`), &v); err != nil { + t.Error(err) + } + if v.E != "'" { + t.Errorf(`expected "'", got %q`, v.E) + } +}