From ea2f370963b99c673938b3d0824f3244bec92400 Mon Sep 17 00:00:00 2001 From: David Koloski Date: Thu, 26 Jan 2023 16:29:50 -0500 Subject: [PATCH] Replace uses of `mem::transmute` with safe methods --- src/sys/socket/addr.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index aad407a7f0..f5d7cc1db6 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