Skip to content

Commit a446cb0

Browse files
committed
fix: guard against the ArrayIndexOutOfBoundsException in BaggageCodec class
1 parent e34049b commit a446cb0

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

api/all/src/main/java/io/opentelemetry/api/baggage/propagation/BaggageCodec.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ private static byte[] decode(byte[] bytes) {
4444
for (int i = 0; i < bytes.length; i++) {
4545
int b = bytes[i];
4646
if (b == ESCAPE_CHAR) {
47-
try {
48-
int u = digit16(bytes[++i]);
49-
int l = digit16(bytes[++i]);
50-
buffer.write((char) ((u << 4) + l));
51-
} catch (ArrayIndexOutOfBoundsException e) { // FIXME
52-
throw new IllegalArgumentException("Invalid URL encoding: ", e);
47+
if (i + 1 >= bytes.length) {
48+
return buffer.toByteArray();
5349
}
50+
int u = digit16(bytes[++i]);
51+
52+
if (i + 1 >= bytes.length) {
53+
return buffer.toByteArray();
54+
}
55+
int l = digit16(bytes[++i]);
56+
57+
buffer.write((char) ((u << 4) + l));
5458
} else {
5559
buffer.write(b);
5660
}

api/all/src/test/java/io/opentelemetry/api/baggage/propagation/BaggageCodecTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package io.opentelemetry.api.baggage.propagation;
77

88
import static org.assertj.core.api.Assertions.assertThat;
9-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
9+
import static org.assertj.core.api.Assertions.assertThatNoException;
1010

1111
import java.nio.charset.StandardCharsets;
1212
import org.junit.jupiter.api.Test;
@@ -36,7 +36,6 @@ void shouldDecodePercentEncodedValues(String percentEncoded, String expectedDeco
3636

3737
@Test
3838
void shouldThrowIfMalformedData() {
39-
assertThatThrownBy(() -> BaggageCodec.decode("%1", StandardCharsets.UTF_8))
40-
.isInstanceOf(IllegalArgumentException.class);
39+
assertThatNoException().isThrownBy(() -> BaggageCodec.decode("%1", StandardCharsets.UTF_8));
4140
}
4241
}

0 commit comments

Comments
 (0)