Skip to content

Commit 507f924

Browse files
authored
Fix corrupted UUID on Motorola devices (#2363)
1 parent be45a71 commit 507f924

File tree

7 files changed

+54
-5
lines changed

7 files changed

+54
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Changelog
22

3-
## 6.7.0
3+
## Unreleased
44

55
### Fixes
66

77
- Fix `Gpu.vendorId` should be a String ([#2343](https://github.com/getsentry/sentry-java/pull/2343))
88
- Don't set device name on Android if `sendDefaultPii` is disabled ([#2354](https://github.com/getsentry/sentry-java/pull/2354))
9+
- Fix corrupted UUID on Motorola devices ([#2363](https://github.com/getsentry/sentry-java/pull/2363))
910

1011
### Features
1112

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,7 @@ public final class io/sentry/util/StringUtils {
33763376
public static fun capitalize (Ljava/lang/String;)Ljava/lang/String;
33773377
public static fun countOf (Ljava/lang/String;C)I
33783378
public static fun getStringAfterDot (Ljava/lang/String;)Ljava/lang/String;
3379+
public static fun normalizeUUID (Ljava/lang/String;)Ljava/lang/String;
33793380
public static fun removeSurrounding (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
33803381
}
33813382

sentry/src/main/java/io/sentry/SpanId.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.sentry;
22

33
import io.sentry.util.Objects;
4+
import io.sentry.util.StringUtils;
45
import java.io.IOException;
56
import java.util.UUID;
67
import org.jetbrains.annotations.NotNull;
78

89
public final class SpanId implements JsonSerializable {
9-
public static final SpanId EMPTY_ID = new SpanId(new UUID(0, 0).toString());
10+
public static final SpanId EMPTY_ID = new SpanId(new UUID(0, 0));
1011

1112
private final @NotNull String value;
1213

@@ -19,7 +20,7 @@ public SpanId() {
1920
}
2021

2122
private SpanId(final @NotNull UUID uuid) {
22-
this(uuid.toString().replace("-", "").substring(0, 16));
23+
this(StringUtils.normalizeUUID(uuid.toString()).replace("-", "").substring(0, 16));
2324
}
2425

2526
@Override

sentry/src/main/java/io/sentry/protocol/SentryId.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.sentry.JsonObjectReader;
66
import io.sentry.JsonObjectWriter;
77
import io.sentry.JsonSerializable;
8+
import io.sentry.util.StringUtils;
89
import java.io.IOException;
910
import java.util.UUID;
1011
import org.jetbrains.annotations.NotNull;
@@ -27,12 +28,12 @@ public SentryId(@Nullable UUID uuid) {
2728
}
2829

2930
public SentryId(final @NotNull String sentryIdString) {
30-
this.uuid = fromStringSentryId(sentryIdString);
31+
this.uuid = fromStringSentryId(StringUtils.normalizeUUID(sentryIdString));
3132
}
3233

3334
@Override
3435
public String toString() {
35-
return uuid.toString().replace("-", "");
36+
return StringUtils.normalizeUUID(uuid.toString()).replace("-", "");
3637
}
3738

3839
@Override

sentry/src/main/java/io/sentry/util/StringUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public final class StringUtils {
1818

1919
private static final Charset UTF_8 = Charset.forName("UTF-8");
2020

21+
private static final String CORRUPTED_NIL_UUID = "0000-0000";
22+
private static final String PROPER_NIL_UUID = "00000000-0000-0000-0000-000000000000";
23+
2124
private StringUtils() {}
2225

2326
public static @Nullable String getStringAfterDot(final @Nullable String str) {
@@ -138,4 +141,19 @@ public static int countOf(@NotNull String str, char character) {
138141
}
139142
return count;
140143
}
144+
145+
/**
146+
* Normalizes UUID string representation to adhere to the actual UUID standard
147+
*
148+
* <p>Because Motorola decided that nil UUIDs should look like this: "0000-0000" ;)
149+
*
150+
* @param uuidString the original UUID string representation
151+
* @return proper UUID string, in case it's a corrupted one
152+
*/
153+
public static String normalizeUUID(final @NotNull String uuidString) {
154+
if (uuidString.equals(CORRUPTED_NIL_UUID)) {
155+
return PROPER_NIL_UUID;
156+
}
157+
return uuidString;
158+
}
141159
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.sentry.protocol
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class SentryIdTest {
7+
8+
@Test
9+
fun `does not throw when instantiated with corrupted UUID`() {
10+
val id = SentryId("0000-0000")
11+
assertEquals("00000000000000000000000000000000", id.toString())
12+
}
13+
}

sentry/src/test/java/io/sentry/util/StringUtilsTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentry.util
22

33
import org.mockito.kotlin.mock
4+
import java.util.UUID
45
import kotlin.test.Test
56
import kotlin.test.assertEquals
67
import kotlin.test.assertNull
@@ -115,4 +116,17 @@ class StringUtilsTest {
115116

116117
assertNull(hashEmpty)
117118
}
119+
120+
@Test
121+
fun `returns proper nil UUID if the given string is corrupted`() {
122+
val normalized = StringUtils.normalizeUUID("0000-0000")
123+
assertEquals("00000000-0000-0000-0000-000000000000", normalized)
124+
}
125+
126+
@Test
127+
fun `returns the unchanged UUID if it was not corrupted`() {
128+
val original = UUID.randomUUID().toString()
129+
val normalized = StringUtils.normalizeUUID(original)
130+
assertEquals(original, normalized)
131+
}
118132
}

0 commit comments

Comments
 (0)