Skip to content

[SR-6440] JSONDecoder behaves differently on macOS vs. Linux #3782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gwynne opened this issue Nov 19, 2017 · 2 comments
Closed

[SR-6440] JSONDecoder behaves differently on macOS vs. Linux #3782

gwynne opened this issue Nov 19, 2017 · 2 comments

Comments

@gwynne
Copy link
Contributor

gwynne commented Nov 19, 2017

Previous ID SR-6440
Radar None
Original Reporter @gwynne
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: ba2ce7916df7171b8a4d2917feb20190

Issue Description:

When decoding JSON, requesting an integer value which is too large for the requested integer type (e.g. 10000000000000000000000000000000000000) will throw the following exception on macOS:

dataCorrupted(Swift.DecodingError.Context(codingPath: [VaporTests.CodableTests.(TestModel in _3EDCAB1B5E5ADB4E80B35729410FF649).CodingKeys.bomb], debugDescription: "Parsed JSON number <10000000000000000000000000000000000000> does not fit in Int64.", underlyingError: nil))

However, on Linux, the value Int64.max is silently returned instead.

As far as I can determine, this corresponds to the documented behavior of strtol() on overflow, per https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/JSONSerialization.swift#L826.

The exception thrown on macOS appears at https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/JSONEncoder.swift#L1832, but the logic doesn't fire on Linux.

The test case which showed this behavior is seen in this commit (which removes the test due to the behavior difference): vapor/vapor@7ebe247

Credit for finding this issue goes to https://github.com/vzsg

@spevans
Copy link
Contributor

spevans commented Aug 6, 2018

Possible fix: #1655

@spevans
Copy link
Contributor

spevans commented Nov 14, 2018

This is now fixed in swift-DEVELOPMENT-SNAPSHOT-2018-11-13

$ cat sr_6440.swift 
import Foundation

let rawJSON = """
{
"foo": "abc",
"baz": 123,
"bomb": 10000000000000000000000000000000000000
}
""".data(using: .utf8)!

struct Foo: Decodable {
    let foo: String
    let baz: Int
    let bomb: Int64
}

let d = try JSONDecoder().decode(Foo.self, from: rawJSON)
print(d)

$ ~/swift-DEVELOPMENT-SNAPSHOT-2018-11-13-a-ubuntu18.04/usr/bin/swift sr_6440.swift 
Fatal error: Error raised at top level: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "bomb", intValue: nil)], debugDescription: "Parsed JSON number <10000000000000000000000000000000000000> does not fit in Int64.", underlyingError: nil)): file /home/buildnode/jenkins/workspace/oss-swift-package-linux-ubuntu-18_04/swift/stdlib/public/core/ErrorType.swift, line 200

Changing the Int64 to Decimal now allows proper decoding

$ ~/swift-DEVELOPMENT-SNAPSHOT-2018-11-13-a-ubuntu18.04/usr/bin/swift sr_6440.swift 
Foo(foo: "abc", baz: 123, bomb: 10000000000000000000000000000000000000)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from swiftlang/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants