diff --git a/Sources/JSONParser.swift b/Sources/JSONParser.swift index 31ddaa9b..6047ca0b 100644 --- a/Sources/JSONParser.swift +++ b/Sources/JSONParser.swift @@ -237,6 +237,7 @@ public struct JSONParser { switch input[loc] { case Literal.BACKSLASH: loc = (loc + 1) + guard loc < input.count else { continue } switch input[loc] { case Literal.DOUBLE_QUOTE: stringDecodingBuffer.append(Literal.DOUBLE_QUOTE) case Literal.BACKSLASH: stringDecodingBuffer.append(Literal.BACKSLASH) diff --git a/Tests/FreddyTests/JSONParserTests.swift b/Tests/FreddyTests/JSONParserTests.swift index c459acbe..5223bebb 100644 --- a/Tests/FreddyTests/JSONParserTests.swift +++ b/Tests/FreddyTests/JSONParserTests.swift @@ -482,4 +482,15 @@ class JSONParserTests: XCTestCase { } } } + + func testThatParserRejectsStringEndingInBackslash() { + let invalidJSONString = "[\"\\" + do { + _ = try JSONParser.parse(invalidJSONString) + } catch JSONParser.Error.endOfStreamUnexpected { + // do nothing - this is the expected error + } catch { + XCTFail("Unexpected error: \(error)") + } + } }