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:
parent
97a9772fa2
commit
0f8e6a30ef
@ -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;
|
||||
|
||||
@ -54,7 +55,12 @@ public class DatagramDnsResponseDecoder extends MessageToMessageDecoder<Datagram
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user