Reject too-long IPs quicker

Now that there can't be a bunch of leading zeros, parsing can be
optimized a bit.
This commit is contained in:
Smitty 2021-07-09 12:54:02 -04:00
parent 69de693af9
commit b9b97bbb9d

View file

@ -289,7 +289,12 @@ impl FromStr for IpAddr {
impl FromStr for Ipv4Addr {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> {
Parser::new(s).parse_with(|p| p.read_ipv4_addr())
// don't try to parse if too long
if s.len() > 15 {
Err(AddrParseError(()))
} else {
Parser::new(s).parse_with(|p| p.read_ipv4_addr())
}
}
}