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
parent 97a9772fa2
commit 0f8e6a30ef
2 changed files with 27 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.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.internal.UnstableApi; import io.netty.util.internal.UnstableApi;
@ -54,7 +55,12 @@ public class DatagramDnsResponseDecoder extends MessageToMessageDecoder<Datagram
@Override @Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { protected void decode(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
DnsResponse response = decodeResponse(ctx, packet); final DnsResponse response;
try {
response = decodeResponse(ctx, packet);
} catch (IndexOutOfBoundsException e) {
throw new CorruptedFrameException("Unable to decode response", e);
}
ctx.fireChannelRead(response); ctx.fireChannelRead(response);
} }

View File

@ -29,6 +29,8 @@ import java.net.InetSocketAddress;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
public class DnsResponseTest { public class DnsResponseTest {
@ -90,6 +92,7 @@ public class DnsResponseTest {
envelope.release(); envelope.release();
} }
assertFalse(embedder.finish());
} }
@Rule @Rule
@ -100,6 +103,22 @@ public class DnsResponseTest {
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder()); EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket); ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
exception.expect(CorruptedFrameException.class); 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());
}
} }
} }