Skip to content

Commit

Permalink
Do not parse an empty string as an IpAddress.
Browse files Browse the repository at this point in the history
This is more likely to result in downstream confusion than not.
Let downstream code decide exactly what it wants to do with
an empty string; be conservative here.
whitequark committed Oct 30, 2017
1 parent 7a2271d commit 1e10525
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/parsers.rs
Original file line number Diff line number Diff line change
@@ -127,9 +127,6 @@ impl<'a> Parser<'a> {
}

fn accept_ip(&mut self) -> Result<IpAddress> {
if let Some(()) = self.try(|p| p.accept_eof()) {
return Ok(IpAddress::Unspecified)
}
if let Some(ipv4) = self.try(|p| p.accept_ipv4()) {
return Ok(IpAddress::Ipv4(ipv4))
}
@@ -158,7 +155,7 @@ impl FromStr for Ipv4Address {
impl FromStr for IpAddress {
type Err = ();

/// Parse a string representation of an IPv4 address.
/// Parse a string representation of an IP address.
fn from_str(s: &str) -> Result<IpAddress> {
Parser::new(s).until_eof(|p| p.accept_ip())
}
@@ -229,8 +226,7 @@ mod test {

#[test]
fn test_ip() {
assert_eq!(IpAddress::from_str(""),
Ok(IpAddress::Unspecified));
assert_eq!(IpAddress::from_str(""), Err(()));
assert_eq!(IpAddress::from_str("1.2.3.4"),
Ok(IpAddress::Ipv4(Ipv4Address([1, 2, 3, 4]))));
assert_eq!(IpAddress::from_str("x"), Err(()));

0 comments on commit 1e10525

Please sign in to comment.