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:
Trustin Lee 2014-07-21 16:48:08 -07:00
parent ae9963a40c
commit 923a0e71ac
2 changed files with 4 additions and 5 deletions

View File

@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf;
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.CharsetUtil;
@ -99,7 +100,7 @@ public class DnsResponseDecoder extends MessageToMessageDecoder<DatagramPacket>
// check for loops
checked += 2;
if (checked >= length) {
return null;
throw new CorruptedFrameException("name contains a loop.");
}
} else {
name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append('.');

View File

@ -19,8 +19,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
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.Rule;
import org.junit.Test;
@ -100,8 +99,7 @@ public class DnsResponseTest {
public void readMalormedResponseTest() throws Exception {
EmbeddedChannel embedder = new EmbeddedChannel(new DnsResponseDecoder());
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
exception.expect(DecoderException.class);
exception.expectMessage("java.lang.NullPointerException: name");
exception.expect(CorruptedFrameException.class);
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
}
}