Skip to content

Commit 55e96b3

Browse files
Do not throw exception if byte array too short for fromBytes (#2868)
* Do not throw exception if byte array too short for fromBytes Signed-off-by: Bogdan Drutu <[email protected]> * Update api/all/src/test/java/io/opentelemetry/api/trace/TraceIdTest.java Co-authored-by: Anuraag Agrawal <[email protected]>
1 parent 49f4bd0 commit 55e96b3

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

api/all/src/main/java/io/opentelemetry/api/internal/OtelEncodingUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public static byte[] bytesFromBase16(CharSequence value, int length) {
8383
}
8484

8585
/** Fills {@code dest} with the hex encoding of {@code bytes}. */
86-
public static void bytesToBase16(byte[] bytes, char[] dest) {
87-
for (int i = 0; i < bytes.length; i++) {
86+
public static void bytesToBase16(byte[] bytes, char[] dest, int length) {
87+
for (int i = 0; i < length; i++) {
8888
byteToBase16(bytes[i], dest, i * 2);
8989
}
9090
}

api/all/src/main/java/io/opentelemetry/api/trace/SpanId.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
public final class SpanId {
2424
private static final ThreadLocal<char[]> charBuffer = new ThreadLocal<>();
2525

26-
private static final int HEX_LENGTH = 16;
26+
private static final int BYTES_LENGTH = 8;
27+
private static final int HEX_LENGTH = 2 * BYTES_LENGTH;
2728
private static final String INVALID = "0000000000000000";
2829

2930
private SpanId() {}
@@ -62,18 +63,20 @@ public static boolean isValid(CharSequence spanId) {
6263

6364
/**
6465
* Returns the lowercase hex (base16) representation of the {@code SpanId} converted from the
65-
* given bytes representation, or {@link #getInvalid()} if input is {@code null}.
66+
* given bytes representation, or {@link #getInvalid()} if input is {@code null} or the given byte
67+
* array is too short.
68+
*
69+
* <p>It converts the first 8 bytes of the given byte array.
6670
*
6771
* @param spanIdBytes the bytes (8-byte array) representation of the {@code SpanId}.
6872
* @return the lowercase hex (base16) representation of the {@code SpanId}.
69-
* @throws IndexOutOfBoundsException if {@code spanIdBytes} too short.
7073
*/
7174
public static String fromBytes(byte[] spanIdBytes) {
72-
if (spanIdBytes == null) {
75+
if (spanIdBytes == null || spanIdBytes.length < BYTES_LENGTH) {
7376
return INVALID;
7477
}
7578
char[] result = getTemporaryBuffer();
76-
OtelEncodingUtils.bytesToBase16(spanIdBytes, result);
79+
OtelEncodingUtils.bytesToBase16(spanIdBytes, result, BYTES_LENGTH);
7780
return new String(result);
7881
}
7982

api/all/src/main/java/io/opentelemetry/api/trace/TraceId.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
public final class TraceId {
2828
private static final ThreadLocal<char[]> charBuffer = new ThreadLocal<>();
2929

30-
private static final int HEX_LENGTH = 32;
30+
private static final int BYTES_LENGTH = 16;
31+
private static final int HEX_LENGTH = 2 * BYTES_LENGTH;
3132
private static final String INVALID = "00000000000000000000000000000000";
3233

3334
private TraceId() {}
@@ -66,18 +67,20 @@ public static boolean isValid(CharSequence traceId) {
6667

6768
/**
6869
* Returns the lowercase hex (base16) representation of the {@code TraceId} converted from the
69-
* given bytes representation, or {@link #getInvalid()} if input is {@code null}.
70+
* given bytes representation, or {@link #getInvalid()} if input is {@code null} or the given byte
71+
* array is too short.
72+
*
73+
* <p>It converts the first 26 bytes of the given byte array.
7074
*
7175
* @param traceIdBytes the bytes (16-byte array) representation of the {@code TraceId}.
7276
* @return the lowercase hex (base16) representation of the {@code TraceId}.
73-
* @throws IndexOutOfBoundsException if {@code traceIdBytes} too short.
7477
*/
7578
public static String fromBytes(byte[] traceIdBytes) {
76-
if (traceIdBytes == null) {
79+
if (traceIdBytes == null || traceIdBytes.length < BYTES_LENGTH) {
7780
return INVALID;
7881
}
7982
char[] result = getTemporaryBuffer();
80-
OtelEncodingUtils.bytesToBase16(traceIdBytes, result);
83+
OtelEncodingUtils.bytesToBase16(traceIdBytes, result, BYTES_LENGTH);
8184
return new String(result);
8285
}
8386

api/all/src/test/java/io/opentelemetry/api/trace/SpanIdTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ void fromLong() {
4040

4141
@Test
4242
void fromBytes() {
43-
assertThat(SpanId.fromBytes(null)).isEqualTo(SpanId.getInvalid());
44-
4543
String spanId = "090a0b0c0d0e0f00";
4644
assertThat(SpanId.fromBytes(OtelEncodingUtils.bytesFromBase16(spanId, SpanId.getLength())))
4745
.isEqualTo(spanId);
4846
}
47+
48+
@Test
49+
void fromBytes_Invalid() {
50+
assertThat(SpanId.fromBytes(null)).isEqualTo(SpanId.getInvalid());
51+
assertThat(SpanId.fromBytes(new byte[] {0, 1, 2, 3, 4})).isEqualTo(SpanId.getInvalid());
52+
}
4953
}

api/all/src/test/java/io/opentelemetry/api/trace/TraceIdTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ void fromLongs() {
4242

4343
@Test
4444
void fromBytes() {
45-
assertThat(TraceId.fromBytes(null)).isEqualTo(TraceId.getInvalid());
46-
4745
String traceId = "0102030405060708090a0b0c0d0e0f00";
4846
assertThat(TraceId.fromBytes(OtelEncodingUtils.bytesFromBase16(traceId, TraceId.getLength())))
4947
.isEqualTo(traceId);
5048
}
49+
50+
@Test
51+
void fromBytes_Invalid() {
52+
assertThat(TraceId.fromBytes(null)).isEqualTo(TraceId.getInvalid());
53+
assertThat(TraceId.fromBytes(new byte[] {0, 1, 2, 3, 4})).isEqualTo(TraceId.getInvalid());
54+
}
5155
}

0 commit comments

Comments
 (0)