-
Notifications
You must be signed in to change notification settings - Fork 89
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
NaN crash Encodable.toString(pretty:) #264
Comments
I can reproduce this in a test here:
Lemme think on the best way to handle it... or if you have any thoughts lemme know. |
Thanks for the quick response. When I try your function, the catch blocks do execute-- i.e., Have you been able to repro a crash in the JSONEncoder? (I just noticed I mis-wrote above and said |
I do now note that:
Doesn't throw an error in |
I'm pretty sure we're hitting NSNumber in objc here on the original stack trace, in which case, you're getting an ObjC exception, not a swift one. |
What is "CodableValue" in your example above? |
:) |
I now have a repro. Running as an Xcode debug build on iPad 10th generation, iPadOS 16.2 simulator. If I add NaN values to the properties in a
Or if I do this via Swift, e.g.,
I get the NaN crash as indicated above. This also happens if I use an iPad Mini, actual hardware running iPadOS 16.6.1. So far all of our reported crashes are on iPadOS 16 or lower. I've not been able to repro on iPadOS 17 simulator. |
Here is a smaller repro, and a fix as well. This only crashes on iOS 16 or lower (i..e, not iOS 17). |
I can't use that fix unfortunately. It introduces side effects that would be unknown to others without reading the code. Since NaN, Infinity, -Infinity aren't allowed in JSON, it makes the most sense for us to throw on data that contains it. That puts the onus on the developer to do whatever interpretations (convert to 0 in your case), maybe type it as a string with "NaN" etc. on their own. What do you think? |
This seems like a migration difference, from the prior Objective-C library. Not sure what was being logged there with NaN values, but I suspect we've had those instances logging to Segment in our code for a while now. Not saying that's the best on our side, but just a fact. It's a large lift for us to go through all our Segment analytics logging and change it. I wonder if you could add a setting something like what JSONEncoder has. And put this setting into your Configuration. The default of configuration value could be to throw, but you could allow for clients to also decide on some other less exceptional behavior, such as a 0 value. |
It's not really. They're likely just getting stripped out silently due to the checks it does during JSON generation. I wrote most of that code for the JSON generation in analytics-ios, and i wasn't thinking about NaN/Infinity so it wasn't on purpose. It's another instance of technically correct vs. convenience. I'll keep noodling on it for the time being since you have a work around in place. |
PR for this pending legal review. Stay tuned. |
Describe the bug
We are getting production app crash reports here with the
toString
. Crashlytics is reporting it as:With more detail:
To Reproduce
So far I don't have a clear repro case. But, we have 34 crash events affecting 30 users.
It looks like some our custom data being added to dictionary payloads has
NaN
numeric (NSNumber) values. And that is making it through the JSON tests (e.g., here), but later somehow causing theJSONDecoder
to crash.I can't actually show that above idea with an actual crash in our development build though, so that's just my best guess so far.
Expected behavior
I wouldn't expect the
JSONDecoder
usage in JSON.swift to cause an app crash.Screenshots
See above.
Platform (please complete the following information):
Additional context
I'd like to resolve this ASAP. I'm hoping to get a patch into our production app. Thanks.
The text was updated successfully, but these errors were encountered: