Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
2061: For invalid IP address conversions with future Rust versions r=asomers a=asomers

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 nix-rust#2053

Co-authored-by: Alan Somers <asomers@gmail.com>
  • Loading branch information
bors[bot] and asomers committed Aug 27, 2023
1 parent 05eee29 commit cedf7a1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
16 changes: 4 additions & 12 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit cedf7a1

Please sign in to comment.