From 7575997519bb9e270dcde4600049e7248c43fc0a Mon Sep 17 00:00:00 2001 From: Kevin Renskers Date: Tue, 13 Sep 2022 11:55:41 +0200 Subject: [PATCH] Provide fallbacks for assertions in production (#2141) Closes #2109 #skip-changelog --- Sources/Sentry/SentryClient.m | 4 ++++ Sources/Sentry/SentrySerialization.m | 8 ++++++++ Sources/Sentry/SentrySwizzle.m | 13 +++++++++++-- .../Recording/Tools/SentryCrashJSONCodecObjC.m | 4 ++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Sources/Sentry/SentryClient.m b/Sources/Sentry/SentryClient.m index d0314352b0d..f321256c813 100644 --- a/Sources/Sentry/SentryClient.m +++ b/Sources/Sentry/SentryClient.m @@ -468,6 +468,10 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event isCrashEvent:(BOOL)isCrashEvent { NSParameterAssert(event); + if (event == nil) { + return nil; + } + if ([self isDisabled]) { [self logDisabledMessage]; return nil; diff --git a/Sources/Sentry/SentrySerialization.m b/Sources/Sentry/SentrySerialization.m index dccf22ee095..6dc7dcb7fd1 100644 --- a/Sources/Sentry/SentrySerialization.m +++ b/Sources/Sentry/SentrySerialization.m @@ -163,6 +163,7 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data SentryEnvelopeHeader *envelopeHeader = nil; const unsigned char *bytes = [data bytes]; int envelopeHeaderIndex = 0; + for (int i = 0; i < data.length; ++i) { if (bytes[i] == '\n') { envelopeHeaderIndex = i; @@ -208,12 +209,19 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data break; } } + if (nil == envelopeHeader) { [SentryLog logWithMessage:[NSString stringWithFormat:@"Invalid envelope. No header found."] andLevel:kSentryLevelError]; return nil; } + NSAssert(envelopeHeaderIndex > 0, @"EnvelopeHeader was parsed, its index is expected."); + if (envelopeHeaderIndex == 0) { + NSLog(@"EnvelopeHeader was parsed, its index is expected."); + return nil; + } + // Parse items NSInteger itemHeaderStart = envelopeHeaderIndex + 1; diff --git a/Sources/Sentry/SentrySwizzle.m b/Sources/Sentry/SentrySwizzle.m index a19c1a90d79..2b9e46928bf 100644 --- a/Sources/Sentry/SentrySwizzle.m +++ b/Sources/Sentry/SentrySwizzle.m @@ -19,7 +19,11 @@ @implementation SentrySwizzleInfo - (SentrySwizzleOriginalIMP)getOriginalImplementation { - NSAssert(_impProviderBlock, nil); + NSAssert(_impProviderBlock, @"_impProviderBlock can't be missing"); + if (!_impProviderBlock) { + NSLog(@"_impProviderBlock can't be missing"); + return NULL; + } #if TEST @synchronized(self) { @@ -136,9 +140,14 @@ + (BOOL)swizzleInstanceMethod:(SEL)selector mode:(SentrySwizzleMode)mode key:(const void *)key { - NSAssert(!(NULL == key && SentrySwizzleModeAlways != mode), + NSAssert(!(key == NULL && mode != SentrySwizzleModeAlways), @"Key may not be NULL if mode is not SentrySwizzleModeAlways."); + if (key == NULL && mode != SentrySwizzleModeAlways) { + NSLog(@"Key may not be NULL if mode is not SentrySwizzleModeAlways."); + return NO; + } + @synchronized(swizzledClassesDictionary()) { if (key) { NSSet *swizzledClasses = swizzledClassesForKey(key); diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodecObjC.m b/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodecObjC.m index fca9a7a66b1..6f24e3d8886 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodecObjC.m +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodecObjC.m @@ -126,6 +126,10 @@ - (id)initWithEncodeOptions:(SentryCrashJSONEncodeOption)encodeOptions self.callbacks = malloc(sizeof(*self.callbacks)); // Unlikely malloc failure. NSAssert(self.callbacks != NULL, @"Could not allocate callbacks"); + if (self.callbacks == NULL) { + NSLog(@"Could not allocate callbacks"); + return NULL; + } self.callbacks->onBeginArray = onBeginArray; self.callbacks->onBeginObject = onBeginObject;