Skip to content

Commit

Permalink
Fix crash on decoding partial YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
kkebo committed May 5, 2023
1 parent 30e241c commit fdadc9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Sources/UniYAML/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,10 @@ public struct UniYAML {
guard i >= block else {
throw UniYAMLError.error(detail: "unexpected indentation")
}
index = stream.index(index, offsetBy: i)
index = stream.index(index, offsetBy: i, limitedBy: stream.endIndex) ?? stream.endIndex
guard index < stream.endIndex else {
break
}
var location = Range(uncheckedBounds: (index, stream.endIndex))
if let border = stream.rangeOfCharacter(from: CharacterSet(charactersIn: "\r\n\u{85}"), range: location) {
location = Range(uncheckedBounds: (index, border.lowerBound))
Expand Down
15 changes: 15 additions & 0 deletions Tests/UniYAMLTests/UniYAMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class UniYAMLTests: XCTestCase {
("testYAMLUnexpectedBrace", testYAMLUnexpectedBrace),
("testYAMLUnexpectedEnd", testYAMLUnexpectedEnd),
("testPartialYAML", testPartialYAML),
("testPartialYAML2", testPartialYAML2),
]

let types = "---\n\nsimple string: a text\nquoted string: 'john ''beatle''\n lennon'\nsplit string: >\n two\n words\nrows: |\n first\n second\n last\nint: -12345\nuint: 67890\ndouble: 3.14159265\npositive: yes\nnegative: off\n"
Expand Down Expand Up @@ -467,4 +468,18 @@ classic:
)
}

func testPartialYAML2() {
let yaml = "a: 3\n "
var obj: YAML?
var err: String = ""
do {
obj = try UniYAML.decode(yaml)
} catch UniYAMLError.error(let detail) {
err = detail
} catch {
print(error)
}
XCTAssert(obj == nil && err.hasPrefix("missing value at line 2"))
}

}

0 comments on commit fdadc9e

Please sign in to comment.