Skip to content

Commit

Permalink
Create date formatters at initialization time rather than at runtime.
Browse files Browse the repository at this point in the history
We had some null pointer exceptions in code that was using BSG_RFC3339DateTool, which seems to be caused by a nil instance of the date formatter. I couldn't find any definitive culprit, but with this change there's zero possibility of the formatter ever being nil again.
  • Loading branch information
kstenerud committed Sep 16, 2020
1 parent d131dd3 commit 6c22595
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
61 changes: 20 additions & 41 deletions Bugsnag/Helpers/BSG_RFC3339DateTool.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,66 +24,45 @@

#import "BSG_RFC3339DateTool.h"

@interface BSG_RFC3339DateTool ()
+ (NSDateFormatter *)iosFormatterInstance;
@end
// New formatter: Everything is UTC
static NSDateFormatter *g_utcDateFormatter;

// Old formatter: Time zones can be specified
static NSDateFormatter *g_timezoneAllowedDateFormatter;

@implementation BSG_RFC3339DateTool

static NSString *const kRfcDateFormatterKey = @"RfcDateFormatter";
static NSString *const kIsoDateFormatterKey = @"IsoDateFormatter";
+ (void)initialize {
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0];

+ (NSDateFormatter *)sharedInstance {
NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
NSDateFormatter *formatter = threadDict[kRfcDateFormatterKey];
if (formatter == nil) {
formatter = [NSDateFormatter new];
NSLocale *locale =
[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:locale];
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
threadDict[kRfcDateFormatterKey] = formatter;
}
g_utcDateFormatter = [NSDateFormatter new];
[g_utcDateFormatter setLocale:locale];
[g_utcDateFormatter setTimeZone:zone];
[g_utcDateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

formatter = threadDict[kRfcDateFormatterKey];
return formatter;
g_timezoneAllowedDateFormatter = [NSDateFormatter new];
[g_timezoneAllowedDateFormatter setLocale:locale];
[g_timezoneAllowedDateFormatter setTimeZone:zone];
[g_timezoneAllowedDateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"];
}

/**
Used internally to convert any dates with timezones (from older notifier versions) to Zulu time
*/
+ (NSDateFormatter *)iosFormatterInstance {
NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
NSDateFormatter *formatter = threadDict[kIsoDateFormatterKey];
if (formatter == nil) {
formatter = [NSDateFormatter new];
NSLocale *locale =
[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:locale];
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
threadDict[kIsoDateFormatterKey] = formatter;
}

formatter = threadDict[kIsoDateFormatterKey];
return formatter;
}

+ (NSString *)stringFromDate:(NSDate *)date {
if (![date isKindOfClass:[NSDate class]]) {
return nil;
}
return [[self sharedInstance] stringFromDate:date];
return [g_utcDateFormatter stringFromDate:date];
}

+ (NSDate *)dateFromString:(NSString *)string {
if (![string isKindOfClass:[NSString class]]) {
return nil;
}
NSDate *date = [[self sharedInstance] dateFromString:string];
NSDate *date = [g_utcDateFormatter dateFromString:string];
if (!date) {
date = [[self iosFormatterInstance] dateFromString:string];
// Fallback to older date format that included time zones
date = [g_timezoneAllowedDateFormatter dateFromString:string];
}
return date;
}
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Changelog
* Disable JSON pretty-printing in KSCrash reports to save disk space and bandwidth.
[802](https://github.com/bugsnag/bugsnag-cocoa/pull/802)

### Bug fixes

* Create date formatters at init time to avoid potential race conditions.
[807](https://github.com/bugsnag/bugsnag-cocoa/pull/807)

## 6.1.4 (2020-09-11)

### Bug fixes
Expand Down

0 comments on commit 6c22595

Please sign in to comment.