Skip to content

Commit d3e7aa6

Browse files
authored
fix: Add null-handling for sentry level parsing (#5803)
1 parent 2a6f451 commit d3e7aa6

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@
888888
D4EDF9842D0B2A210071E7B3 /* Data+SentryTracing.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4EDF9832D0B2A1D0071E7B3 /* Data+SentryTracing.swift */; };
889889
D4EE12D22DE9AC3800385BAF /* TestNSNotificationCenterWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4EE12D12DE9AC3300385BAF /* TestNSNotificationCenterWrapperTests.swift */; };
890890
D4F2B5352D0C69D500649E42 /* SentryCrashCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4F2B5342D0C69D100649E42 /* SentryCrashCTests.swift */; };
891+
D4F7BD822E4373BF004A2D77 /* SentryLevelMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4F7BD7C2E4373BB004A2D77 /* SentryLevelMapperTests.swift */; };
891892
D4FC68172DD632E7001B74FF /* SentryDispatchSourceProviderProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = D4FC68162DD632E7001B74FF /* SentryDispatchSourceProviderProtocol.h */; };
892893
D4FC681A2DD63465001B74FF /* SentryDispatchQueueWrapperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D4FC68192DD63465001B74FF /* SentryDispatchQueueWrapperTests.m */; };
893894
D8019910286B089000C277F0 /* SentryCrashReportSinkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D801990F286B089000C277F0 /* SentryCrashReportSinkTests.swift */; };
@@ -2214,6 +2215,7 @@
22142215
D4EDF9832D0B2A1D0071E7B3 /* Data+SentryTracing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+SentryTracing.swift"; sourceTree = "<group>"; };
22152216
D4EE12D12DE9AC3300385BAF /* TestNSNotificationCenterWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestNSNotificationCenterWrapperTests.swift; sourceTree = "<group>"; };
22162217
D4F2B5342D0C69D100649E42 /* SentryCrashCTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCrashCTests.swift; sourceTree = "<group>"; };
2218+
D4F7BD7C2E4373BB004A2D77 /* SentryLevelMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryLevelMapperTests.swift; sourceTree = "<group>"; };
22172219
D4FC68162DD632E7001B74FF /* SentryDispatchSourceProviderProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryDispatchSourceProviderProtocol.h; path = include/SentryDispatchSourceProviderProtocol.h; sourceTree = "<group>"; };
22182220
D4FC68192DD63465001B74FF /* SentryDispatchQueueWrapperTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryDispatchQueueWrapperTests.m; sourceTree = "<group>"; };
22192221
D801990F286B089000C277F0 /* SentryCrashReportSinkTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCrashReportSinkTests.swift; sourceTree = "<group>"; };
@@ -3575,6 +3577,7 @@
35753577
7BD7299B24654CD500EA3610 /* Helper */ = {
35763578
isa = PBXGroup;
35773579
children = (
3580+
D4F7BD7C2E4373BB004A2D77 /* SentryLevelMapperTests.swift */,
35783581
D8AE48BE2C578D540092A2A6 /* SentrySDKLog.swift */,
35793582
849AC3FF29E0C1FF00889C16 /* SentryFormatterTests.swift */,
35803583
7B88F30324BC8E6500ADF90A /* SentrySerializationTests.swift */,
@@ -6148,6 +6151,7 @@
61486151
7BB6550D253EEB3900887E87 /* SentryUserFeedbackTests.swift in Sources */,
61496152
7BBD18B7245180FF00427C76 /* SentryDsnTests.m in Sources */,
61506153
D8DBE0D22C0EFFC300FAB1FD /* SentryReplayOptionsTests.swift in Sources */,
6154+
D4F7BD822E4373BF004A2D77 /* SentryLevelMapperTests.swift in Sources */,
61516155
D4291A6D2DD62ACE00772088 /* SentryDispatchFactoryTests.m in Sources */,
61526156
62950F1029E7FE0100A42624 /* SentryTransactionContextTests.swift in Sources */,
61536157
0A2D7BBA29152CBF008727AF /* SentryWatchdogTerminationScopeObserverTests.swift in Sources */,

Sources/Sentry/SentryHub.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "SentryHub+Private.h"
1010
#import "SentryInstallation.h"
1111
#import "SentryIntegrationProtocol.h"
12+
#import "SentryInternalDefines.h"
1213
#import "SentryLevelMapper.h"
1314
#import "SentryLogC.h"
1415
#import "SentryNSTimerFactory.h"
@@ -774,11 +775,13 @@ - (BOOL)envelopeContainsEventWithErrorOrHigher:(NSArray<SentryEnvelopeItem *> *)
774775
for (SentryEnvelopeItem *item in items) {
775776
if ([item.header.type isEqualToString:SentryEnvelopeItemTypeEvent]) {
776777
// If there is no level the default is error
777-
NSDictionary *_Nullable eventJson =
778+
NSDictionary *_Nullable nullableEventJson =
778779
[SentrySerialization deserializeDictionaryFromJsonData:item.data];
779-
if (eventJson == nil) {
780+
if (nullableEventJson == nil) {
780781
return NO;
781782
}
783+
NSDictionary *_Nonnull eventJson
784+
= SENTRY_UNWRAP_NULLABLE(NSDictionary, nullableEventJson);
782785

783786
SentryLevel level = sentryLevelForString(eventJson[@"level"]);
784787
if (level >= kSentryLevelError) {

Sources/Sentry/SentryLevelMapper.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
NS_ASSUME_NONNULL_BEGIN
44

55
SentryLevel
6-
sentryLevelForString(NSString *string)
6+
sentryLevelForString(NSString *_Nullable string)
77
{
88
return [SentryLevelHelper levelForName:string];
99
}

Sources/Sentry/include/SentryLevelMapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ NS_ASSUME_NONNULL_BEGIN
66
* Maps a string to a SentryLevel. If the passed string doesn't match any level this defaults to
77
* the 'error' level. See https://develop.sentry.dev/sdk/event-payloads/#optional-attributes
88
*/
9-
SentryLevel sentryLevelForString(NSString *string);
9+
SentryLevel sentryLevelForString(NSString *_Nullable string);
1010

1111
NSString *nameForSentryLevel(SentryLevel level);
1212

Sources/Swift/Core/Helper/Log/SentryLevel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ extension SentryLevel: CustomStringConvertible {
2929
return SentryLevel.levelNames[Int(self.rawValue)]
3030
}
3131

32-
static func fromName(_ name: String) -> SentryLevel {
33-
guard let index = SentryLevel.levelNames.firstIndex(of: name) else { return .error }
32+
static func fromName(_ name: String?) -> SentryLevel {
33+
guard let name = name, let index = SentryLevel.levelNames.firstIndex(of: name) else { return .error }
3434
return SentryLevel(rawValue: UInt(index)) ?? .error
3535
}
3636
}
@@ -41,7 +41,7 @@ extension SentryLevel: CustomStringConvertible {
4141
return level.description
4242
}
4343

44-
public static func levelForName(_ name: String) -> SentryLevel {
44+
public static func levelForName(_ name: String?) -> SentryLevel {
4545
.fromName(name)
4646
}
4747
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@testable import Sentry
2+
import XCTest
3+
4+
class SentryLevelMapperTests: XCTestCase {
5+
func testSentryLevelForString_nilParameter_shouldReturnDefault() {
6+
XCTAssertEqual(sentryLevelForString(nil), .error)
7+
}
8+
9+
func testSentryLevelForString_emptyString_shouldReturnDefault() {
10+
XCTAssertEqual(sentryLevelForString(""), .error)
11+
}
12+
13+
func testSentryLevelForString_invalidString_shouldReturnDefault() {
14+
XCTAssertEqual(sentryLevelForString("invalid"), .error)
15+
}
16+
17+
func testSentryLevelForString_validString_shouldReturnCorrectLevel() {
18+
XCTAssertEqual(sentryLevelForString("none"), .none)
19+
XCTAssertEqual(sentryLevelForString("debug"), .debug)
20+
XCTAssertEqual(sentryLevelForString("info"), .info)
21+
XCTAssertEqual(sentryLevelForString("warning"), .warning)
22+
XCTAssertEqual(sentryLevelForString("error"), .error)
23+
XCTAssertEqual(sentryLevelForString("fatal"), .fatal)
24+
}
25+
}

0 commit comments

Comments
 (0)