From a34fc5f051b108c25a8bcc0cfa558fd6d65b2cb0 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Tue, 10 Dec 2013 17:34:11 +0100 Subject: [PATCH] Don't allow invalid escape sequences --- lexer.go | 3 +++ lexer_test.go | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lexer.go b/lexer.go index 679bb4d..c3a145c 100644 --- a/lexer.go +++ b/lexer.go @@ -358,6 +358,9 @@ func lexString(l *lexer) stateFn { return l.errorf("invalid unicode escape: \\u" + code) } growing_string += string(rune(intcode)) + } else if l.follow("\\") { + l.pos += 1 + return l.errorf("invalid escape sequence: \\" + string(l.peek())) } else { growing_string += string(l.peek()) } diff --git a/lexer_test.go b/lexer_test.go index e3b5daf..b7de9f5 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -292,6 +292,14 @@ func TestDoubleEqualKey(t *testing.T) { }) } +func TestInvalidEsquapeSequence(t *testing.T) { + testFlow(t, "foo = \"\\x\"", []token{ + token{tokenKey, "foo"}, + token{tokenEqual, "="}, + token{tokenError, "invalid escape sequence: \\x"}, + }) +} + func TestKeyEqualNumber(t *testing.T) { testFlow(t, "foo = 42", []token{ token{tokenKey, "foo"},