Reject empty TXT records in Txt::parse (#709)

RFC 1035 section 3.3.14 requires "one or more" character strings, so
rdlength=0 is invalid. `check_slice` (used by `from_octets` /
`from_slice`) already enforced this; `parse` did not, allowing
construction of a `Txt` that panics in `as_flat_slice`.

Replace the manual `CharStr::skip` loop with a single `check_slice`
call so all construction paths share the same validation.

The zero-length unknown-rdata test now uses TYPE65534 instead of TXT
since zero-length TXT is correctly invalid.
This commit is contained in:
SebastiaanYN
2026-09-28 16:14:04 +02:00
committed by GitHub
parent 576b2b33cd
commit 39ffa13d64
2 changed files with 10 additions and 7 deletions
+8 -5
View File
@@ -185,12 +185,9 @@ impl<Octs> Txt<Octs> {
Octs: AsRef<[u8]>,
{
let len = parser.remaining();
LongRecordData::check_len(len)?;
let text = parser.parse_octets(len)?;
let mut tmp = Parser::from_ref(text.as_ref());
while tmp.remaining() != 0 {
CharStr::skip(&mut tmp)?
}
Txt::check_slice(text.as_ref())
.map_err(|e| ParseError::Form(e.into()))?;
Ok(Txt(text))
}
@@ -1147,6 +1144,12 @@ mod test {
);
}
#[test]
fn txt_empty_rejected() {
let mut parser = Parser::from_static(b"");
assert!(Txt::<&[u8]>::parse(&mut parser).is_err());
}
#[test]
fn txt_display() {
fn cmp(input: &[u8], output: &str) {
+2 -2
View File
@@ -1,10 +1,10 @@
origin: example.com.
zonefile: |
example.com. 3600 IN TXT \# 0
example.com. 3600 IN TYPE65534 \# 0
result:
- owner: example.com.
class: IN
ttl: 3600
data: !Unknown
rtype: Txt
rtype: TYPE65534
data: