Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/backend/libc/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use {
core::slice,
};
#[cfg(all(unix, feature = "alloc"))]
use {
crate::ffi::CString,
alloc::borrow::{Cow, ToOwned},
};
use {crate::ffi::CString, alloc::borrow::Cow, alloc::vec::Vec};

/// `struct sockaddr_un`
#[cfg(unix)]
Expand Down Expand Up @@ -143,7 +140,8 @@ impl SocketAddrUnix {
let bytes = self.bytes()?;
if !bytes.is_empty() && bytes[0] != 0 {
if self.unix.sun_path.len() == bytes.len() {
self.path_with_termination(bytes)
// SAFETY: no NULs are contained in bytes
unsafe { self.path_with_termination(bytes) }
} else {
// SAFETY: `from_bytes_with_nul_unchecked` since the string is
// NUL-terminated.
Expand All @@ -155,11 +153,18 @@ impl SocketAddrUnix {
}

/// If the `sun_path` field is not NUL-terminated, terminate it.
///
/// SAFETY: the input `bytes` must not contain any NULs
#[cfg(feature = "alloc")]
fn path_with_termination(&self, bytes: &[u8]) -> Option<Cow<'_, CStr>> {
let mut owned = bytes.to_owned();
unsafe fn path_with_termination(&self, bytes: &[u8]) -> Option<Cow<'_, CStr>> {
let mut owned = Vec::with_capacity(bytes.len() + 1);
owned.extend_from_slice(bytes);
owned.push(b'\0');
Some(CString::from_vec_with_nul(owned).unwrap().into())
// SAFETY: `from_vec_with_nul_unchecked` since the string is
// NUL-terminated and `bytes` does not conain any NULs.
Some(Cow::Owned(
CString::from_vec_with_nul_unchecked(owned).into(),
))
}

/// For a filesystem path address, return the path as a byte sequence,
Expand Down
21 changes: 13 additions & 8 deletions src/backend/linux_raw/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
use core::{fmt, slice};
#[cfg(feature = "alloc")]
use {
crate::ffi::CString,
alloc::borrow::{Cow, ToOwned},
};
use {crate::ffi::CString, alloc::borrow::Cow, alloc::vec::Vec};

/// `struct sockaddr_un`
#[derive(Clone)]
Expand Down Expand Up @@ -105,7 +102,8 @@ impl SocketAddrUnix {
let bytes = self.bytes()?;
if !bytes.is_empty() && bytes[0] != 0 {
if self.unix.sun_path.len() == bytes.len() {
self.path_with_termination(bytes)
// SAFETY: no NULs are contained in bytes
unsafe { self.path_with_termination(bytes) }
} else {
// SAFETY: `from_bytes_with_nul_unchecked` since the string is
// NUL-terminated.
Expand All @@ -117,11 +115,18 @@ impl SocketAddrUnix {
}

/// If the `sun_path` field is not NUL-terminated, terminate it.
///
/// SAFETY: the input `bytes` must not contain any NULs
#[cfg(feature = "alloc")]
fn path_with_termination(&self, bytes: &[u8]) -> Option<Cow<'_, CStr>> {
let mut owned = bytes.to_owned();
unsafe fn path_with_termination(&self, bytes: &[u8]) -> Option<Cow<'_, CStr>> {
let mut owned = Vec::with_capacity(bytes.len() + 1);
owned.extend_from_slice(bytes);
owned.push(b'\0');
Some(CString::from_vec_with_nul(owned).unwrap().into())
// SAFETY: `from_vec_with_nul_unchecked` since the string is
// NUL-terminated and `bytes` does not conain any NULs.
Some(Cow::Owned(
CString::from_vec_with_nul_unchecked(owned).into(),
))
}

/// For a filesystem path address, return the path as a byte sequence,
Expand Down