From 6451fac54267134ed760cb6083a4066c7060f070 Mon Sep 17 00:00:00 2001 From: Karl Stenerud Date: Tue, 5 Jul 2022 14:29:12 +0200 Subject: [PATCH] Make sure parts are properly initialized before use --- Bugsnag/Client/BugsnagClient.m | 14 ++++++------- .../Source/KSCrash/Recording/BSG_KSCrashC.c | 2 +- .../KSCrash/Recording/BSG_KSCrashState.h | 4 +--- .../KSCrash/Recording/BSG_KSCrashState.m | 20 +++++++++---------- .../KSCrash/Recording/BSG_KSSystemInfo.m | 3 +-- 5 files changed, 19 insertions(+), 24 deletions(-) diff --git a/Bugsnag/Client/BugsnagClient.m b/Bugsnag/Client/BugsnagClient.m index 53bec162c..5a5801871 100644 --- a/Bugsnag/Client/BugsnagClient.m +++ b/Bugsnag/Client/BugsnagClient.m @@ -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:@{ @@ -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; } @@ -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: diff --git a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashC.c b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashC.c index aa98477e4..5efc40ded 100644 --- a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashC.c +++ b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashC.c @@ -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(); } } @@ -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(); diff --git a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.h b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.h index c7c6652c3..f65ac0b4f 100644 --- a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.h +++ b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.h @@ -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 } diff --git a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m index 69c5675b1..6b1b6908c 100644 --- a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m +++ b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m @@ -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) { diff --git a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m index 6ad03c8de..c398e8dc1 100644 --- a/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m +++ b/Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m @@ -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" @@ -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);