Skip to content

Commit

Permalink
Merge pull request swiftlang#2989 from spevans/pr_json_float_infinity
Browse files Browse the repository at this point in the history
JSONDecoder: Check that a parsed float does not return infinity.
  • Loading branch information
spevans authored Mar 9, 2021
2 parents 06e86e7 + 77218dd commit 2232811
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Foundation/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions Tests/Foundation/Tests/TestJSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 2232811

Please sign in to comment.