diff --git a/Sources/Foundation/JSONDecoder.swift b/Sources/Foundation/JSONDecoder.swift index 503f27a337..6913494cb8 100644 --- a/Sources/Foundation/JSONDecoder.swift +++ b/Sources/Foundation/JSONDecoder.swift @@ -404,7 +404,7 @@ extension JSONDecoderImpl: Decoder { as type: T.Type) throws -> T { if case .number(let number) = value { - guard let floatingPoint = T(number) else { + guard let floatingPoint = T(number), floatingPoint.isFinite else { var path = self.codingPath if let additionalKey = additionalKey { path.append(additionalKey) diff --git a/Tests/Foundation/Tests/TestJSONEncoder.swift b/Tests/Foundation/Tests/TestJSONEncoder.swift index 03a389f33d..c7e61a9b6b 100644 --- a/Tests/Foundation/Tests/TestJSONEncoder.swift +++ b/Tests/Foundation/Tests/TestJSONEncoder.swift @@ -601,14 +601,23 @@ class TestJSONEncoder : XCTestCase { func test_codingOfFloat() { test_codingOf(value: Float(1.5), toAndFrom: "1.5") + + // Check value too large fails to decode. + XCTAssertThrowsError(try JSONDecoder().decode(Float.self, from: "1e100".data(using: .utf8)!)) } func test_codingOfDouble() { test_codingOf(value: Double(1.5), toAndFrom: "1.5") + + // Check value too large fails to decode. + XCTAssertThrowsError(try JSONDecoder().decode(Double.self, from: "100e323".data(using: .utf8)!)) } func test_codingOfDecimal() { test_codingOf(value: Decimal.pi, toAndFrom: "3.14159265358979323846264338327950288419") + + // Check value too large fails to decode. + XCTAssertThrowsError(try JSONDecoder().decode(Decimal.self, from: "100e200".data(using: .utf8)!)) } func test_codingOfString() {