Skip to content

Commit

Permalink
Merge pull request #1597 from bugsnag/PLAT-11112-check-app-version
Browse files Browse the repository at this point in the history
[Plat-11112] check app version when detecting OOMs
  • Loading branch information
kstenerud authored Nov 13, 2023
2 parents d2c982b + 6840c9e commit 483715d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 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
24 changes: 19 additions & 5 deletions Bugsnag/Helpers/BSGRunContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ static void InitRunContext(void) {
if (image && image->uuid) {
uuid_copy(bsg_runContext->machoUUID, image->uuid);
}


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

if ([NSThread isMainThread]) {
bsg_runContext->isActive = GetIsActive();
} else {
Expand Down Expand Up @@ -487,8 +493,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 Expand Up @@ -516,10 +527,13 @@ bool BSGRunContextWasKilled(void) {

#define SIZEOF_STRUCT sizeof(struct BSGRunContext)

static struct BSGRunContext fallback;
struct BSGRunContext *bsg_runContext = &fallback;
static struct BSGRunContext bsg_runContext_fallback;
struct BSGRunContext *bsg_runContext = &bsg_runContext_fallback;

const struct BSGRunContext *bsg_lastRunContext;
static struct BSGRunContext bsg_lastRunContext_fallback = {
.structVersion = ~0,
};
const struct BSGRunContext *bsg_lastRunContext = &bsg_lastRunContext_fallback;

/// Opens the file and disables content protection, returning -1 on error.
static int OpenFile(NSString *_Nonnull path) {
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 characters from src to dst, up to a maximum of length bytes (including the NUL terminator).
* Unlike strncpy, this function always ensures that dst is NUL terminated (if length > 0).
*/
void bsg_safe_strncpy(char *dst, const char *src, size_t length);

NS_ASSUME_NONNULL_END

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

#import "BugsnagLogger.h"

void bsg_safe_strncpy(char *dst, const char *src, size_t length) {
if (length > 0) {
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

## TBD

### Bug fixes

* Check app version to avoid detecting an app upgrade as an OOM
[1597](https://github.com/bugsnag/bugsnag-cocoa/pull/1597)


## 6.27.2 (2023-07-24)

### Enhancements
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 483715d

Please sign in to comment.