From 14db4fa830db8ac7157525aae588a38b2d8cca11 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Sat, 24 Nov 2018 13:27:48 +0100 Subject: [PATCH] Guard ToByteBufEncoder calls with try/finally #930 Lettuce now makes sure to properly release temporary buffers after encoding keys and values. This avoids memory leaks caused by encoding errors. --- .../io/lettuce/core/protocol/CommandArgs.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/lettuce/core/protocol/CommandArgs.java b/src/main/java/io/lettuce/core/protocol/CommandArgs.java index 614c0309f0..035d2d4e7f 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandArgs.java +++ b/src/main/java/io/lettuce/core/protocol/CommandArgs.java @@ -654,10 +654,14 @@ void encode(ByteBuf target) { ToByteBufEncoder toByteBufEncoder = (ToByteBufEncoder) 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; } @@ -693,10 +697,13 @@ void encode(ByteBuf target) { ToByteBufEncoder toByteBufEncoder = (ToByteBufEncoder) 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; }