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 parsing of literal header option #582

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
113 changes: 58 additions & 55 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,15 +1083,23 @@ func (s *Scanner) validateDocumentHeaderOption(opt string) error {
if len(opt) == 0 {
return nil
}
orgOpt := opt
opt = strings.TrimPrefix(opt, "-")
opt = strings.TrimPrefix(opt, "+")
opt = strings.TrimSuffix(opt, "-")
opt = strings.TrimSuffix(opt, "+")
if len(opt) == 0 {
return nil
}
if _, err := strconv.ParseInt(opt, 10, 64); err != nil {
return fmt.Errorf("invalid header option: %q", opt)
if opt == "0" {
return fmt.Errorf("invalid header option: %q", orgOpt)
}
i, err := strconv.ParseInt(opt, 10, 64)
if err != nil {
return fmt.Errorf("invalid header option: %q", orgOpt)
}
if i > 9 {
return fmt.Errorf("invalid header option: %q", orgOpt)
}
return nil
}
Expand All @@ -1100,64 +1108,59 @@ func (s *Scanner) scanDocumentHeaderOption(ctx *Context) error {
header := ctx.currentChar()
ctx.addOriginBuf(header)
s.progress(ctx, 1) // skip '|' or '>' character

var progress int
for idx, c := range ctx.src[ctx.idx:] {
progress := idx
progress = idx
ctx.addOriginBuf(c)
switch c {
case '\n', '\r':
value := strings.TrimRight(ctx.source(ctx.idx, ctx.idx+idx), " ")
commentValueIndex := strings.Index(value, "#")
opt := value
if commentValueIndex > 0 {
opt = value[:commentValueIndex]
}
opt = strings.TrimRightFunc(opt, func(r rune) bool {
return r == ' ' || r == '\t'
})
if len(opt) != 0 {
if err := s.validateDocumentHeaderOption(opt); err != nil {
invalidTk := token.Invalid(err.Error(), string(ctx.obuf), s.pos())
s.progressColumn(ctx, progress)
return ErrInvalidToken(invalidTk)
}
}
if s.column == 1 {
s.lastDelimColumn = 1
}

commentIndex := strings.Index(string(ctx.obuf), "#")
headerBuf := string(ctx.obuf)
if commentIndex > 0 {
headerBuf = headerBuf[:commentIndex]
}
switch header {
case '|':
ctx.addToken(token.Literal("|"+opt, headerBuf, s.pos()))
ctx.isLiteral = true
case '>':
ctx.addToken(token.Folded(">"+opt, headerBuf, s.pos()))
ctx.isFolded = true
}
if commentIndex > 0 {
comment := string(value[commentValueIndex+1:])
s.offset += len(headerBuf)
s.column += len(headerBuf)
ctx.addToken(token.Comment(comment, string(ctx.obuf[len(headerBuf):]), s.pos()))
}
s.indentState = IndentStateKeep
ctx.resetBuffer()
ctx.docOpt = opt
if s.isNewLineChar(c) {
break
}
}
value := strings.TrimRight(ctx.source(ctx.idx, ctx.idx+progress), " ")
commentValueIndex := strings.Index(value, "#")
opt := value
if commentValueIndex > 0 {
opt = value[:commentValueIndex]
}
opt = strings.TrimRightFunc(opt, func(r rune) bool {
return r == ' ' || r == '\t'
})
if len(opt) != 0 {
if err := s.validateDocumentHeaderOption(opt); err != nil {
invalidTk := token.Invalid(err.Error(), string(ctx.obuf), s.pos())
s.progressColumn(ctx, progress)
return nil
return ErrInvalidToken(invalidTk)
}
}
text := string(ctx.src[ctx.idx:])
invalidTk := token.Invalid(
fmt.Sprintf("invalid document header: %q", text),
string(ctx.obuf), s.pos(),
)
s.progressColumn(ctx, len(text))
return ErrInvalidToken(invalidTk)
if s.column == 1 {
s.lastDelimColumn = 1
}

commentIndex := strings.Index(string(ctx.obuf), "#")
headerBuf := string(ctx.obuf)
if commentIndex > 0 {
headerBuf = headerBuf[:commentIndex]
}
switch header {
case '|':
ctx.addToken(token.Literal("|"+opt, headerBuf, s.pos()))
ctx.isLiteral = true
case '>':
ctx.addToken(token.Folded(">"+opt, headerBuf, s.pos()))
ctx.isFolded = true
}
if commentIndex > 0 {
comment := string(value[commentValueIndex+1:])
s.offset += len(headerBuf)
s.column += len(headerBuf)
ctx.addToken(token.Comment(comment, string(ctx.obuf[len(headerBuf):]), s.pos()))
}
s.indentState = IndentStateKeep
ctx.resetBuffer()
ctx.docOpt = opt
s.progressColumn(ctx, progress)
return nil
}

func (s *Scanner) scanMapKey(ctx *Context) bool {
Expand Down
4 changes: 0 additions & 4 deletions yaml_test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ var failureTestNames = []string{
"invalid-comma-in-tag",
"invalid-tag", // pass yamlv3.
"legal-tab-after-indentation", // pass yamlv3.
"literal-modifers/00", // pass yamlv3.
"literal-modifers/01", // pass yamlv3.
"literal-modifers/02", // pass yamlv3.
"literal-modifers/03", // pass yamlv3.
"literal-scalars", // pass yamlv3.
"mapping-key-and-flow-sequence-item-anchors", // no json.
"multiline-plain-value-with-tabs-on-empty-lines", // pass yamlv3.
Expand Down
Loading