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 6, 2023
1 parent fdadc9e commit 4f605d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Sources/UniYAML/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ public struct UniYAML {
location = Range(uncheckedBounds: (index, border.lowerBound))
glue = (folded) ? " ":stream[border.lowerBound]
}
guard location.upperBound < stream.endIndex else {
break
}
index = stream.index(after: location.upperBound)
line += 1
if !value.isEmpty {
Expand Down
15 changes: 15 additions & 0 deletions Tests/UniYAMLTests/UniYAMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class UniYAMLTests: XCTestCase {
("testYAMLUnexpectedEnd", testYAMLUnexpectedEnd),
("testPartialYAML", testPartialYAML),
("testPartialYAML2", testPartialYAML2),
("testPartialYAML3", testPartialYAML3),
]

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 @@ -482,4 +483,18 @@ classic:
XCTAssert(obj == nil && err.hasPrefix("missing value at line 2"))
}

func testPartialYAML3() {
let yaml = "a: 3\n b"
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 4f605d4

Please sign in to comment.