You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When decoding an explicit signed 64 bit value (Int64), if that value is smaller than Int64.min, I am seeing the value being clamped to Int64.min.
I am running this in Xcode 15.3 / M1Pro / Sonoma, but the same behavior happens in 15.2. Not sure when this was introduced, but we have some unit tests now failing due to this.
For example, if my JSON contains "value" = -9223372036854775809, the decoder will assign my struct property the value -9223372036854775808
I believe this might be unexpected because decoding an Int32 with a value less than Int32.min throws a JSON Decoding error: "Number -2147483649 is not representable in Swift."
Performing a similar decode with JSONSerialization doesn't suffer from this, although it exhibits the classic integer overflow (circling back to the positive value)
This behavior isn't present when testing against Int64.max, as that will throw an error as expected.
Expected to throw a JSON Decoding error: "Number -9223372036854775809 is not representable in Swift."
Environment
Swift version 5.10 (swift-5.10-RELEASE)
Additional information
Based on the discussion in the forums and with my own local experimentation, it seems to be related to func _slowpath_unwrapFixedWidthInteger<T: FixedWidthInteger>(...) where the value is instantiated with: T(exactly: double), where using Int64(exactly:) has a range of values where it's returning Int64.min.
The boundary seems to be between -9223372036854776832 and -9223372036854776833, as noted.
The text was updated successfully, but these errors were encountered:
Description
(Forum thread discussing this issue: https://forums.swift.org/t/unexpected-decoding-behavior-of-int64/71762)
When decoding an explicit signed 64 bit value (Int64), if that value is smaller than
Int64.min
, I am seeing the value being clamped toInt64.min
.I am running this in Xcode 15.3 / M1Pro / Sonoma, but the same behavior happens in 15.2. Not sure when this was introduced, but we have some unit tests now failing due to this.
For example, if my JSON contains
"value" = -9223372036854775809
, the decoder will assign my struct property the value-9223372036854775808
I believe this might be unexpected because decoding an Int32 with a value less than Int32.min throws a JSON Decoding error:
"Number -2147483649 is not representable in Swift."
Performing a similar decode with JSONSerialization doesn't suffer from this, although it exhibits the classic integer overflow (circling back to the positive value)
This behavior isn't present when testing against
Int64.max
, as that will throw an error as expected.Reproduction
Expected behavior
Expected to throw a JSON Decoding error:
"Number -9223372036854775809 is not representable in Swift."
Environment
Swift version 5.10 (swift-5.10-RELEASE)
Additional information
Based on the discussion in the forums and with my own local experimentation, it seems to be related to
func _slowpath_unwrapFixedWidthInteger<T: FixedWidthInteger>(...)
where the value is instantiated with:T(exactly: double)
, where usingInt64(exactly:)
has a range of values where it's returningInt64.min
.The boundary seems to be between
-9223372036854776832
and-9223372036854776833
, as noted.The text was updated successfully, but these errors were encountered: