Skip to content

Commit f459f80

Browse files
authored
fix: add guard against the ArrayIndexOutOfBoundsException in BaggageCodec… (#7239)
1 parent 21c1b02 commit f459f80

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

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

Lines changed: 10 additions & 6 deletions
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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
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;
109

1110
import java.nio.charset.StandardCharsets;
1211
import org.junit.jupiter.api.Test;
@@ -35,8 +34,8 @@ void shouldDecodePercentEncodedValues(String percentEncoded, String expectedDeco
3534
}
3635

3736
@Test
38-
void shouldThrowIfMalformedData() {
39-
assertThatThrownBy(() -> BaggageCodec.decode("%1", StandardCharsets.UTF_8))
40-
.isInstanceOf(IllegalArgumentException.class);
37+
void shouldIgnoreIfMalformedData() {
38+
assertThat(BaggageCodec.decode("%", StandardCharsets.UTF_8)).isEqualTo("");
39+
assertThat(BaggageCodec.decode("%1", StandardCharsets.UTF_8)).isEqualTo("");
4140
}
4241
}

0 commit comments

Comments
 (0)