Skip to content

Commit 9788cc8

Browse files
authored
refactor: Add null-handling to tracer (#5888)
1 parent ef46b05 commit 9788cc8

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Sources/Sentry/SentryTraceContext.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#import "SentryBaggage.h"
33
#import "SentryDefines.h"
44
#import "SentryDsn.h"
5+
#import "SentryInternalDefines.h"
56
#import "SentryLogC.h"
67
#import "SentryOptions+Private.h"
78
#import "SentrySampleDecision.h"
@@ -78,17 +79,17 @@ - (nullable instancetype)initWithScope:(SentryScope *)scope options:(SentryOptio
7879
SentryTracer *tracer = [SentryTracer getTracer:scope.span];
7980
if (tracer == nil) {
8081
return nil;
81-
} else {
82-
return [self initWithTracer:tracer scope:scope options:options];
8382
}
83+
return [self initWithTracer:tracer scope:scope options:options];
8484
}
8585

8686
- (nullable instancetype)initWithTracer:(SentryTracer *)tracer
8787
scope:(nullable SentryScope *)scope
8888
options:(SentryOptions *)options
8989
{
90-
if (tracer.traceId == nil || options.parsedDsn == nil)
90+
if (tracer.traceId == nil || options.parsedDsn == nil) {
9191
return nil;
92+
}
9293

9394
#if !SDK_V9
9495
NSString *userSegment = nil;
@@ -154,7 +155,14 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
154155

155156
- (nullable instancetype)initWithDict:(NSDictionary<NSString *, id> *)dictionary
156157
{
157-
SentryId *traceId = [[SentryId alloc] initWithUUIDString:dictionary[@"trace_id"]];
158+
NSString *_Nullable rawTraceId = dictionary[@"trace_id"];
159+
if (rawTraceId == nil || ![rawTraceId isKindOfClass:[NSString class]]) {
160+
SENTRY_LOG_ERROR(@"Invalid trace_id: %@", rawTraceId);
161+
return nil;
162+
}
163+
164+
SentryId *traceId =
165+
[[SentryId alloc] initWithUUIDString:SENTRY_UNWRAP_NULLABLE(NSString, rawTraceId)];
158166
NSString *publicKey = dictionary[@"public_key"];
159167
if (traceId == nil || publicKey == nil)
160168
return nil;

Sources/Sentry/SentryTracer.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ + (void)resetAppStartMeasurementRead
919919
}
920920
}
921921

922-
+ (nullable SentryTracer *)getTracer:(id<SentrySpan>)span
922+
+ (nullable SentryTracer *)getTracer:(id<SentrySpan> _Nullable)span
923923
{
924924
if (span == nil) {
925925
return nil;

Sources/Sentry/include/SentryTracer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static const NSTimeInterval SENTRY_AUTO_TRANSACTION_MAX_DURATION = 500.0;
8888
/**
8989
* Get the tracer from a span.
9090
*/
91-
+ (nullable SentryTracer *)getTracer:(id<SentrySpan>)span;
91+
+ (nullable SentryTracer *)getTracer:(id<SentrySpan> _Nullable)span;
9292

9393
- (void)startIdleTimeout;
9494

0 commit comments

Comments
 (0)