iOS 14: Infinite value in returned location data causes app crash
Bug Report
Problem
What is expected to happen?
The plugin returns native location data without causing the app to crash.
What does actually happen?
The plugin sometimes causes the app to crash when attempting to return native location data.
Information
Firebase has reported a spike in crashes the iOS versions of several of my apps. The Crashlytics data indicates the crashes are only occurring on devices running iOS 14.
The stack trace indicates the crash originates in the geolocation plugin because one of the values being extracted from the native location object has an infinite value and therefore an exception is raised when trying to extract the values and transform then into a JSON object.
This can be resolved by checking the NSMutableDictionary, into which the values from the CLLocation object are copied, is valid for conversion to JSON before attempting the conversion.
Example crash stacktrace:
Fatal Exception: NSInvalidArgumentException
Invalid number value (infinite) in JSON write
Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x18f35e754 __exceptionPreprocess
1 libobjc.A.dylib 0x1a3e257a8 objc_exception_throw
2 Foundation 0x190775ad8 _writeJSONNumber
3 Foundation 0x19077687c ___writeJSONObject_block_invoke
4 CoreFoundation 0x18f2c2a68 __NSDICTIONARY_IS_CALLING_OUT_TO_A_BLOCK__
5 CoreFoundation 0x18f2371f8 -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:]
6 Foundation 0x190775d7c _writeJSONObject
7 Foundation 0x190776ba0 ___writeJSONArray_block_invoke
8 CoreFoundation 0x18f2dc5bc __NSARRAY_IS_CALLING_OUT_TO_A_BLOCK__
9 CoreFoundation 0x18f236b8c -[__NSSingleObjectArrayI enumerateObjectsWithOptions:usingBlock:]
10 Foundation 0x19077625c _writeJSONArray
11 Foundation 0x190773db8 -[_NSJSONWriter dataWithRootObject:options:]
12 Foundation 0x19061d7a8 +[NSJSONSerialization dataWithJSONObject:options:error:]
13 Greater_Anglia 0x1025a71ac -[NSArray(CDVJSONSerializingPrivate) cdv_JSONString]
14 Greater_Anglia 0x10259deb8 -[CDVPluginResult argumentsAsJSON]
15 Greater_Anglia 0x10259a9f8 -[CDVCommandDelegateImpl sendPluginResult:callbackId:]
16 Greater_Anglia 0x1021bb48c -[CDVLocation returnLocationInfo:andKeepCallback:] + 300 (CDVLocation.m:300)
17 Greater_Anglia 0x1021ba4f8 -[CDVLocation locationManager:didUpdateToLocation:fromLocation:] + 177 (CDVLocation.m:177)
18 CoreLocation 0x19520f8fc CLClientStopVehicleHeadingUpdates
19 CoreLocation 0x19520ea80 CLClientStopVehicleHeadingUpdates
20 CoreLocation 0x1951f4d1c CLClientCreateWithBundleIdentifierAndPathOnSilo
21 CoreFoundation 0x18f2d8718 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__
22 CoreFoundation 0x18f2d796c __CFRunLoopDoBlocks
23 CoreFoundation 0x18f2d1f54 __CFRunLoopRun
24 CoreFoundation 0x18f2d1360 CFRunLoopRunSpecific
25 GraphicsServices 0x1a690f734 GSEventRunModal
26 UIKitCore 0x191d4c584 -[UIApplication _run]
27 UIKitCore 0x191d51df4 UIApplicationMain
28 Greater_Anglia 0x10216e904 main + 32 (main.m:32)
29 libdyld.dylib 0x18ef8dcf8 start
Command or Code
I'm not certain exactly which value retrieved from the CLLocation object is INFINITY because the stack trace from the crash only points to the line where the conversion to JSON is attempted but doesn't actually indicate which field in the NSMutableDictionary contains the INFINITE value.
However, the issue can be artificially reproduced by setting one of the values to INFINITY before the conversion is made, e.g. on line 293 of CDVLocation.m:
[returnInfo setObject:[NSNumber numberWithDouble:INFINITY] forKey@"timestamp"];
Environment, Platform, Device
Devices/Simulators running iOS 14.x
Version information
[email protected]
[email protected]
[email protected]
Checklist
- [x] I searched for existing GitHub issues
- [x] I updated all Cordova tooling to most recent version
- [x] I included all the necessary information above
@dpa99c You're awesome ! :heart_eyes:
I'm facing the issue, is it planed to merge Dave's merge request ?
Thanks !
Fixed by https://github.com/apache/cordova-plugin-geolocation/pull/231