Skip to content

fix: add guard against the ArrayIndexOutOfBoundsException in BaggageCodec… #7239

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ private static byte[] decode(byte[] bytes) {
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b == ESCAPE_CHAR) {
try {
int u = digit16(bytes[++i]);
int l = digit16(bytes[++i]);
buffer.write((char) ((u << 4) + l));
} catch (ArrayIndexOutOfBoundsException e) { // FIXME
throw new IllegalArgumentException("Invalid URL encoding: ", e);
if (i + 1 >= bytes.length) {
return buffer.toByteArray();
}
int u = digit16(bytes[++i]);

if (i + 1 >= bytes.length) {
return buffer.toByteArray();
}
int l = digit16(bytes[++i]);

buffer.write((char) ((u << 4) + l));
} else {
buffer.write(b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.api.baggage.propagation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -35,8 +34,8 @@ void shouldDecodePercentEncodedValues(String percentEncoded, String expectedDeco
}

@Test
void shouldThrowIfMalformedData() {
assertThatThrownBy(() -> BaggageCodec.decode("%1", StandardCharsets.UTF_8))
.isInstanceOf(IllegalArgumentException.class);
void shouldIgnoreIfMalformedData() {
assertThat(BaggageCodec.decode("%", StandardCharsets.UTF_8)).isEqualTo("");
assertThat(BaggageCodec.decode("%1", StandardCharsets.UTF_8)).isEqualTo("");
}
}
Loading