From 149029188a16bcc6b7dfda9ac07f7f4b99bd96c2 Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Fri, 15 May 2015 11:07:59 +0200 Subject: [PATCH] Bugfix: unexpected string endings --- json/parse.go | 5 ++--- json/parse_test.go | 7 ++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/json/parse.go b/json/parse.go index b8d3b15..5217a69 100644 --- a/json/parse.go +++ b/json/parse.go @@ -310,13 +310,12 @@ func (p *Parser) consumeStringToken() bool { p.r.Move(1) break } else if c == 0 { - break + return false } else if c == '\\' { if p.r.Peek(1) != 0 { p.r.Move(1) } else { - p.r.Move(1) - break + return false } } p.r.Move(1) diff --git a/json/parse_test.go b/json/parse_test.go index 08d3c41..1cc59ab 100644 --- a/json/parse_test.go +++ b/json/parse_test.go @@ -19,7 +19,7 @@ func assertGrammars(t *testing.T, s string, grammartypes ...GrammarType) { tt, _ := p.Next() if tt == ErrorGrammar { assert.Equal(t, io.EOF, p.Err(), "error must be EOF in "+stringify) - assert.Equal(t, len(grammartypes), i, "when error occurred we must be at the end in "+stringify) + assert.Equal(t, len(grammartypes), i, "when error occurred we must be at the end in "+s) break } else if tt == WhitespaceGrammar { continue @@ -99,10 +99,6 @@ func TestGrammars(t *testing.T) { assertGrammars(t, `{"a": [1, 2], "b": {"c": 3}}`, StartObjectGrammar, StringGrammar, StartArrayGrammar, NumberGrammar, NumberGrammar, EndArrayGrammar, StringGrammar, StartObjectGrammar, StringGrammar, NumberGrammar, EndObjectGrammar, EndObjectGrammar) assertGrammars(t, "[null,]", StartArrayGrammar, LiteralGrammar, EndArrayGrammar) - // early endings - assertGrammars(t, "\"a", StringGrammar) - assertGrammars(t, "\"a\\", StringGrammar) - assert.Equal(t, "Whitespace", WhitespaceGrammar.String()) assert.Equal(t, "Invalid(100)", GrammarType(100).String()) assert.Equal(t, "Value", ValueState.String()) @@ -121,6 +117,7 @@ func TestGrammarsError(t *testing.T) { assertGrammarsError(t, "{\"a\" 1}", ErrBadObjectDeclaration) assertGrammarsError(t, "1.", ErrNoComma) assertGrammarsError(t, "1e+", ErrNoComma) + assertGrammarsError(t, `{"":"`, io.EOF) } func TestStates(t *testing.T) {