From 4f605d4ffd49e2fb8f40ddc1fe5b47ed414e71b8 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkk669@users.noreply.github.com> Date: Fri, 5 May 2023 15:39:11 +0900 Subject: [PATCH] Fix crash on decoding partial YAML --- Sources/UniYAML/Decoder.swift | 3 +++ Tests/UniYAMLTests/UniYAMLTests.swift | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/Sources/UniYAML/Decoder.swift b/Sources/UniYAML/Decoder.swift index 9425f8c..6403640 100644 --- a/Sources/UniYAML/Decoder.swift +++ b/Sources/UniYAML/Decoder.swift @@ -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 { diff --git a/Tests/UniYAMLTests/UniYAMLTests.swift b/Tests/UniYAMLTests/UniYAMLTests.swift index c2147fb..92e54d7 100644 --- a/Tests/UniYAMLTests/UniYAMLTests.swift +++ b/Tests/UniYAMLTests/UniYAMLTests.swift @@ -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" @@ -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")) + } + }