From eeb6ddc52a019dc1a2053cd5b7be8d6ad6186721 Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Tue, 2 Jan 2024 13:54:42 +0100 Subject: [PATCH] fix: Crash when adding a crumb for a timezone change Fix a crash when adding a time crumb for a timezone change and the previously stored timezone offset is nil. Fixes GH-3513 --- CHANGELOG.md | 1 + Sources/Sentry/SentrySystemEventBreadcrumbs.m | 14 +++++++++----- .../SentrySystemEventBreadcrumbsTest.swift | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 110234facbe..8abdbd4fada 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - TTFD waits for next drawn frame (#3505) - Fix TTID/TTFD for app start transactions (#3512): TTID/TTFD spans and measurements for app start transaction now include the app start duration. +- Crash when adding a crumb for a timezone change (#3524) ## 8.17.2 diff --git a/Sources/Sentry/SentrySystemEventBreadcrumbs.m b/Sources/Sentry/SentrySystemEventBreadcrumbs.m index f482b9d725c..dcdb672e562 100644 --- a/Sources/Sentry/SentrySystemEventBreadcrumbs.m +++ b/Sources/Sentry/SentrySystemEventBreadcrumbs.m @@ -240,11 +240,15 @@ - (void)timezoneEventTriggered:(NSNumber *)storedTimezoneOffset NSInteger offset = SentryDependencyContainer.sharedInstance.dateProvider.timezoneOffset; crumb.type = @"system"; - crumb.data = @{ - @"action" : @"TIMEZONE_CHANGE", - @"previous_seconds_from_gmt" : storedTimezoneOffset, - @"current_seconds_from_gmt" : @(offset) - }; + + NSMutableDictionary *dataDict = + [@{ @"action" : @"TIMEZONE_CHANGE", @"current_seconds_from_gmt" : @(offset) } mutableCopy]; + + if (storedTimezoneOffset != nil) { + dataDict[@"previous_seconds_from_gmt"] = storedTimezoneOffset; + } + + crumb.data = dataDict; [_delegate addBreadcrumb:crumb]; [self updateStoredTimezone]; diff --git a/Tests/SentryTests/Integrations/Breadcrumbs/SentrySystemEventBreadcrumbsTest.swift b/Tests/SentryTests/Integrations/Breadcrumbs/SentrySystemEventBreadcrumbsTest.swift index 71a18216e1c..ffc8ee4cdee 100644 --- a/Tests/SentryTests/Integrations/Breadcrumbs/SentrySystemEventBreadcrumbsTest.swift +++ b/Tests/SentryTests/Integrations/Breadcrumbs/SentrySystemEventBreadcrumbsTest.swift @@ -1,3 +1,4 @@ +import Nimble @testable import Sentry import SentryTestUtils import XCTest @@ -251,6 +252,20 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase { XCTAssertEqual(data["current_seconds_from_gmt"] as? Int64, 7_200) } } + + func testTimezoneChangeNotificationBreadcrumb_NoStoredTimezoneOffset() { + sut = fixture.getSut(currentDevice: nil) + + fixture.currentDateProvider.timezoneOffsetValue = 7_200 + fixture.fileManager.deleteTimezoneOffset() + + sut.timezoneEventTriggered() + + assertBreadcrumbAction(action: "TIMEZONE_CHANGE") { data in + expect(data["previous_seconds_from_gmt"]) == nil + expect(data["current_seconds_from_gmt"] as? Int64) == 7_200 + } + } func testStopCallsSpecificRemoveObserverMethods() { sut = fixture.getSut(currentDevice: nil)