Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when parsing unattached comment #565

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
charithe marked this conversation as resolved.
Show resolved Hide resolved
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()
charithe marked this conversation as resolved.
Show resolved Hide resolved
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