Skip to content

Commit

Permalink
parser: Don't panic on single eof token
Browse files Browse the repository at this point in the history
When the expressin is empty the lexer will return a slice of tokens with
just one EOF, there is a bug at parser that throws a panic trying to
access the last token. This change add the guard to prevent that.

Signed-off-by: Quique Llorente <[email protected]>
  • Loading branch information
qinqon committed Dec 2, 2021
1 parent b863b90 commit f4ed96a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nmpolicy/internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (p *parser) parse() (ast.Node, error) {
}
p.nextToken()
}
if p.lastNode == nil {
return ast.Node{}, nil
}
return *p.lastNode, nil
}

Expand Down
1 change: 1 addition & 0 deletions nmpolicy/internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func testParseFailures(t *testing.T) {
func testParseSuccess(t *testing.T) {
var tests = []test{
expectEmptyAST(fromTokens()),
expectEmptyAST(fromTokens(eof())),
expectAST(t, `
pos: 0
path:
Expand Down

0 comments on commit f4ed96a

Please sign in to comment.