Skip to content

Commit

Permalink
Fix a crash from attempted to read past the end of input when given i…
Browse files Browse the repository at this point in the history
…ncomplete input data.

Closes bignerdranch#199.
  • Loading branch information
hydhknn authored and cbrauchli committed Sep 13, 2016
1 parent a3192bf commit 6703a48
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Sources/JSONParser.swift
Expand Up @@ -158,8 +158,12 @@ public struct JSONParser {
return try decodeNumberAsString(start)
}
}

throw Error.ValueInvalid(offset: loc, character: UnicodeScalar(input[loc]))

if loc < input.count {
throw Error.ValueInvalid(offset: loc, character: UnicodeScalar(input[loc]))
} else {
throw Error.EndOfStreamUnexpected
}
}

private mutating func skipWhitespace() {
Expand Down
13 changes: 13 additions & 0 deletions Tests/JSONParserTests.swift
Expand Up @@ -153,6 +153,19 @@ class JSONParserTests: XCTestCase {
XCTAssertEqual(value, JSON.String(expect))
}

func testThatParserFailsWhenIncompleteDataIsPresent() {
for s in [" ", "[0,", "{\"\":"] {
do {
let value = try JSONFromString(" ")
XCTFail("Unexpectedly parsed \(s) as \(value)")
} catch JSONParser.Error.EndOfStreamUnexpected {
// expected error - do nothing
} catch {
XCTFail("Unexpected error \(error)")
}
}
}

func testThatParserUnderstandsNumbers() {
for (string, shouldBeInt) in [
(" 0 ", 0),
Expand Down

0 comments on commit 6703a48

Please sign in to comment.