From 64ff1ea4d585bc1fca65e6331eb8239f2ff31845 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Thu, 30 Jun 2016 16:21:25 +0200 Subject: [PATCH] Don't hang when reading an invalid rvalue (#77) Fixes #76 --- lexer.go | 1 + lexer_test.go | 14 ++++++++++++++ parser_test.go | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/lexer.go b/lexer.go index e7d5d24..3235753 100644 --- a/lexer.go +++ b/lexer.go @@ -234,6 +234,7 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn { return l.lexKey } + return l.errorf("no value can start with %c", next) } l.emit(tokenEOF) diff --git a/lexer_test.go b/lexer_test.go index 382e05a..d6ddc07 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -692,3 +692,17 @@ func TestInvalidFloat(t *testing.T) { token{Position{1, 7}, tokenEOF, ""}, }) } + +func TestLexUnknownRvalue(t *testing.T) { + testFlow(t, `a = !b`, []token{ + token{Position{1, 1}, tokenKey, "a"}, + token{Position{1, 3}, tokenEqual, "="}, + token{Position{1, 5}, tokenError, "no value can start with !"}, + }) + + testFlow(t, `a = \b`, []token{ + token{Position{1, 1}, tokenKey, "a"}, + token{Position{1, 3}, tokenEqual, "="}, + token{Position{1, 5}, tokenError, `no value can start with \`}, + }) +} diff --git a/parser_test.go b/parser_test.go index 1ea5924..784cdb8 100644 --- a/parser_test.go +++ b/parser_test.go @@ -299,6 +299,18 @@ func TestArrayNestedStrings(t *testing.T) { }) } +func TestParseUnknownRvalue(t *testing.T) { + _, err := Load("a = !bssss") + if err == nil { + t.Error("Expecting a parse error") + } + + _, err = Load("a = /b") + if err == nil { + t.Error("Expecting a parse error") + } +} + func TestMissingValue(t *testing.T) { _, err := Load("a = ") if err.Error() != "(1, 5): expecting a value" {