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:
parent
c13419750d
commit
74dd295e59
@ -64,16 +64,35 @@ public class DnsResponseDecoder extends MessageToMessageDecoder<DatagramPacket>
|
||||
out.add(response);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < answers; i++) {
|
||||
response.addAnswer(decodeResource(buf));
|
||||
boolean release = true;
|
||||
try {
|
||||
for (int i = 0; i < answers; i++) {
|
||||
response.addAnswer(decodeResource(buf));
|
||||
}
|
||||
for (int i = 0; i < authorities; i++) {
|
||||
response.addAuthorityResource(decodeResource(buf));
|
||||
}
|
||||
for (int i = 0; i < additionals; i++) {
|
||||
response.addAdditionalResource(decodeResource(buf));
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < authorities; i++) {
|
||||
response.addAuthorityResource(decodeResource(buf));
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
for (int i = 0; i < additionals; i++) {
|
||||
response.addAdditionalResource(decodeResource(buf));
|
||||
}
|
||||
out.add(response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,7 +74,6 @@ public class DnsResponseTest {
|
||||
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(p);
|
||||
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
|
||||
DnsResponse decoded = embedder.readInbound();
|
||||
packet.retain().readerIndex(0);
|
||||
ByteBuf raw = Unpooled.wrappedBuffer(p);
|
||||
Assert.assertEquals("Invalid id, expected: " + raw.getUnsignedShort(0) + ", actual: "
|
||||
+ decoded.header().id(), raw.getUnsignedShort(0), decoded.header().id());
|
||||
|
Loading…
Reference in New Issue
Block a user