From e435b5ea6fa84d8aff2cd40b358382daf6939f3c Mon Sep 17 00:00:00 2001 From: Yilin Chen Date: Thu, 5 Sep 2019 11:23:15 +0800 Subject: [PATCH] Check domain length Signed-off-by: Yilin Chen --- src/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a180f9..cfebd73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,8 +168,7 @@ impl<'a> IntoTargetAddr<'a> for (&'a str, u16) { } // Treat as domain name - let len = self.0.as_bytes().len(); - if len > 255 { + if self.0.len() > 255 { return Err(Error::InvalidTargetAddress("overlong domain")); } // TODO: Should we validate the domain format here? @@ -193,6 +192,9 @@ impl<'a> IntoTargetAddr<'a> for &'a str { let domain = parts_iter .next() .ok_or(Error::InvalidTargetAddress("invalid address format"))?; + if domain.len() > 255 { + return Err(Error::InvalidTargetAddress("overlong domain")); + } Ok(TargetAddr::Domain(domain.into(), port)) } } @@ -213,6 +215,9 @@ impl IntoTargetAddr<'static> for String { .next() .ok_or(Error::InvalidTargetAddress("invalid address format"))? .len(); + if domain_len > 255 { + return Err(Error::InvalidTargetAddress("overlong domain")); + } self.truncate(domain_len); Ok(TargetAddr::Domain(self.into(), port)) }