Skip to content

Commit

Permalink
Make sure parts are properly initialized before use
Browse files Browse the repository at this point in the history
  • Loading branch information
kstenerud committed Jul 5, 2022
1 parent b1e5a40 commit 6451fac
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
14 changes: 7 additions & 7 deletions Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ - (instancetype)initWithConfiguration:(BugsnagConfiguration *)configuration {
if (!_configuration.user.id) { // populate with an autogenerated ID if no value set
[_configuration setUser:[BSG_KSSystemInfo deviceAndAppHash] withEmail:_configuration.user.email andName:_configuration.user.name];
}

[_configuration validate];

_featureFlagStore = [configuration.featureFlagStore mutableCopy];

_state = [[BugsnagMetadata alloc] initWithDictionary:@{
Expand Down Expand Up @@ -193,10 +194,6 @@ - (instancetype)initWithConfiguration:(BugsnagConfiguration *)configuration {

// Start with a copy of the configuration metadata
self.metadata = [[_configuration metadata] deepCopy];
// add metadata about app/device
NSDictionary *systemInfo = [BSG_KSSystemInfo systemInfo];
[self.metadata addMetadata:BSGParseAppMetadata(@{@"system": systemInfo}) toSection:BSGKeyApp];
[self.metadata addMetadata:BSGParseDeviceMetadata(@{@"system": systemInfo}) toSection:BSGKeyDevice];
}
return self;
}
Expand All @@ -208,12 +205,15 @@ - (void)start {
bsg_log_debug(@"Internal error reporting was disable in config");
}

[self.configuration validate];

BSGRunContextInit(BSGFileLocations.current.runContext);
BSGCrashSentryInstall(self.configuration, BSSerializeDataCrashHandler);
self.systemState = [[BugsnagSystemState alloc] initWithConfiguration:self.configuration];

// add metadata about app/device
NSDictionary *systemInfo = [BSG_KSSystemInfo systemInfo];
[self.metadata addMetadata:BSGParseAppMetadata(@{@"system": systemInfo}) toSection:BSGKeyApp];
[self.metadata addMetadata:BSGParseDeviceMetadata(@{@"system": systemInfo}) toSection:BSGKeyDevice];

[self computeDidCrashLastLaunch];

// These files can only be overwritten once the previous contents have been read; see -generateEventForLastLaunchWithError:
Expand Down
2 changes: 1 addition & 1 deletion Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashC.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void bsg_kscrash_init(void) {
if (!bsg_g_initialised) {
bsg_g_initialised = true;
bsg_g_crashReportContext.config.handlingCrashTypes = BSG_KSCrashTypeProductionSafe;
bsg_ksmach_init();
}
}

Expand All @@ -111,7 +112,6 @@ BSG_KSCrashType bsg_kscrash_install(const char *const crashReportFilePath,
}
bsg_g_installed = 1;

bsg_ksmach_init();

if (context->config.introspectionRules.enabled) {
bsg_ksobjc_init();
Expand Down
4 changes: 1 addition & 3 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ const BSG_KSCrash_State *bsg_kscrashstate_currentState(void);
/**
* Updates the stats for duration in foreground/background. This needs to
* be updated whenever an error report is captured.
*
* @param state the kscrash state
*/
void bsg_kscrashstate_updateDurationStats(BSG_KSCrash_State *const state);
void bsg_kscrashstate_updateDurationStats(void);

#ifdef __cplusplus
}
Expand Down
20 changes: 9 additions & 11 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,21 @@ void bsg_kscrashstate_notifyAppInForeground(const bool isInForeground) {
}

void bsg_kscrashstate_notifyAppCrash(void) {
BSG_KSCrash_State *const state = bsg_g_state;
const char *const stateFilePath = bsg_g_stateFilePath;
bsg_kscrashstate_updateDurationStats(state);
state->crashedThisLaunch = YES;
bsg_kscrashstate_i_saveState(state, stateFilePath);
bsg_kscrashstate_updateDurationStats();
bsg_g_state->crashedThisLaunch = YES;
bsg_kscrashstate_i_saveState(bsg_g_state, bsg_g_stateFilePath);
}

void bsg_kscrashstate_updateDurationStats(BSG_KSCrash_State *const state) {
void bsg_kscrashstate_updateDurationStats() {
uint64_t timeNow = mach_absolute_time();
const double duration = bsg_ksmachtimeDifferenceInSeconds(
timeNow, state->lastUpdateDurationsTime ?: state->appLaunchTime);
if (state->applicationIsInForeground) {
state->foregroundDurationSinceLaunch += duration;
timeNow, bsg_g_state->lastUpdateDurationsTime ?: bsg_g_state->appLaunchTime);
if (bsg_g_state->applicationIsInForeground) {
bsg_g_state->foregroundDurationSinceLaunch += duration;
} else {
state->backgroundDurationSinceLaunch += duration;
bsg_g_state->backgroundDurationSinceLaunch += duration;
}
state->lastUpdateDurationsTime = timeNow;
bsg_g_state->lastUpdateDurationsTime = timeNow;
}

const BSG_KSCrash_State *bsg_kscrashstate_currentState(void) {
Expand Down
3 changes: 1 addition & 2 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#import "BSGKeys.h"
#import "BSG_Jailbreak.h"
#import "BSG_KSCrash.h"
#import "BSG_KSCrashC.h"
#import "BSG_KSCrashReportFields.h"
#import "BSG_KSFileUtils.h"
Expand Down Expand Up @@ -453,8 +452,8 @@ + (NSDictionary *)systemInfo {
}
}

bsg_kscrashstate_updateDurationStats();
BSG_KSCrash_State state = crashContext()->state;
bsg_kscrashstate_updateDurationStats(&state);
NSMutableDictionary *statsInfo = [NSMutableDictionary dictionary];
statsInfo[@ BSG_KSCrashField_ActiveTimeSinceLaunch] = @(state.foregroundDurationSinceLaunch);
statsInfo[@ BSG_KSCrashField_BGTimeSinceLaunch] = @(state.backgroundDurationSinceLaunch);
Expand Down

0 comments on commit 6451fac

Please sign in to comment.