diff options
author | Alan Somers <asomers@gmail.com> | 2023-06-27 17:30:35 -0600 |
---|---|---|
committer | Alan Somers <asomers@gmail.com> | 2023-06-27 21:26:18 -0600 |
commit | 1546857f8c6a8ccd444eb656f4421003a376f5cd (patch) | |
tree | 05b9dad5c01c4f752a8913a3b007a638071ee0f1 /src/sys | |
parent | 9a3010b62bdd61aa7d5d221af2ea3d72868dd59e (diff) | |
download | nix-1546857f8c6a8ccd444eb656f4421003a376f5cd.zip |
For invalid IP address conversions with future Rust versions
Rust's standard library no longer guarantees that Ipv4Addr and Ipv6Addr
are wrappers around the C types (though for now at least, they are
identical on all platforms I'm aware of). So do the conversions
explicitly instead of transmuting.
Fixes #2053
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/socket/addr.rs | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 48309749..2b3a1ed4 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -39,18 +39,17 @@ use std::{fmt, mem, net, ptr, slice}; /// Convert a std::net::Ipv4Addr into the libc form. #[cfg(feature = "net")] pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr { - static_assertions::assert_eq_size!(net::Ipv4Addr, libc::in_addr); - // Safe because both types have the same memory layout, and no fancy Drop - // impls. - unsafe { mem::transmute(addr) } + libc::in_addr { + s_addr: u32::from_ne_bytes(addr.octets()) + } } /// Convert a std::net::Ipv6Addr into the libc form. #[cfg(feature = "net")] pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> libc::in6_addr { - static_assertions::assert_eq_size!(net::Ipv6Addr, libc::in6_addr); - // Safe because both are Newtype wrappers around the same libc type - unsafe { mem::transmute(*addr) } + libc::in6_addr { + s6_addr: addr.octets() + } } /// These constants specify the protocol family to be used @@ -949,9 +948,6 @@ impl SockaddrLike for () { } /// An IPv4 socket address -// This is identical to net::SocketAddrV4. But the standard library -// doesn't allow direct access to the libc fields, which we need. So we -// reimplement it here. #[cfg(feature = "net")] #[repr(transparent)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |