From cb4812a5c58cdbcb281efc03b3591d3b8c1d48a5 Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Sun, 28 Jul 2024 18:23:31 +0200 Subject: [PATCH] fix wrong condition check --- src/parser.rs | 68 ++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index cd58b80..cae9a07 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -158,32 +158,25 @@ impl ToBytes for LabelString { impl FromBytes for Question { fn from_bytes(reader: &mut Reader) -> Result { // 16 for length octet + zero length octet - if reader.unread_bytes() < 2 + size_of::() + size_of::() { + let qname = LabelString::from_bytes(reader)?; + + if reader.unread_bytes() < 4 { Err(ZNSError::Parse { object: String::from("Question"), - message: String::from("len of bytes smaller then minimum size"), + message: String::from("len of rest bytes smaller then minimum size"), }) } else { - let qname = LabelString::from_bytes(reader)?; + //Try Parse qtype + let qtype = Type::from(reader.read_u16()?); - if reader.unread_bytes() < 4 { - Err(ZNSError::Parse { - object: String::from("Question"), - message: String::from("len of rest bytes smaller then minimum size"), - }) - } else { - //Try Parse qtype - let qtype = Type::from(reader.read_u16()?); + //Try Parse qclass + let qclass = Class::from(reader.read_u16()?); - //Try Parse qclass - let qclass = Class::from(reader.read_u16()?); - - Ok(Question { - qname, - qtype, - qclass, - }) - } + Ok(Question { + qname, + qtype, + qclass, + }) } } } @@ -200,31 +193,24 @@ impl ToBytes for Question { impl FromBytes for RR { fn from_bytes(reader: &mut Reader) -> Result { let name = LabelString::from_bytes(reader)?; - if reader.unread_bytes() < size_of::() + size_of::() + 6 { + let _type = Type::from(reader.read_u16()?); + let class = Class::from(reader.read_u16()?); + let ttl = reader.read_i32()?; + let rdlength = reader.read_u16()?; + if reader.unread_bytes() < rdlength as usize { Err(ZNSError::Parse { object: String::from("RR"), - message: String::from("len of rest of bytes smaller then minimum size"), + message: String::from("len of rest of bytes not equal to rdlength"), }) } else { - let _type = Type::from(reader.read_u16()?); - let class = Class::from(reader.read_u16()?); - let ttl = reader.read_i32()?; - let rdlength = reader.read_u16()?; - if reader.unread_bytes() < rdlength as usize { - Err(ZNSError::Parse { - object: String::from("RR"), - message: String::from("len of rest of bytes not equal to rdlength"), - }) - } else { - Ok(RR { - name, - _type, - class, - ttl, - rdlength, - rdata: reader.read(rdlength as usize)?, - }) - } + Ok(RR { + name, + _type, + class, + ttl, + rdlength, + rdata: reader.read(rdlength as usize)?, + }) } } }