From 7620272ed63390e979cf5882d2fa0506fe2a8db5 Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Sun, 17 Jul 2016 15:56:20 -0400 Subject: [PATCH] Fix escaped carriage return parsing Found with go-fuzz Signed-off-by: Jonathan Rudenberg --- json5_test.go | 11 ++++++++++- scanner.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/json5_test.go b/json5_test.go index 7f0bbcb..4e8eeb1 100644 --- a/json5_test.go +++ b/json5_test.go @@ -131,7 +131,8 @@ func TestJSON5Decode(t *testing.T) { }) } -// found with go-fuzz +// The tests below this comment were found with go-fuzz + func TestQuotedQuote(t *testing.T) { var v struct { E string @@ -143,3 +144,11 @@ func TestQuotedQuote(t *testing.T) { t.Errorf(`expected "'", got %q`, v.E) } } + +func TestInvalidNewline(t *testing.T) { + expected := "invalid character '\\n' in string literal" + var v interface{} + if err := Unmarshal([]byte("{a:'\\\r0\n'}"), &v); err == nil || err.Error() != expected { + t.Errorf("expected error %q, got %s", expected, err) + } +} diff --git a/scanner.go b/scanner.go index b5d1715..283459a 100644 --- a/scanner.go +++ b/scanner.go @@ -500,8 +500,8 @@ func stateInStringEsc(resume func(s *scanner, c byte) int) func(s *scanner, c by // stateInStringEscCR is the state after reading `"\\r` during a quoted string. func stateInStringEscCR(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { return func(s *scanner, c byte) int { + s.step = resume if c == '\n' { - s.step = resume return scanContinue } return resume(s, c)