Skip to content

Commit e9f5126

Browse files
bors[bot]asomers
andcommitted
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 <[email protected]>
1 parent 78ecbf2 commit e9f5126

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ libc = { version = "0.2.137", features = [ "extra_traits" ] }
3131
bitflags = "1.1"
3232
cfg-if = "1.0"
3333
pin-utils = { version = "0.1.0", optional = true }
34-
static_assertions = "1"
3534

3635
[target.'cfg(not(target_os = "redox"))'.dependencies]
3736
memoffset = { version = "0.7", optional = true }

src/sys/socket/addr.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,16 @@ use std::{fmt, mem, net, ptr, slice};
4141
/// Convert a std::net::Ipv4Addr into the libc form.
4242
#[cfg(feature = "net")]
4343
pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr {
44-
static_assertions::assert_eq_size!(net::Ipv4Addr, libc::in_addr);
45-
// Safe because both types have the same memory layout, and no fancy Drop
46-
// impls.
47-
unsafe {
48-
mem::transmute(addr)
44+
libc::in_addr {
45+
s_addr: u32::from_ne_bytes(addr.octets())
4946
}
5047
}
5148

5249
/// Convert a std::net::Ipv6Addr into the libc form.
5350
#[cfg(feature = "net")]
5451
pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> libc::in6_addr {
55-
static_assertions::assert_eq_size!(net::Ipv6Addr, libc::in6_addr);
56-
// Safe because both are Newtype wrappers around the same libc type
57-
unsafe {
58-
mem::transmute(*addr)
52+
libc::in6_addr {
53+
s6_addr: addr.octets()
5954
}
6055
}
6156

@@ -1276,9 +1271,6 @@ impl SockaddrLike for () {
12761271
}
12771272

12781273
/// An IPv4 socket address
1279-
// This is identical to net::SocketAddrV4. But the standard library
1280-
// doesn't allow direct access to the libc fields, which we need. So we
1281-
// reimplement it here.
12821274
#[cfg(feature = "net")]
12831275
#[repr(transparent)]
12841276
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]

0 commit comments

Comments
 (0)