Skip to content

Commit 2232811

Browse files
authored
Merge pull request swiftlang#2989 from spevans/pr_json_float_infinity
JSONDecoder: Check that a parsed float does not return infinity.
2 parents 06e86e7 + 77218dd commit 2232811

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Sources/Foundation/JSONDecoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ extension JSONDecoderImpl: Decoder {
404404
as type: T.Type) throws -> T
405405
{
406406
if case .number(let number) = value {
407-
guard let floatingPoint = T(number) else {
407+
guard let floatingPoint = T(number), floatingPoint.isFinite else {
408408
var path = self.codingPath
409409
if let additionalKey = additionalKey {
410410
path.append(additionalKey)

Tests/Foundation/Tests/TestJSONEncoder.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,23 @@ class TestJSONEncoder : XCTestCase {
601601

602602
func test_codingOfFloat() {
603603
test_codingOf(value: Float(1.5), toAndFrom: "1.5")
604+
605+
// Check value too large fails to decode.
606+
XCTAssertThrowsError(try JSONDecoder().decode(Float.self, from: "1e100".data(using: .utf8)!))
604607
}
605608

606609
func test_codingOfDouble() {
607610
test_codingOf(value: Double(1.5), toAndFrom: "1.5")
611+
612+
// Check value too large fails to decode.
613+
XCTAssertThrowsError(try JSONDecoder().decode(Double.self, from: "100e323".data(using: .utf8)!))
608614
}
609615

610616
func test_codingOfDecimal() {
611617
test_codingOf(value: Decimal.pi, toAndFrom: "3.14159265358979323846264338327950288419")
618+
619+
// Check value too large fails to decode.
620+
XCTAssertThrowsError(try JSONDecoder().decode(Decimal.self, from: "100e200".data(using: .utf8)!))
612621
}
613622

614623
func test_codingOfString() {

0 commit comments

Comments
 (0)