diff --git a/Sources/UniYAML/Decoder.swift b/Sources/UniYAML/Decoder.swift index 44041f6..9425f8c 100644 --- a/Sources/UniYAML/Decoder.swift +++ b/Sources/UniYAML/Decoder.swift @@ -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)) diff --git a/Tests/UniYAMLTests/UniYAMLTests.swift b/Tests/UniYAMLTests/UniYAMLTests.swift index 55229fa..c2147fb 100644 --- a/Tests/UniYAMLTests/UniYAMLTests.swift +++ b/Tests/UniYAMLTests/UniYAMLTests.swift @@ -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" @@ -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")) + } + }