Fix SDK crash in encodeObject
Description
The SDK sometimes crashes with. See also internal SDK crashes for reference. This is our most frequent SDK crash in our latest stable release 8.36.0.
Exception Type: EXC_CRASH (SIGABRT)
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x1e5e2c558 __pthread_kill
1 libsystem_pthread.dylib 0x206ab2114 pthread_kill
2 libsystem_c.dylib 0x1ae636174 abort
3 libsystem_malloc.dylib 0x1b55740e0 malloc_vreport
4 libsystem_malloc.dylib 0x1b5574388 malloc_zone_error
5 libsystem_malloc.dylib 0x1b5573cec _tiny_check_and_zero_inline_meta_from_freelist
6 libsystem_malloc.dylib 0x1b55592a8 tiny_malloc_from_free_list
7 libsystem_malloc.dylib 0x1b5559894 tiny_malloc_should_clear
8 libsystem_malloc.dylib 0x1b555ca58 szone_malloc_should_clear
9 libsystem_malloc.dylib 0x1b556c6f8 nanov2_allocate_outlined
10 Foundation 0x1a141367c -[NSConcreteMutableData initWithLength:]
11 Foundation 0x1a14134e4 -[NSString(NSStringOtherEncodings) dataUsingEncoding:allowLossyConversion:]
12 Sentry.framework 0x101d37738 encodeObject (Sentry.framework:326)
13 Sentry.framework 0x101d37c54 encodeObject (Sentry.framework:384)
14 Sentry.framework 0x101d37608 +[SentryCrashJSONCodec encode:options:error:] (Sentry.framework:427)
15 Sentry.framework 0x101d497dc -[SentryCrashScopeObserver toJSONEncodedCString:] (Sentry.framework:149)
16 Sentry.framework 0x101d49718 -[SentryCrashScopeObserver syncScope:serialize:syncToSentryCrash:] (Sentry.framework:136)
17 Sentry.framework 0x101d49670 -[SentryCrashScopeObserver syncScope:syncToSentryCrash:] (Sentry.framework:110)
18 Sentry.framework 0x101d8686c -[SentryScope setExtraValue:forKey:] (Sentry.framework:258)
19 Sentry.framework 0x101d63340 -[SentryHub configureScope:] (Sentry.framework:555)
20 Sentry.framework 0x101d8af90 +[SentrySDK configureScope:] (Sentry.framework:406)
The culprit is here https://github.com/getsentry/sentry-cocoa/blob/eae2b596111cf7fd8bfc17f1edaae76a48f72aca/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodecObjC.m#L318-L332
Looking at the stacktrace, it seems like we can't allocate enough memory to encode the NSString to NSData. As this code is called from the scope observer, this could happen if some of our users try to put a very large string to the scope. We could fix this by truncating long strings in the scope.
I also noticed that we could use dataUsingEncoding with setting allowLossyConversion:YES so the conversation looses some data such as ‘Á’ becomes ‘A’ instead of throwing the whole string away, but that isn't related to the crash.