Ensure uncompressed ByteBuf is released when an exception happens during decoding.

Motivation:

We need to ensure the uncompressed ByteBuf is released if an exception happens while calling decode(...). If we miss to do so we leak buffers.

Modifications:

Correctly release buffer on exception.

Result:

No more memory leak.
This commit is contained in:
Norman Maurer 2016-07-20 06:35:05 +02:00
parent 94d7557dea
commit 7db3e01498

View File

@ -166,20 +166,27 @@ public class SnappyFrameDecoder extends ByteToMessageDecoder {
in.skipBytes(4);
int checksum = in.readIntLE();
ByteBuf uncompressed = ctx.alloc().buffer(0);
if (validateChecksums) {
int oldWriterIndex = in.writerIndex();
try {
in.writerIndex(in.readerIndex() + chunkLength - 4);
snappy.decode(in, uncompressed);
} finally {
in.writerIndex(oldWriterIndex);
ByteBuf uncompressed = ctx.alloc().buffer();
try {
if (validateChecksums) {
int oldWriterIndex = in.writerIndex();
try {
in.writerIndex(in.readerIndex() + chunkLength - 4);
snappy.decode(in, uncompressed);
} finally {
in.writerIndex(oldWriterIndex);
}
validateChecksum(checksum, uncompressed, 0, uncompressed.writerIndex());
} else {
snappy.decode(in.readSlice(chunkLength - 4), uncompressed);
}
out.add(uncompressed);
uncompressed = null;
} finally {
if (uncompressed != null) {
uncompressed.release();
}
validateChecksum(checksum, uncompressed, 0, uncompressed.writerIndex());
} else {
snappy.decode(in.readSlice(chunkLength - 4), uncompressed);
}
out.add(uncompressed);
snappy.reset();
break;
}