From 986a8442c966d3c90ccccc74c01a508d06a3fa47 Mon Sep 17 00:00:00 2001 From: mido Date: Thu, 24 Mar 2022 07:22:25 -0700 Subject: [PATCH] Fix panic if the condition is empty --- parser/parser.go | 4 ++++ parser/parser_test.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/parser/parser.go b/parser/parser.go index 370a83d..d3563b0 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -814,6 +814,10 @@ func (p *parser) registerInfix(tokenType token.Type, fn infixParseFn) { } func (p *parser) confrimIfCondition(v ast.Expression) (returnData bool) { + if v == nil { + p.invalidIfCondition("missing condition in if statement") + return + } _, ok := v.(ast.Comparable) if !ok { p.invalidIfCondition(v.String()) diff --git a/parser/parser_test.go b/parser/parser_test.go index ec57e55..7fe220e 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -1114,6 +1114,14 @@ func Test_Break_IfCondtion(t *testing.T) { r.Error(err) } +func Test_Empty_IfCondtion(t *testing.T) { + r := require.New(t) + input := `<% if() { v } %>` + + _, err := Parse(input) + r.Error(err) +} + func Test_Continue_Function(t *testing.T) { r := require.New(t) input := `<% fn(x, y) { continue } %>`