diff --git a/FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 b/FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 new file mode 100644 index 000000000..064746ff3 --- /dev/null +++ b/FuzzTesting/FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 @@ -0,0 +1 @@ +{"wktTimestamp":"9999-12-31T23:59:60Z"} \ No newline at end of file diff --git a/Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift b/Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift index ab0a68b87..cad7355f2 100644 --- a/Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift +++ b/Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift @@ -167,9 +167,6 @@ private func parseTimestamp(s: String) throws -> (Int64, Int32) { adjusted += Int64(hourOffset) * Int64(3600) adjusted += Int64(minuteOffset) * Int64(60) } - if adjusted < minTimestampSeconds || adjusted > maxTimestampSeconds { - throw JSONDecodingError.malformedTimestamp - } seconds = adjusted pos += 6 } else if value[pos] == letterZ { // "Z" indicator for UTC @@ -181,6 +178,9 @@ private func parseTimestamp(s: String) throws -> (Int64, Int32) { if pos != value.count { throw JSONDecodingError.malformedTimestamp } + guard seconds >= minTimestampSeconds && seconds <= maxTimestampSeconds else { + throw JSONDecodingError.malformedTimestamp + } return (seconds, nanos) } diff --git a/Tests/SwiftProtobufTests/Test_FuzzTests.swift b/Tests/SwiftProtobufTests/Test_FuzzTests.swift index 23ffd0f1b..463c7a056 100644 --- a/Tests/SwiftProtobufTests/Test_FuzzTests.swift +++ b/Tests/SwiftProtobufTests/Test_FuzzTests.swift @@ -121,6 +121,9 @@ final class Test_FuzzTests: XCTestCase { // This actually fails when the fuzzer was trying to write it back out again. let msg = try! SwiftProtoTesting_Fuzz_Message(jsonString: " {\"wktAny\": {}} ") XCTAssertEqual(try! msg.jsonString(), "{\"wktAny\":{}}") + + // FailCases/clusterfuzz-testcase-minimized-FuzzJSON_debug-6286338012282880 + assertJSONFails("{\"wktTimestamp\":\"9999-12-31T23:59:60Z\"}") } func test_TextFormat() {