Raise a meaningful exception instead of NPE
Motivation: When decoding the NAME field in a DNS Resource Record, DnsResponseDecoder can raise a NullPointerException if the NAME field contains a loop. Modification: Instead of raising an NPE, raise CorruptedFrameException so that the exception itself has meaning. Result: Less confusing when a malformed DNS RR is received
This commit is contained in:
parent
0983166699
commit
eab9bcb9d3
@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf;
|
|||||||
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.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
|
|
||||||
@ -99,7 +100,7 @@ public class DnsResponseDecoder extends MessageToMessageDecoder<DatagramPacket>
|
|||||||
// check for loops
|
// check for loops
|
||||||
checked += 2;
|
checked += 2;
|
||||||
if (checked >= length) {
|
if (checked >= length) {
|
||||||
return null;
|
throw new CorruptedFrameException("name contains a loop.");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append('.');
|
name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append('.');
|
||||||
|
@ -19,8 +19,7 @@ import io.netty.buffer.ByteBuf;
|
|||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import io.netty.channel.embedded.EmbeddedChannel;
|
import io.netty.channel.embedded.EmbeddedChannel;
|
||||||
import io.netty.channel.socket.DatagramPacket;
|
import io.netty.channel.socket.DatagramPacket;
|
||||||
import io.netty.handler.codec.DecoderException;
|
import io.netty.handler.codec.CorruptedFrameException;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -100,8 +99,7 @@ public class DnsResponseTest {
|
|||||||
public void readMalormedResponseTest() throws Exception {
|
public void readMalormedResponseTest() throws Exception {
|
||||||
EmbeddedChannel embedder = new EmbeddedChannel(new DnsResponseDecoder());
|
EmbeddedChannel embedder = new EmbeddedChannel(new DnsResponseDecoder());
|
||||||
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
|
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
|
||||||
exception.expect(DecoderException.class);
|
exception.expect(CorruptedFrameException.class);
|
||||||
exception.expectMessage("java.lang.NullPointerException: name");
|
|
||||||
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
|
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user