Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Fixes

- Fixed crashes when adding breadcrumbs on iOS/macOS. This was caused by invalid date parsing and corrupted string data in the native bridge responsible for scope sync ([#2327](https://github.com/getsentry/sentry-unity/pull/2327))
- Fixed input handling in samples to work with old and new input system ([#2319](https://github.com/getsentry/sentry-unity/pull/2319))

### Dependencies
Expand Down
101 changes: 69 additions & 32 deletions package-dev/Plugins/iOS/SentryNativeBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,28 @@ void SentryNativeBridgeAddBreadcrumb(
return;
}

NSString *timestampString = (timestamp != NULL) ? [NSString stringWithUTF8String:timestamp] : nil;
NSString *messageString = (message != NULL) ? [NSString stringWithUTF8String:message] : nil;
NSString *typeString = (type != NULL) ? [NSString stringWithUTF8String:type] : nil;
NSString *categoryString = (category != NULL) ? [NSString stringWithUTF8String:category] : nil;

[SentrySDK configureScope:^(SentryScope *scope) {
SentryBreadcrumb *breadcrumb = [[SentryBreadcrumb alloc]
initWithLevel:level
category:(category ? [NSString stringWithUTF8String:category] : nil)];
category:categoryString];

if (timestamp != NULL) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSCalendarIdentifierISO8601];
breadcrumb.timestamp =
[dateFormatter dateFromString:[NSString stringWithUTF8String:timestamp]];
if (timestampString != nil && timestampString.length > 0) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
breadcrumb.timestamp = [formatter dateFromString:timestampString];
}

if (message != NULL) {
breadcrumb.message = [NSString stringWithUTF8String:message];
if (messageString != nil) {
breadcrumb.message = messageString;
}

if (type != NULL) {
breadcrumb.type = [NSString stringWithUTF8String:type];
if (typeString != nil) {
breadcrumb.type = typeString;
}

[scope addBreadcrumb:breadcrumb];
Expand All @@ -94,12 +98,17 @@ void SentryNativeBridgeSetExtra(const char *key, const char *value)
return;
}

NSString *keyString = [NSString stringWithUTF8String:key];
NSString *valueString = nil;
if (value != NULL) {
valueString = [NSString stringWithUTF8String:value];
}

[SentrySDK configureScope:^(SentryScope *scope) {
if (value != NULL) {
[scope setExtraValue:[NSString stringWithUTF8String:value]
forKey:[NSString stringWithUTF8String:key]];
if (valueString != nil) {
[scope setExtraValue:valueString forKey:keyString];
} else {
[scope removeExtraForKey:[NSString stringWithUTF8String:key]];
[scope removeExtraForKey:keyString];
}
}];
}
Expand All @@ -110,12 +119,17 @@ void SentryNativeBridgeSetTag(const char *key, const char *value)
return;
}

NSString *keyString = [NSString stringWithUTF8String:key];
NSString *valueString = nil;
if (value != NULL) {
valueString = [NSString stringWithUTF8String:value];
}

[SentrySDK configureScope:^(SentryScope *scope) {
if (value != NULL) {
[scope setTagValue:[NSString stringWithUTF8String:value]
forKey:[NSString stringWithUTF8String:key]];
if (valueString != nil) {
[scope setTagValue:valueString forKey:keyString];
} else {
[scope removeTagForKey:[NSString stringWithUTF8String:key]];
[scope removeTagForKey:keyString];
}
}];
}
Expand All @@ -126,8 +140,11 @@ void SentryNativeBridgeUnsetTag(const char *key)
return;
}

[SentrySDK configureScope:^(
SentryScope *scope) { [scope removeTagForKey:[NSString stringWithUTF8String:key]]; }];
NSString *keyString = [NSString stringWithUTF8String:key];

[SentrySDK configureScope:^(SentryScope *scope) {
[scope removeTagForKey:keyString];
}];
}

void SentryNativeBridgeSetUser(
Expand All @@ -137,23 +154,43 @@ void SentryNativeBridgeSetUser(
return;
}

NSString *emailString = nil;
if (email != NULL) {
emailString = [NSString stringWithUTF8String:email];
}

NSString *userIdString = nil;
if (userId != NULL) {
userIdString = [NSString stringWithUTF8String:userId];
}

NSString *ipAddressString = nil;
if (ipAddress != NULL) {
ipAddressString = [NSString stringWithUTF8String:ipAddress];
}

NSString *usernameString = nil;
if (username != NULL) {
usernameString = [NSString stringWithUTF8String:username];
}

[SentrySDK configureScope:^(SentryScope *scope) {
SentryUser *user = [[SentryUser alloc] init];

if (email != NULL) {
user.email = [NSString stringWithUTF8String:email];
if (emailString != nil) {
user.email = emailString;
}

if (userId != NULL) {
user.userId = [NSString stringWithUTF8String:userId];
if (userIdString != nil) {
user.userId = userIdString;
}

if (ipAddress != NULL) {
user.ipAddress = [NSString stringWithUTF8String:ipAddress];
if (ipAddressString != nil) {
user.ipAddress = ipAddressString;
}

if (username != NULL) {
user.username = [NSString stringWithUTF8String:username];
if (usernameString != nil) {
user.username = usernameString;
}

[scope setUser:user];
Expand Down Expand Up @@ -184,15 +221,15 @@ void SentryNativeBridgeSetTrace(const char *traceId, const char *spanId)

NSString *traceIdStr = [NSString stringWithUTF8String:traceId];
NSString *spanIdStr = [NSString stringWithUTF8String:spanId];

// This is a workaround to deal with SentryId living inside the Swift header
Class sentryIdClass = NSClassFromString(@"_TtC6Sentry8SentryId");
Class sentrySpanIdClass = NSClassFromString(@"SentrySpanId");

if (sentryIdClass && sentrySpanIdClass) {
id sentryTraceId = [[sentryIdClass alloc] initWithUUIDString:traceIdStr];
id sentrySpanId = [[sentrySpanIdClass alloc] initWithValue:spanIdStr];

if (sentryTraceId && sentrySpanId) {
[PrivateSentrySDKOnly setTrace:sentryTraceId spanId:sentrySpanId];
}
Expand Down Expand Up @@ -288,4 +325,4 @@ void SentryNativeBridgeWriteScope( // clang-format off
}];
}

NS_ASSUME_NONNULL_END
NS_ASSUME_NONNULL_END
99 changes: 69 additions & 30 deletions package-dev/Plugins/macOS/SentryNativeBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,33 @@ void SentryNativeBridgeAddBreadcrumb(
return;
}

NSString *timestampString = (timestamp != NULL) ? [NSString stringWithUTF8String:timestamp] : nil;
NSString *messageString = (message != NULL) ? [NSString stringWithUTF8String:message] : nil;
NSString *typeString = (type != NULL) ? [NSString stringWithUTF8String:type] : nil;
NSString *categoryString = (category != NULL) ? [NSString stringWithUTF8String:category] : nil;

SentryConfigureScope(^(id scope) {
id breadcrumb = [[SentryBreadcrumb alloc] init];

if (timestamp != NULL) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSCalendarIdentifierISO8601];
[breadcrumb
setValue:[dateFormatter dateFromString:[NSString stringWithUTF8String:timestamp]]
forKey:@"timestamp"];
if (timestampString != nil && timestampString.length > 0) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [formatter dateFromString:timestampString];
if (date != nil) {
[breadcrumb setValue:date forKey:@"timestamp"];
}
}

if (message != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:message] forKey:@"message"];
if (messageString != nil) {
[breadcrumb setValue:messageString forKey:@"message"];
}

if (type != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:type] forKey:@"type"];
if (typeString != nil) {
[breadcrumb setValue:typeString forKey:@"type"];
}

if (category != NULL) {
[breadcrumb setValue:[NSString stringWithUTF8String:category] forKey:@"category"];
if (categoryString != nil) {
[breadcrumb setValue:categoryString forKey:@"category"];
}

[breadcrumb setValue:[NSNumber numberWithInt:level] forKey:@"level"];
Expand All @@ -191,14 +197,20 @@ void SentryNativeBridgeSetExtra(const char *key, const char *value)
return;
}

NSString *keyString = [NSString stringWithUTF8String:key];
NSString *valueString = nil;
if (value != NULL) {
valueString = [NSString stringWithUTF8String:value];
}

SentryConfigureScope(^(id scope) {
if (value != NULL) {
if (valueString != nil) {
[scope performSelector:@selector(setExtraValue:forKey:)
withObject:[NSString stringWithUTF8String:value]
withObject:[NSString stringWithUTF8String:key]];
withObject:valueString
withObject:keyString];
} else {
[scope performSelector:@selector(removeExtraForKey:)
withObject:[NSString stringWithUTF8String:key]];
withObject:keyString];
}
});
}
Expand All @@ -209,14 +221,20 @@ void SentryNativeBridgeSetTag(const char *key, const char *value)
return;
}

NSString *keyString = [NSString stringWithUTF8String:key];
NSString *valueString = nil;
if (value != NULL) {
valueString = [NSString stringWithUTF8String:value];
}

SentryConfigureScope(^(id scope) {
if (value != NULL) {
if (valueString != nil) {
[scope performSelector:@selector(setTagValue:forKey:)
withObject:[NSString stringWithUTF8String:value]
withObject:[NSString stringWithUTF8String:key]];
withObject:valueString
withObject:keyString];
} else {
[scope performSelector:@selector(removeTagForKey:)
withObject:[NSString stringWithUTF8String:key]];
withObject:keyString];
}
});
}
Expand All @@ -227,9 +245,10 @@ void SentryNativeBridgeUnsetTag(const char *key)
return;
}

NSString *keyString = [NSString stringWithUTF8String:key];

SentryConfigureScope(^(id scope) {
[scope performSelector:@selector(removeTagForKey:)
withObject:[NSString stringWithUTF8String:key]];
[scope performSelector:@selector(removeTagForKey:) withObject:keyString];
});
}

Expand All @@ -240,23 +259,43 @@ void SentryNativeBridgeSetUser(
return;
}

NSString *emailString = nil;
if (email != NULL) {
emailString = [NSString stringWithUTF8String:email];
}

NSString *userIdString = nil;
if (userId != NULL) {
userIdString = [NSString stringWithUTF8String:userId];
}

NSString *ipAddressString = nil;
if (ipAddress != NULL) {
ipAddressString = [NSString stringWithUTF8String:ipAddress];
}

NSString *usernameString = nil;
if (username != NULL) {
usernameString = [NSString stringWithUTF8String:username];
}

SentryConfigureScope(^(id scope) {
id user = [[SentryUser alloc] init];

if (email != NULL) {
[user setValue:[NSString stringWithUTF8String:email] forKey:@"email"];
if (emailString != nil) {
[user setValue:emailString forKey:@"email"];
}

if (userId != NULL) {
[user setValue:[NSString stringWithUTF8String:userId] forKey:@"userId"];
if (userIdString != nil) {
[user setValue:userIdString forKey:@"userId"];
}

if (ipAddress != NULL) {
[user setValue:[NSString stringWithUTF8String:ipAddress] forKey:@"ipAddress"];
if (ipAddressString != nil) {
[user setValue:ipAddressString forKey:@"ipAddress"];
}

if (username != NULL) {
[user setValue:[NSString stringWithUTF8String:username] forKey:@"username"];
if (usernameString != nil) {
[user setValue:usernameString forKey:@"username"];
}

[scope performSelector:@selector(setUser:) withObject:user];
Expand Down
Loading