Skip to content

Commit f9c9e38

Browse files
committed
ensure that errors in the char->digit conversion is also handled
1 parent 35c0d7a commit f9c9e38

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/internal/tombstone/OleGuidFormatter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* c0bcc3f1-9827-fe65-3058-404b2831d9e6
2121
* </pre>
2222
*
23-
* Note: Java bytes are signed. When promoted (e.g. during formatting or bit shifts), they
23+
* <p>Note: Java bytes are signed. When promoted (e.g. during formatting or bit shifts), they
2424
* sign-extend to int, unlike uint8_t in C. We therefore mask with & 0xff to preserve the intended
2525
* unsigned byte values.
2626
*/
@@ -78,6 +78,10 @@ private static byte[] hexToBytes(String hex) {
7878
for (int byteIdx = 0; byteIdx < numBytes; byteIdx++) {
7979
int hi = Character.digit(hex.charAt(byteIdx * 2), numBytes);
8080
int lo = Character.digit(hex.charAt(byteIdx * 2 + 1), numBytes);
81+
if (hi < 0 || lo < 0) {
82+
throw new IllegalArgumentException(
83+
"GUID conversion input hex string contains invalid characters");
84+
}
8185
result[byteIdx] = (byte) ((hi << 4) | lo);
8286
}
8387
return result;

sentry-android-core/src/test/java/io/sentry/android/core/internal/tombstone/OleGuidFormatterTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ class OleGuidFormatterTest {
2424
}
2525
}
2626

27+
@Test
28+
fun `an input string with with an invalid hex character throws`() {
29+
// fail the low nibble conversion
30+
assertFailsWith<IllegalArgumentException> {
31+
OleGuidFormatter.convert("g123456789abcdef0123456789abcdef")
32+
}
33+
34+
// fail the high nibble conversion
35+
assertFailsWith<IllegalArgumentException> {
36+
OleGuidFormatter.convert("0h23456789abcdef0123456789abcdef")
37+
}
38+
}
39+
2740
@Test
2841
fun `an input example from the develop docs leads to the expected result`() {
2942
val input = "f1c3bcc0279865fe3058404b2831d9e64135386c"

0 commit comments

Comments
 (0)