Fix buffer leaks in DnsResponseDecoder and DnsResponseDecoderTest

Motivation:

There were two buffer leaks in the codec-dns.

Modifications:

- Fix buffer leak in DnsResponseTest.readResponseTest()
- Correctly release DnsResources on Exception

Result:

No more buffer leaks in the codec-dns module.
This commit is contained in:
Norman Maurer 2014-08-04 14:05:02 +02:00
parent c13419750d
commit 74dd295e59
2 changed files with 27 additions and 9 deletions

View File

@ -64,6 +64,8 @@ public class DnsResponseDecoder extends MessageToMessageDecoder<DatagramPacket>
out.add(response); out.add(response);
return; return;
} }
boolean release = true;
try {
for (int i = 0; i < answers; i++) { for (int i = 0; i < answers; i++) {
response.addAnswer(decodeResource(buf)); response.addAnswer(decodeResource(buf));
} }
@ -74,6 +76,23 @@ public class DnsResponseDecoder extends MessageToMessageDecoder<DatagramPacket>
response.addAdditionalResource(decodeResource(buf)); response.addAdditionalResource(decodeResource(buf));
} }
out.add(response); out.add(response);
release = false;
} finally {
if (release) {
// We need to release te DnsResources in case of an Exception as we called retain() on the buffer.
releaseDnsResources(response.answers());
releaseDnsResources(response.authorityResources());
releaseDnsResources(response.additionalResources());
}
}
}
private static void releaseDnsResources(List<DnsResource> resources) {
int size = resources.size();
for (int i = 0; i < size; i++) {
DnsResource resource = resources.get(i);
resource.release();
}
} }
/** /**

View File

@ -74,7 +74,6 @@ public class DnsResponseTest {
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(p); ByteBuf packet = embedder.alloc().buffer(512).writeBytes(p);
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0))); embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
DnsResponse decoded = embedder.readInbound(); DnsResponse decoded = embedder.readInbound();
packet.retain().readerIndex(0);
ByteBuf raw = Unpooled.wrappedBuffer(p); ByteBuf raw = Unpooled.wrappedBuffer(p);
Assert.assertEquals("Invalid id, expected: " + raw.getUnsignedShort(0) + ", actual: " Assert.assertEquals("Invalid id, expected: " + raw.getUnsignedShort(0) + ", actual: "
+ decoded.header().id(), raw.getUnsignedShort(0), decoded.header().id()); + decoded.header().id(), raw.getUnsignedShort(0), decoded.header().id());