Skip to content

Commit

Permalink
Provide fallbacks for assertions in production (#2141)
Browse files Browse the repository at this point in the history
Closes #2109
#skip-changelog
  • Loading branch information
kevinrenskers authored Sep 13, 2022
1 parent 48b387e commit 7575997
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions Sources/Sentry/SentrySerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
13 changes: 11 additions & 2 deletions Sources/Sentry/SentrySwizzle.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Class> *swizzledClasses = swizzledClassesForKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7575997

Please sign in to comment.