Skip to content

Commit

Permalink
fix: Remove "/" from crash report file name (#3005)
Browse files Browse the repository at this point in the history
Crash report uses bundleName to create the report file, but "/" is a valid character for bundle name but not for file name.
Replace "/" with "-" for the report file name.
  • Loading branch information
brustolin committed May 8, 2023
1 parent a6ac3d2 commit c791a49
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This change has no impact on grouping of the issues in Sentry.
### Fixes

- Propagate span when copying scope (#2952)
- Remove "/" from crash report file name (#3005)

## 8.6.0

Expand Down
62 changes: 32 additions & 30 deletions Sources/SentryCrash/Recording/SentryCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,6 @@

@end

static NSString *
getBundleName(void)
{
NSString *bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
if (bundleName == nil) {
bundleName = @"Unknown";
}
return bundleName;
}

static NSString *
getBasePath(void)
{
NSArray *directories
= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
if ([directories count] == 0) {
SentryCrashLOG_ERROR(@"Could not locate cache directory path.");
return nil;
}
NSString *cachePath = [directories objectAtIndex:0];
if ([cachePath length] == 0) {
SentryCrashLOG_ERROR(@"Could not locate cache directory path.");
return nil;
}
NSString *pathEnd = [@"SentryCrash" stringByAppendingPathComponent:getBundleName()];
return [cachePath stringByAppendingPathComponent:pathEnd];
}

@implementation SentryCrash

// ============================================================================
Expand Down Expand Up @@ -126,13 +98,13 @@ + (instancetype)sharedInstance

- (id)init
{
return [self initWithBasePath:getBasePath()];
return [self initWithBasePath:[self getBasePath]];
}

- (id)initWithBasePath:(NSString *)basePath
{
if ((self = [super init])) {
self.bundleName = getBundleName();
self.bundleName = [self getBundleName];
self.basePath = basePath;
if (self.basePath == nil) {
SentryCrashLOG_ERROR(@"Failed to initialize crash handler. Crash "
Expand Down Expand Up @@ -562,6 +534,36 @@ - (void)applicationWillTerminate
sentrycrash_notifyAppTerminate();
}

- (NSString *)clearBundleName:(NSString *)filename
{
// The bundle name is used as file name, therefore "/" is not allowed.
return [filename stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
}

- (NSString *)getBundleName
{
NSString *bundleName =
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"] ?: @"Unknown";
return [self clearBundleName:bundleName];
}

- (NSString *)getBasePath
{
NSArray *directories
= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
if ([directories count] == 0) {
SentryCrashLOG_ERROR(@"Could not locate cache directory path.");
return nil;
}
NSString *cachePath = [directories objectAtIndex:0];
if ([cachePath length] == 0) {
SentryCrashLOG_ERROR(@"Could not locate cache directory path.");
return nil;
}
NSString *pathEnd = [@"SentryCrash" stringByAppendingPathComponent:[self getBundleName]];
return [cachePath stringByAppendingPathComponent:pathEnd];
}

@end

//! Project version number for SentryCrashFramework.
Expand Down
13 changes: 13 additions & 0 deletions Tests/SentryTests/SentryCrash/SentryCrashTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ @interface SentryCrashTests : FileBasedTestCase
@end

@interface SentryCrash (private)

- (NSString *)clearBundleName:(NSString *)filename;

- (NSArray *)getAttachmentPaths:(int64_t)reportID;

@end

@implementation SentryCrashTests
Expand Down Expand Up @@ -43,6 +47,15 @@ - (void)test_getScreenshots_TwoFiles
XCTAssertEqual(files.count, 2);
}

- (void)test_cleanBundleName
{
SentryCrash *sentryCrash = [[SentryCrash alloc] init];

NSString *clearedBundleName = [sentryCrash clearBundleName:@"Sentry/Test"];

XCTAssertEqualObjects(clearedBundleName, @"Sentry-Test");
}

- (void)test_getScreenshots_NoFiles
{
[self initReport:12 withScreenshots:0];
Expand Down

0 comments on commit c791a49

Please sign in to comment.