Skip to content

Commit 3519eff

Browse files
committed
Auto merge of #29724 - alexcrichton:ip-endian, r=aturon
The comparison of IP addresses should happen not always in network endianness but rather in the host endianness format, so be sure to convert to that before comparing addresses. There are still locations where the endianness will factor into visible properties, such as the hash, but these are not important to be independent of the endianness in play (as hash values are pretty undefined anyway. Closes #29691
2 parents d668fab + f16e7b4 commit 3519eff

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/libstd/net/ip.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl PartialOrd for Ipv4Addr {
236236
#[stable(feature = "rust1", since = "1.0.0")]
237237
impl Ord for Ipv4Addr {
238238
fn cmp(&self, other: &Ipv4Addr) -> Ordering {
239-
self.inner.s_addr.cmp(&other.inner.s_addr)
239+
self.octets().cmp(&other.octets())
240240
}
241241
}
242242

@@ -506,7 +506,7 @@ impl PartialOrd for Ipv6Addr {
506506
#[stable(feature = "rust1", since = "1.0.0")]
507507
impl Ord for Ipv6Addr {
508508
fn cmp(&self, other: &Ipv6Addr) -> Ordering {
509-
self.inner.s6_addr.cmp(&other.inner.s6_addr)
509+
self.segments().cmp(&other.segments())
510510
}
511511
}
512512

@@ -794,4 +794,11 @@ mod tests {
794794
let a = Ipv4Addr::new(127, 0, 0, 1);
795795
assert_eq!(Ipv4Addr::from(2130706433), a);
796796
}
797+
798+
#[test]
799+
fn ord() {
800+
assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));
801+
assert!("2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap() <
802+
"2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap());
803+
}
797804
}

0 commit comments

Comments
 (0)