Skip to content

Commit 5213840

Browse files
bors[bot]djkoloski
andauthored
Merge #1983
1983: Replace uses of `mem::transmute` with safe methods r=rtzoeller a=djkoloski These transmutes are unnecessary and technically incorrect since std doesn't guarantee that the byte order of their network address types matches the expected byte order of libc. Co-authored-by: David Koloski <[email protected]>
2 parents 15d624a + ea2f370 commit 5213840

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/sys/socket/addr.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,17 @@ use std::{fmt, mem, net, ptr, slice};
3939
/// Convert a std::net::Ipv4Addr into the libc form.
4040
#[cfg(feature = "net")]
4141
pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr {
42-
static_assertions::assert_eq_size!(net::Ipv4Addr, libc::in_addr);
43-
// Safe because both types have the same memory layout, and no fancy Drop
44-
// impls.
45-
unsafe { mem::transmute(addr) }
42+
libc::in_addr {
43+
s_addr: u32::from_ne_bytes(addr.octets()),
44+
}
4645
}
4746

4847
/// Convert a std::net::Ipv6Addr into the libc form.
4948
#[cfg(feature = "net")]
5049
pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> libc::in6_addr {
51-
static_assertions::assert_eq_size!(net::Ipv6Addr, libc::in6_addr);
52-
// Safe because both are Newtype wrappers around the same libc type
53-
unsafe { mem::transmute(*addr) }
50+
libc::in6_addr {
51+
s6_addr: addr.octets(),
52+
}
5453
}
5554

5655
/// These constants specify the protocol family to be used

0 commit comments

Comments
 (0)