Skip to content

Commit

Permalink
Fix IOOBE when converting empty bytes to a UTF8 String (#32)
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Bair <[email protected]>
  • Loading branch information
rbair23 authored Apr 11, 2023
1 parent dcb0e74 commit fe97298
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ default String asUtf8String(final long offset, final long len) {
throw new BufferUnderflowException();
}

if (offset < 0 || offset >= length()) {
if (offset < 0 || offset + len > length()) {
throw new IndexOutOfBoundsException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.hedera.pbj.runtime.io.ReadableSequentialData;
import com.hedera.pbj.runtime.io.ReadableTestBase;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.Assertions.assertThat;

public class BytesTest extends ReadableTestBase {

Expand All @@ -25,4 +29,11 @@ protected ReadableSequentialData fullyUsedSequence() {
protected ReadableSequentialData sequence(@NonNull final byte[] arr) {
return Bytes.wrap(arr).toReadableSequentialData();
}

@ParameterizedTest
@ValueSource(strings = { "", "a", "ab", "abc", "✅" })
void utf8Strings(final String s) {
final var buf = Bytes.wrap(s.getBytes(StandardCharsets.UTF_8));
assertThat(buf.asUtf8String()).isEqualTo(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import com.hedera.pbj.runtime.io.ReadableSequentialData;
import com.hedera.pbj.runtime.io.ReadableTestBase;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.Assertions.assertThat;

public class RandomAccessDataTest extends ReadableTestBase {

Expand All @@ -26,15 +31,22 @@ protected ReadableSequentialData sequence(@NonNull final byte[] arr) {
return new RandomAccessSequenceAdapter(new StubbedRandomAccessData(arr));
}

@ParameterizedTest
@ValueSource(strings = { "", "a", "ab", "abc", "✅" })
void utf8Strings(final String s) {
final var buf = new StubbedRandomAccessData(s.getBytes(StandardCharsets.UTF_8));
assertThat(buf.asUtf8String()).isEqualTo(s);
}

private record StubbedRandomAccessData(@NonNull byte[] bytes) implements RandomAccessData {
@Override
public long length() {
return bytes.length;
}

@Override
public byte getByte(long offset) {
return bytes[Math.toIntExact(offset)];
}
@Override
public long length() {
return bytes.length;
}

@Override
public byte getByte(long offset) {
return bytes[Math.toIntExact(offset)];
}
}
}

0 comments on commit fe97298

Please sign in to comment.