diff --git a/Sources/UniYAML/Decoder.swift b/Sources/UniYAML/Decoder.swift index 46115a1..7880b9c 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 = "" @@ -402,12 +405,18 @@ 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)) glue = (folded) ? " ":stream[border.lowerBound] } + guard location.upperBound < stream.endIndex else { + break + } index = stream.index(after: location.upperBound) line += 1 if !value.isEmpty { @@ -427,8 +436,14 @@ public struct UniYAML { } let s = ss.replacingOccurrences(of: "\r", with: "").replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\u{85}", with: "") if s.hasPrefix("'"), s.hasSuffix("'") { + guard s.count >= 2 else { + return nil + } return String(s[s.index(after: s.startIndex)..= 2 else { + return nil + } return String(s[s.index(after: s.startIndex)..