HttpPostStandardRequestDecoder leaks memory when constructor throws ErrorDataDecoderException. (#9517)

Motivation:

Currently when HttpPostStandardRequestDecoder throws a ErrorDataDecoderException during construction we leak memory. We need to ensure all is released correctly.

Modifications:

- Call destroy() if parseBody() throws and rethrow the ErrorDataDecoderException
- Add unit test

Result:

Fixes https://github.com/netty/netty/issues/9513.
This commit is contained in:
Norman Maurer 2019-08-28 10:27:38 +02:00 committed by GitHub
parent 491b1f428b
commit 95527eec91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -148,13 +148,18 @@ public class HttpPostStandardRequestDecoder implements InterfaceHttpPostRequestD
this.request = checkNotNull(request, "request");
this.charset = checkNotNull(charset, "charset");
this.factory = checkNotNull(factory, "factory");
if (request instanceof HttpContent) {
// Offer automatically if the given request is als type of HttpContent
// See #1089
offer((HttpContent) request);
} else {
undecodedChunk = buffer();
parseBody();
try {
if (request instanceof HttpContent) {
// Offer automatically if the given request is als type of HttpContent
// See #1089
offer((HttpContent) request);
} else {
undecodedChunk = buffer();
parseBody();
}
} catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
destroy();
throw e;
}
}

View File

@ -723,4 +723,15 @@ public class HttpPostRequestDecoderTest {
decoder.destroy();
assertEquals(1, req.refCnt());
}
@Test(expected = HttpPostRequestDecoder.ErrorDataDecoderException.class)
public void testNotLeak() {
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/",
Unpooled.copiedBuffer("a=1&&b=2", CharsetUtil.US_ASCII));
try {
new HttpPostStandardRequestDecoder(request);
} finally {
assertTrue(request.release());
}
}
}