Skip to content

Commit

Permalink
Fix panic when parsing unattached comment
Browse files Browse the repository at this point in the history
The parser panics when there's a comment without anything to attach it
to. This PR fixes that case.

Signed-off-by: Charith Ellawala <[email protected]>
  • Loading branch information
charithe committed Dec 2, 2024
1 parent 2ab584e commit 48f0004
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func (p *parser) parseDocumentBody(ctx *context) (ast.Node, error) {
}

func (p *parser) parseToken(ctx *context, tk *Token) (ast.Node, error) {
if tk == nil {
return nil, nil
}

switch tk.GroupType() {
case TokenGroupMapKey, TokenGroupMapKeyValue:
return p.parseMap(ctx)
Expand Down Expand Up @@ -1063,7 +1067,12 @@ func (p *parser) parseDirective(ctx *context, g *TokenGroup) (*ast.DirectiveNode

func (p *parser) parseComment(ctx *context) (ast.Node, error) {
cm := p.parseHeadComment(ctx)
node, err := p.parseToken(ctx, ctx.currentToken())
nextTok := ctx.currentToken()
if nextTok == nil {
return cm, nil
}

node, err := p.parseToken(ctx, nextTok)
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 48f0004

Please sign in to comment.