Skip to content

Commit ecb215c

Browse files
author
Norman Maurer
committed
Fix buffer leaks
1 parent 9dfad35 commit ecb215c

7 files changed

Lines changed: 27 additions & 9 deletions

File tree

codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
211211
private void cleanup() {
212212
if (decoder != null) {
213213
// Clean-up the previous decoder if not cleaned up correctly.
214-
finishDecode(Unpooled.buffer());
214+
ByteBuf buf = Unpooled.buffer();
215+
finishDecode(buf);
216+
buf.release();
215217
}
216218
}
217219

@@ -236,6 +238,7 @@ private void fetchDecoderOutput(ByteBuf out) {
236238
break;
237239
}
238240
out.writeBytes(buf);
241+
buf.release();
239242
}
240243
}
241244
}

codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,10 @@ public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
708708
HttpContent chunk = nextChunk();
709709
if (request instanceof FullHttpRequest) {
710710
FullHttpRequest fullRequest = (FullHttpRequest) request;
711-
if (!fullRequest.content().equals(chunk.content())) {
712-
fullRequest.content().clear().writeBytes(chunk.content());
711+
ByteBuf chunkContent = chunk.content();
712+
if (fullRequest.content() != chunkContent) {
713+
fullRequest.content().clear().writeBytes(chunkContent);
714+
chunkContent.release();
713715
}
714716
return fullRequest;
715717
} else {

codec/src/main/java/io/netty/handler/codec/string/StringEncoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ protected void encode(ChannelHandlerContext ctx, CharSequence msg, ByteBuf out)
7676
}
7777

7878
ByteBuf encoded = Unpooled.copiedBuffer(msg, charset);
79-
out.writeBytes(encoded);
79+
try {
80+
out.writeBytes(encoded);
81+
} finally {
82+
encoded.release();
83+
}
8084
}
8185
}

example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.netty.example.http.file;
1717

18+
import io.netty.buffer.ByteBuf;
1819
import io.netty.buffer.Unpooled;
1920
import io.netty.channel.ChannelFuture;
2021
import io.netty.channel.ChannelFutureListener;
@@ -272,8 +273,9 @@ private static void sendListing(ChannelHandlerContext ctx, File dir) {
272273
}
273274

274275
buf.append("</ul></body></html>\r\n");
275-
276-
response.content().writeBytes(Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8));
276+
ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8);
277+
response.content().writeBytes(buffer);
278+
buffer.release();
277279

278280
// Close the connection as soon as the error message is sent.
279281
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);

example/src/main/java/io/netty/example/http/websocketx/autobahn/AutobahnServerHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.netty.example.http.websocketx.autobahn;
1717

18+
import io.netty.buffer.ByteBuf;
1819
import io.netty.buffer.Unpooled;
1920
import io.netty.channel.ChannelFuture;
2021
import io.netty.channel.ChannelFutureListener;
@@ -122,7 +123,9 @@ private static void sendHttpResponse(
122123
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
123124
// Generate an error page if response getStatus code is not OK (200).
124125
if (res.getStatus().code() != 200) {
125-
res.content().writeBytes(Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
126+
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
127+
res.content().writeBytes(buf);
128+
buf.release();
126129
setContentLength(res, res.content().readableBytes());
127130
}
128131

example/src/main/java/io/netty/example/http/websocketx/server/WebSocketServerHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ private static void sendHttpResponse(
135135
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
136136
// Generate an error page if response getStatus code is not OK (200).
137137
if (res.getStatus().code() != 200) {
138-
res.content().writeBytes(Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
138+
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
139+
res.content().writeBytes(buf);
140+
buf.release();
139141
setContentLength(res, res.content().readableBytes());
140142
}
141143

example/src/main/java/io/netty/example/http/websocketx/sslserver/WebSocketSslServerHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ private static void sendHttpResponse(
132132
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
133133
// Generate an error page if response getStatus code is not OK (200).
134134
if (res.getStatus().code() != 200) {
135-
res.content().writeBytes(Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
135+
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
136+
res.content().writeBytes(buf);
137+
buf.release();
136138
setContentLength(res, res.content().readableBytes());
137139
}
138140

0 commit comments

Comments
 (0)