[#5760] Do not change writerIndex when decode DnsPtrRecord

Motivation:

We need to not change the original writerIndex when decode a DnsPtrRecord as otherwise we will not be able to decode other records that follow it.

Modifications:

Slice the data out and so not increase the writerIndex.

Result:

No more problems when decoding.
This commit is contained in:
Norman Maurer 2016-08-31 15:17:21 +02:00
parent 463b5cf21b
commit 8cf90f0512
2 changed files with 25 additions and 6 deletions

View File

@ -91,11 +91,10 @@ public class DefaultDnsRecordDecoder implements DnsRecordDecoder {
ByteBuf in, int offset, int length) throws Exception {
if (type == DnsRecordType.PTR) {
in.setIndex(offset, offset + length);
return new DefaultDnsPtrRecord(name, dnsClass, timeToLive, decodeName0(in));
return new DefaultDnsPtrRecord(name, dnsClass, timeToLive, decodeName0(in.slice(offset, length)));
}
return new DefaultDnsRawRecord(
name, type, dnsClass, timeToLive, in.retainedDuplicate().setIndex(offset, offset + length));
name, type, dnsClass, timeToLive, in.retainedSlice(offset, length));
}
/**

View File

@ -17,10 +17,10 @@ package io.netty.handler.codec.dns;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.StringUtil;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DefaultDnsRecordDecoderTest {
@Test
@ -63,7 +63,27 @@ public class DefaultDnsRecordDecoderTest {
private static void testDecodeName(String expected, ByteBuf buffer) {
try {
DefaultDnsRecordDecoder decoder = new DefaultDnsRecordDecoder();
Assert.assertEquals(expected, decoder.decodeName(buffer));
assertEquals(expected, decoder.decodeName0(buffer));
} finally {
buffer.release();
}
}
@Test
public void testDecodePtrRecord() throws Exception {
DefaultDnsRecordDecoder decoder = new DefaultDnsRecordDecoder();
ByteBuf buffer = Unpooled.buffer().writeByte(0);
int readerIndex = buffer.readerIndex();
int writerIndex = buffer.writerIndex();
try {
DnsPtrRecord record = (DnsPtrRecord) decoder.decodeRecord(
"netty.io", DnsRecordType.PTR, DnsRecord.CLASS_IN, 60, buffer, 0, 1);
assertEquals("netty.io.", record.name());
assertEquals(DnsRecord.CLASS_IN, record.dnsClass());
assertEquals(60, record.timeToLive());
assertEquals(DnsRecordType.PTR, record.type());
assertEquals(readerIndex, buffer.readerIndex());
assertEquals(writerIndex, buffer.writerIndex());
} finally {
buffer.release();
}