Fix buffer leaks in Bzip2Decoder(Test)

If decompression fails, the buffer that contains the decompressed data
is not released.  Bzip2DecoderTest.testStreamCrcError() also does not
release the partial output Bzip2Decoder produces.
This commit is contained in:
Trustin Lee 2014-06-26 17:45:41 +09:00
parent c0462c0c3b
commit 5f889d92a1
2 changed files with 31 additions and 11 deletions

View File

@ -276,17 +276,26 @@ public class Bzip2Decoder extends ByteToMessageDecoder {
if (!decoded) {
return;
}
int blockLength = blockDecompressor.blockLength();
ByteBuf uncompressed = ctx.alloc().buffer(blockLength);
int uncByte;
while ((uncByte = blockDecompressor.read()) >= 0) {
uncompressed.writeByte(uncByte);
final int blockLength = blockDecompressor.blockLength();
final ByteBuf uncompressed = ctx.alloc().buffer(blockLength);
boolean success = false;
try {
int uncByte;
while ((uncByte = blockDecompressor.read()) >= 0) {
uncompressed.writeByte(uncByte);
}
int currentBlockCRC = blockDecompressor.checkCRC();
streamCRC = (streamCRC << 1 | streamCRC >>> 31) ^ currentBlockCRC;
out.add(uncompressed);
success = true;
} finally {
if (!success) {
uncompressed.release();
}
}
int currentBlockCRC = blockDecompressor.checkCRC();
streamCRC = (streamCRC << 1 | streamCRC >>> 31) ^ currentBlockCRC;
out.add(uncompressed);
currentState = State.INIT_BLOCK;
break;
case EOF:

View File

@ -119,7 +119,18 @@ public class Bzip2DecoderTest {
0x4E, 0x14, 0x24, 0x1D, (byte) 0xDD, (byte) 0xF2, (byte) 0xB0, 0x00 };
ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in);
try {
channel.writeInbound(in);
} finally {
for (;;) {
ByteBuf inflated = channel.readInbound();
if (inflated == null) {
break;
}
inflated.release();
}
channel.finish();
}
}
@Test