diff --git a/Sources/UniYAML/Decoder.swift b/Sources/UniYAML/Decoder.swift index 46115a1..44041f6 100644 --- a/Sources/UniYAML/Decoder.swift +++ b/Sources/UniYAML/Decoder.swift @@ -297,6 +297,9 @@ public struct UniYAML { return nil } _ = parseIndent(stream, index: &index) + guard index < stream.endIndex else { + return nil + } var search = Range(uncheckedBounds: (index, stream.endIndex)) var location = search var fragments = "" diff --git a/Tests/UniYAMLTests/UniYAMLTests.swift b/Tests/UniYAMLTests/UniYAMLTests.swift index d15c113..80fa900 100644 --- a/Tests/UniYAMLTests/UniYAMLTests.swift +++ b/Tests/UniYAMLTests/UniYAMLTests.swift @@ -32,6 +32,7 @@ class UniYAMLTests: XCTestCase { ("testYAMLUnexpectedColon", testYAMLUnexpectedColon), ("testYAMLUnexpectedBrace", testYAMLUnexpectedBrace), ("testYAMLUnexpectedEnd", testYAMLUnexpectedEnd), + ("testPartialYAML", testPartialYAML), ] 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" @@ -456,4 +457,10 @@ classic: XCTAssert(obj == nil && err.hasPrefix("unexpected stream end")) } + func testPartialYAML() throws { + let yaml = "a: " + let obj = try UniYAML.decode(yaml) + XCTAssertEqual(obj, nil) + } + }