Skip to content

Commit

Permalink
ipv6 cannot start with ipv4
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Nov 24, 2024
1 parent ba44b34 commit 4116617
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/iri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn parse_ip_v6(s: &str) -> ParserResult<Vec<u16>> {
fn segment(s: &str) -> ParserResult<Segment> {
alt((
map(tag("::"), |_| Segment::Compressed),
preceded(tag(":"), map(parse_ip_v4, Segment::IpV4)),
preceded(opt(tag(":")), map(parse_ip_v4, Segment::IpV4)),
preceded(opt(tag(":")), map(hextet, Segment::Hextet)),
))(s)
}
Expand All @@ -54,6 +54,10 @@ fn parse_ip_v6(s: &str) -> ParserResult<Vec<u16>> {
Segment::Compressed => {
compression_pos = Some(idx);
}
Segment::IpV4(_) if idx == 0 => {
let err = VerboseError::from_error_kind(s, ErrorKind::IsNot);
return Err(nom::Err::Error(err));
}
Segment::IpV4(l) => {
ipv6.push((l[0] as u16) << 8 | l[1] as u16);
ipv6.push((l[2] as u16) << 8 | l[3] as u16);
Expand Down Expand Up @@ -183,5 +187,7 @@ mod test {
let result = parse_ip_v6(addr).unwrap();
assert_eq!(result, ("", expected),);
}

assert!(parse_ip_v6("192.168.1.1:0::ffff").is_err());
}
}

0 comments on commit 4116617

Please sign in to comment.