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 e473c19e9d
commit bab50a9777
2 changed files with 31 additions and 11 deletions

View File

@ -276,8 +276,11 @@ public class Bzip2Decoder extends ByteToMessageDecoder {
if (!decoded) {
return;
}
int blockLength = blockDecompressor.blockLength();
ByteBuf uncompressed = ctx.alloc().buffer(blockLength);
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);
@ -287,6 +290,12 @@ public class Bzip2Decoder extends ByteToMessageDecoder {
streamCRC = (streamCRC << 1 | streamCRC >>> 31) ^ currentBlockCRC;
out.add(uncompressed);
success = true;
} finally {
if (!success) {
uncompressed.release();
}
}
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);
try {
channel.writeInbound(in);
} finally {
for (;;) {
ByteBuf inflated = channel.readInbound();
if (inflated == null) {
break;
}
inflated.release();
}
channel.finish();
}
}
@Test