Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on decoding partial YAML (#1) #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Sources/UniYAML/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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 {
Expand All @@ -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)..<s.index(before: s.endIndex)])
} else if s.hasPrefix("\""), s.hasSuffix("\"") {
guard s.count >= 2 else {
return nil
}
return String(s[s.index(after: s.startIndex)..<s.index(before: s.endIndex)]
.replacingOccurrences(of: "\\\\", with: "_backslash_holder_") // XXX: bad ugly hack
.replacingOccurrences(of: "\\0", with: "\0")
Expand Down
52 changes: 52 additions & 0 deletions Tests/UniYAMLTests/UniYAMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class UniYAMLTests: XCTestCase {
("testYAMLUnexpectedColon", testYAMLUnexpectedColon),
("testYAMLUnexpectedBrace", testYAMLUnexpectedBrace),
("testYAMLUnexpectedEnd", testYAMLUnexpectedEnd),
("testPartialYAML", testPartialYAML),
("testPartialYAML2", testPartialYAML2),
("testPartialYAML3", testPartialYAML3),
("testPartialYAML4", testPartialYAML4),
]

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 @@ -456,4 +460,52 @@ classic:
XCTAssert(obj == nil && err.hasPrefix("unexpected stream end"))
}

func testPartialYAML() throws {
let yaml = "a: "
let obj = try UniYAML.decode(yaml)
XCTAssert(
obj.type == .dictionary &&
obj.keys!.count == 1 &&
obj["a"]?.type == .pending
)
}

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"))
}

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"))
}

func testPartialYAML4() throws {
let yaml = "a: '"
let obj = try UniYAML.decode(yaml)
XCTAssert(
obj.type == .dictionary &&
obj.keys!.count == 1 &&
obj["a"]?.type == .pending
)
}

}