Skip to content

Commit

Permalink
Guard ToByteBufEncoder calls with try/finally #930
Browse files Browse the repository at this point in the history
Lettuce now makes sure to properly release temporary buffers after encoding keys and values. This avoids memory leaks caused by encoding errors.
  • Loading branch information
mp911de committed Nov 24, 2018
1 parent 70d82c0 commit c705fd4
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/io/lettuce/core/protocol/CommandArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,14 @@ void encode(ByteBuf target) {

ToByteBufEncoder<K, V> toByteBufEncoder = (ToByteBufEncoder<K, V>) codec;
ByteBuf temporaryBuffer = target.alloc().buffer(toByteBufEncoder.estimateSize(key) + 6);
toByteBufEncoder.encodeKey(key, temporaryBuffer);

ByteBufferArgument.writeByteBuf(target, temporaryBuffer);
temporaryBuffer.release();
try {

toByteBufEncoder.encodeKey(key, temporaryBuffer);
ByteBufferArgument.writeByteBuf(target, temporaryBuffer);
} finally {
temporaryBuffer.release();
}

return;
}
Expand Down Expand Up @@ -693,10 +697,13 @@ void encode(ByteBuf target) {

ToByteBufEncoder<K, V> toByteBufEncoder = (ToByteBufEncoder<K, V>) codec;
ByteBuf temporaryBuffer = target.alloc().buffer(toByteBufEncoder.estimateSize(val) + 6);
toByteBufEncoder.encodeValue(val, temporaryBuffer);

ByteBufferArgument.writeByteBuf(target, temporaryBuffer);
temporaryBuffer.release();
try {
toByteBufEncoder.encodeValue(val, temporaryBuffer);
ByteBufferArgument.writeByteBuf(target, temporaryBuffer);
} finally {
temporaryBuffer.release();
}

return;
}
Expand Down

0 comments on commit c705fd4

Please sign in to comment.