Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix corrupted UUID on Motorola devices #2363

Merged
merged 2 commits into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -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
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
@@ -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
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
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
@@ -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
@@ -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)
}
}