Skip to content

Commit

Permalink
Fix panic when parsing unattached comment (#565)
Browse files Browse the repository at this point in the history
* Fix panic when parsing unattached comment

---------

Signed-off-by: Charith Ellawala <charith@cerbos.dev>
  • Loading branch information
charithe authored Dec 2, 2024
1 parent 2ab584e commit 5d22eed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,9 @@ func (p *parser) parseDirective(ctx *context, g *TokenGroup) (*ast.DirectiveNode

func (p *parser) parseComment(ctx *context) (ast.Node, error) {
cm := p.parseHeadComment(ctx)
if ctx.isTokenNotFound() {
return cm, nil
}
node, err := p.parseToken(ctx, ctx.currentToken())
if err != nil {
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,14 @@ foo: | # comment
yaml: `
foo: > # comment
x: 42
`,
},
{
name: "unattached comment",
yaml: `
# This comment is in its own document
---
a: b
`,
},
}
Expand Down

0 comments on commit 5d22eed

Please sign in to comment.