[#5190] Ensure memory is not leaked when readEndOfLine(...) throws an exception.

Motivation:

We need to ensure we not retain the buffer until readEndOfLine(...) completes as otherwise we may leak memory in the case of an exception.

Modifications:

Only call retain after readEndOfLine(...) returns.

Result:

No more leak in case of exception while decoding redis messages.
This commit is contained in:
Norman Maurer 2016-05-03 10:32:25 +02:00
parent ef13d19b8b
commit 06436efaa1

View File

@ -205,9 +205,10 @@ public final class RedisDecoder extends ByteToMessageDecoder {
// if this is last frame. // if this is last frame.
if (readableBytes >= remainingBulkLength + RedisConstants.EOL_LENGTH) { if (readableBytes >= remainingBulkLength + RedisConstants.EOL_LENGTH) {
ByteBuf content = in.readSlice(remainingBulkLength).retain(); ByteBuf content = in.readSlice(remainingBulkLength);
readEndOfLine(in); readEndOfLine(in);
out.add(new DefaultLastBulkStringRedisContent(content)); // Only call retain after readEndOfLine(...) as the method may throw an exception.
out.add(new DefaultLastBulkStringRedisContent(content.retain()));
resetDecoder(); resetDecoder();
return true; return true;
} }