Skip to content

Commit

Permalink
fix: throw on not enough bytes (#288)
Browse files Browse the repository at this point in the history
Signed-off-by: Lazar Petrovic <lpetrovic05@gmail.com>
  • Loading branch information
lpetrovic05 authored Sep 18, 2024
1 parent bd40ceb commit aa12028
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ default long readBytes(@NonNull final BufferedData dst) throws UncheckedIOExcept
}

final var bytes = new byte[length];
readBytes(bytes, 0, length);
final long bytesRead = readBytes(bytes, 0, length);
if (bytesRead != length) {
throw new BufferUnderflowException();
}
return Bytes.wrap(bytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ void testReadBytes_incomplete() throws IOException {
final byte[] bytes = data.toInputStream().readAllBytes();
final byte[] incompleteCopy = new byte[bytes.length - 1];
System.arraycopy(bytes, 0, incompleteCopy, 0, bytes.length - 1);
final ReadableStreamingData streamingData = new ReadableStreamingData(new ByteArrayInputStream(incompleteCopy));
assertThrows(BufferUnderflowException.class, () -> ProtoParserTools.readString(streamingData));
assertThrows(BufferUnderflowException.class,
() -> ProtoParserTools.readString(new ReadableStreamingData(new ByteArrayInputStream(incompleteCopy))));
assertThrows(BufferUnderflowException.class,
() -> ProtoParserTools.readBytes(new ReadableStreamingData(new ByteArrayInputStream(incompleteCopy))));
}


Expand Down

0 comments on commit aa12028

Please sign in to comment.