Skip to content

Commit

Permalink
Fix corrupted UUID on Motorola devices (#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn authored Nov 15, 2022
1 parent be45a71 commit 507f924
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Changelog

## 6.7.0
## Unreleased

### Fixes

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

### Features

Expand Down
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,7 @@ public final class io/sentry/util/StringUtils {
public static fun capitalize (Ljava/lang/String;)Ljava/lang/String;
public static fun countOf (Ljava/lang/String;C)I
public static fun getStringAfterDot (Ljava/lang/String;)Ljava/lang/String;
public static fun normalizeUUID (Ljava/lang/String;)Ljava/lang/String;
public static fun removeSurrounding (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
}

Expand Down
5 changes: 3 additions & 2 deletions sentry/src/main/java/io/sentry/SpanId.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.sentry;

import io.sentry.util.Objects;
import io.sentry.util.StringUtils;
import java.io.IOException;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;

public final class SpanId implements JsonSerializable {
public static final SpanId EMPTY_ID = new SpanId(new UUID(0, 0).toString());
public static final SpanId EMPTY_ID = new SpanId(new UUID(0, 0));

private final @NotNull String value;

Expand All @@ -19,7 +20,7 @@ public SpanId() {
}

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

@Override
Expand Down
5 changes: 3 additions & 2 deletions sentry/src/main/java/io/sentry/protocol/SentryId.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.sentry.JsonObjectReader;
import io.sentry.JsonObjectWriter;
import io.sentry.JsonSerializable;
import io.sentry.util.StringUtils;
import java.io.IOException;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
Expand All @@ -27,12 +28,12 @@ public SentryId(@Nullable UUID uuid) {
}

public SentryId(final @NotNull String sentryIdString) {
this.uuid = fromStringSentryId(sentryIdString);
this.uuid = fromStringSentryId(StringUtils.normalizeUUID(sentryIdString));
}

@Override
public String toString() {
return uuid.toString().replace("-", "");
return StringUtils.normalizeUUID(uuid.toString()).replace("-", "");
}

@Override
Expand Down
18 changes: 18 additions & 0 deletions sentry/src/main/java/io/sentry/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public final class StringUtils {

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

private static final String CORRUPTED_NIL_UUID = "0000-0000";
private static final String PROPER_NIL_UUID = "00000000-0000-0000-0000-000000000000";

private StringUtils() {}

public static @Nullable String getStringAfterDot(final @Nullable String str) {
Expand Down Expand Up @@ -138,4 +141,19 @@ public static int countOf(@NotNull String str, char character) {
}
return count;
}

/**
* Normalizes UUID string representation to adhere to the actual UUID standard
*
* <p>Because Motorola decided that nil UUIDs should look like this: "0000-0000" ;)
*
* @param uuidString the original UUID string representation
* @return proper UUID string, in case it's a corrupted one
*/
public static String normalizeUUID(final @NotNull String uuidString) {
if (uuidString.equals(CORRUPTED_NIL_UUID)) {
return PROPER_NIL_UUID;
}
return uuidString;
}
}
13 changes: 13 additions & 0 deletions sentry/src/test/java/io/sentry/protocol/SentryIdTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.sentry.protocol

import kotlin.test.Test
import kotlin.test.assertEquals

class SentryIdTest {

@Test
fun `does not throw when instantiated with corrupted UUID`() {
val id = SentryId("0000-0000")
assertEquals("00000000000000000000000000000000", id.toString())
}
}
14 changes: 14 additions & 0 deletions sentry/src/test/java/io/sentry/util/StringUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.util

import org.mockito.kotlin.mock
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
Expand Down Expand Up @@ -115,4 +116,17 @@ class StringUtilsTest {

assertNull(hashEmpty)
}

@Test
fun `returns proper nil UUID if the given string is corrupted`() {
val normalized = StringUtils.normalizeUUID("0000-0000")
assertEquals("00000000-0000-0000-0000-000000000000", normalized)
}

@Test
fun `returns the unchanged UUID if it was not corrupted`() {
val original = UUID.randomUUID().toString()
val normalized = StringUtils.normalizeUUID(original)
assertEquals(original, normalized)
}
}

0 comments on commit 507f924

Please sign in to comment.