DatagramDnsResponseDecoder should rethrow as CorruptedFrameException (#10714)

Motivation:

DatagramDnsResponseDecoder should rethrow as CorruptedFrameException if an IndexOutOfBoundsException happens.

Modifications:

- Catch IndexOutOfBoundsException and rethrow as CorruptedFrameException
- Add a testcase

Result:

Less noise in the logs
This commit is contained in:
Norman Maurer 2020-10-22 09:05:46 +02:00 committed by GitHub
parent ad8fe88abd
commit ee4164ab8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec.dns;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.internal.UnstableApi;
@ -55,7 +56,11 @@ public class DatagramDnsResponseDecoder extends MessageToMessageDecoder<Datagram
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
out.add(decodeResponse(ctx, packet));
try {
out.add(decodeResponse(ctx, packet));
} catch (IndexOutOfBoundsException e) {
throw new CorruptedFrameException("Unable to decode response", e);
}
}
protected DnsResponse decodeResponse(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {

View File

@ -29,6 +29,8 @@ import java.net.InetSocketAddress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
public class DnsResponseTest {
@ -90,6 +92,7 @@ public class DnsResponseTest {
envelope.release();
}
assertFalse(embedder.finish());
}
@Rule
@ -100,6 +103,22 @@ public class DnsResponseTest {
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
exception.expect(CorruptedFrameException.class);
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
try {
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
} finally {
assertFalse(embedder.finish());
}
}
@Test
public void readIncompleteResponseTest() {
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
ByteBuf packet = embedder.alloc().buffer(512);
exception.expect(CorruptedFrameException.class);
try {
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
} finally {
assertFalse(embedder.finish());
}
}
}