diff --git a/CHANGELOG.md b/CHANGELOG.md index b57bbed5b2..1b72b86cfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,11 @@ This project adheres to [Semantic Versioning](https://semver.org/). `SockaddrIn6::from`, `IpMembershipRequest::new`, and `Ipv6MembershipRequest::new` with future Rust versions. ([#2061](https://github.com/nix-rust/nix/pull/2061)) +- Fix potentially invalid conversions in + `SockaddrIn::from`, + `SockaddrIn6::from`, `IpMembershipRequest::new`, and + `Ipv6MembershipRequest::new` with future Rust versions. + ([#2061](https://github.com/nix-rust/nix/pull/2061)) ### Removed diff --git a/Cargo.toml b/Cargo.toml index 8b1d873186..acb03363bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,6 @@ libc = { version = "0.2.137", features = [ "extra_traits" ] } bitflags = "1.1" cfg-if = "1.0" pin-utils = { version = "0.1.0", optional = true } -static_assertions = "1" [target.'cfg(not(target_os = "redox"))'.dependencies] memoffset = { version = "0.7", optional = true } diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 6f2e9eb31e..6ae657f493 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -41,21 +41,16 @@ 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() } } @@ -1276,9 +1271,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)]