Skip to content

Commit

Permalink
Check the app's bundle version as an extra precaution against false n…
Browse files Browse the repository at this point in the history
…egatives in OOM detection
  • Loading branch information
kstenerud committed Nov 1, 2023
1 parent ccd790d commit f93446c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Bugsnag/Helpers/BSGRunContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// During development this is not strictly necessary since last run's data will
// not be loaded if the struct's size has changed.
//
#define BSGRUNCONTEXT_VERSION 4
#define BSGRUNCONTEXT_VERSION 5

struct BSGRunContext {
long structVersion;
Expand Down Expand Up @@ -50,6 +50,7 @@ struct BSGRunContext {
unsigned long long memoryAvailable;
unsigned long long memoryFootprint;
unsigned long long memoryLimit;
char bundleVersion[32]; // Won't actually get this long but just to be sure.
};

/// Information about the current run of the app / process.
Expand Down
14 changes: 12 additions & 2 deletions Bugsnag/Helpers/BSGRunContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ static void InitRunContext(void) {
if (image && image->uuid) {
uuid_copy(bsg_runContext->machoUUID, image->uuid);
}


NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
bsg_safe_strncpy(bsg_runContext->bundleVersion,
(const char*)[bundleVersion cStringUsingEncoding:NSUTF8StringEncoding],
sizeof(bsg_runContext->bundleVersion));

if ([NSThread isMainThread]) {
bsg_runContext->isActive = GetIsActive();
} else {
Expand Down Expand Up @@ -470,8 +475,13 @@ bool BSGRunContextWasKilled(void) {
if (bsg_lastRunContext->bootTime != bsg_runContext->bootTime) {
return NO; // The app may have been terminated due to the reboot
}

// Ignore unexpected terminations due to the app being upgraded
if (strncmp(bsg_lastRunContext->bundleVersion,
bsg_runContext->bundleVersion,
sizeof(bsg_runContext->bundleVersion)) != 0) {
return NO;
}
if (uuid_compare(bsg_lastRunContext->machoUUID, bsg_runContext->machoUUID)) {
return NO;
}
Expand Down
6 changes: 6 additions & 0 deletions Bugsnag/Helpers/BSGUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ static inline NSString * _Nullable BSGStringFromClass(Class _Nullable cls) {
return cls ? NSStringFromClass((Class _Nonnull)cls) : nil;
}

/**
* Copy length characters from src to dst, filling any remainder with NUL characters.
* Unlike strncpy, this function ALWAYS puts a NUL in position (length-1).
*/
void bsg_safe_strncpy(char *dst, const char *src, size_t length);

NS_ASSUME_NONNULL_END

__END_DECLS
5 changes: 5 additions & 0 deletions Bugsnag/Helpers/BSGUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

#import "BugsnagLogger.h"

void bsg_safe_strncpy(char *dst, const char *src, size_t length) {
strncpy(dst, src, length);
dst[length-1] = 0;
}

char *_Nullable BSGCStringWithData(NSData *_Nullable data) {
char *buffer;
if (data.length && (buffer = calloc(1, data.length + 1))) {
Expand Down
3 changes: 3 additions & 0 deletions Tests/BugsnagTests/BSGOutOfMemoryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ - (void)testLastLaunchTerminatedUnexpectedly {
XCTAssertFalse(BSGRunContextWasKilled());
uuid_copy(lastRunContext.machoUUID, bsg_runContext->machoUUID);

strncpy(lastRunContext.bundleVersion, "999.99", sizeof(lastRunContext.bundleVersion));
XCTAssertFalse(BSGRunContextWasKilled());

lastRunContext.bootTime = 0;
XCTAssertFalse(BSGRunContextWasKilled());
lastRunContext.bootTime = bsg_runContext->bootTime;
Expand Down

0 comments on commit f93446c

Please sign in to comment.