Fix Buffer Overflow During Lz4FrameEncoder Close (#11429)

Motivation:

We failed to account for the last header when estimating the buffer
size. If the data does not compress enough to make space for the
last header we would exceed the ByteBuf's capacity.

Modifications:

Call #ensureWritable with appropriate capacity for footer ByteBuf
befor writing footer.

Result:

If there is not enough space left in the buffer, the buffer will be
expanded.
This commit is contained in:
Tamara Braun 2021-06-30 18:47:07 +02:00 committed by Norman Maurer
parent fe1ed36cc4
commit 3afece1e13

View File

@ -317,6 +317,7 @@ public class Lz4FrameEncoder extends MessageToByteEncoder<ByteBuf> {
compressor.maxCompressedLength(buffer.readableBytes()) + HEADER_LENGTH); compressor.maxCompressedLength(buffer.readableBytes()) + HEADER_LENGTH);
flushBufferedData(footer); flushBufferedData(footer);
footer.ensureWritable(HEADER_LENGTH);
final int idx = footer.writerIndex(); final int idx = footer.writerIndex();
footer.setLong(idx, MAGIC_NUMBER); footer.setLong(idx, MAGIC_NUMBER);
footer.setByte(idx + TOKEN_OFFSET, (byte) (BLOCK_TYPE_NON_COMPRESSED | compressionLevel)); footer.setByte(idx + TOKEN_OFFSET, (byte) (BLOCK_TYPE_NON_COMPRESSED | compressionLevel));