Skip to content

Commit 327cb39

Browse files
committed
Make SocketAncillary::add_* return a Result to indicate failure.
1 parent 28e9eca commit 327cb39

File tree

4 files changed

+62
-32
lines changed

4 files changed

+62
-32
lines changed

library/std/src/os/unix/net/ancillary.rs

+56-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{sockaddr_un, SocketAddr};
22
use crate::convert::TryFrom;
3+
use crate::fmt;
34
use crate::io::{self, IoSlice, IoSliceMut};
45
use crate::marker::PhantomData;
56
use crate::mem::{size_of, zeroed, MaybeUninit};
@@ -84,28 +85,22 @@ fn add_to_ancillary_data<T>(
8485
source: &[T],
8586
cmsg_level: libc::c_int,
8687
cmsg_type: libc::c_int,
87-
) -> bool {
88-
let source_len = if let Some(source_len) = source.len().checked_mul(size_of::<T>()) {
89-
if let Ok(source_len) = u32::try_from(source_len) {
90-
source_len
91-
} else {
92-
return false;
93-
}
94-
} else {
95-
return false;
96-
};
88+
) -> Result<(), AddAncillaryError> {
89+
let source_len =
90+
source.len().checked_mul(size_of::<T>()).ok_or_else(|| AddAncillaryError::new())?;
91+
let source_len = u32::try_from(source_len).map_err(|_| AddAncillaryError::new())?;
9792

9893
unsafe {
9994
let additional_space = libc::CMSG_SPACE(source_len) as usize;
10095

10196
let new_length = if let Some(new_length) = additional_space.checked_add(*length) {
10297
new_length
10398
} else {
104-
return false;
99+
return Err(AddAncillaryError::new());
105100
};
106101

107102
if new_length > buffer.len() {
108-
return false;
103+
return Err(AddAncillaryError::new());
109104
}
110105

111106
buffer[*length..new_length].fill(MaybeUninit::new(0));
@@ -131,7 +126,7 @@ fn add_to_ancillary_data<T>(
131126
}
132127

133128
if previous_cmsg.is_null() {
134-
return false;
129+
return Err(AddAncillaryError::new());
135130
}
136131

137132
(*previous_cmsg).cmsg_level = cmsg_level;
@@ -142,7 +137,7 @@ fn add_to_ancillary_data<T>(
142137

143138
libc::memcpy(data, source.as_ptr().cast(), source_len as usize);
144139
}
145-
true
140+
Ok(())
146141
}
147142

148143
struct AncillaryDataIter<'a, T> {
@@ -536,10 +531,9 @@ impl<'a> SocketAncillary<'a> {
536531

537532
/// Add file descriptors to the ancillary data.
538533
///
539-
/// The function returns `true` if there was enough space in the buffer.
540-
/// If there was not enough space then no file descriptors was appended.
541-
/// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
542-
/// and type `SCM_RIGHTS`.
534+
/// This operation adds a control message with the level `SOL_SOCKET` and type `SCM_RIGHTS`.
535+
/// If there is not enough space in the buffer for all file descriptors,
536+
/// an error is returned and no file descriptors are added.
543537
///
544538
/// # Example
545539
///
@@ -554,7 +548,7 @@ impl<'a> SocketAncillary<'a> {
554548
///
555549
/// let mut ancillary_buffer = [0; 128];
556550
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
557-
/// ancillary.add_fds(&[sock.as_raw_fd()][..]);
551+
/// ancillary.add_fds(&[sock.as_raw_fd()][..])?;
558552
///
559553
/// let mut buf = [1; 8];
560554
/// let mut bufs = &mut [IoSlice::new(&mut buf[..])][..];
@@ -563,7 +557,7 @@ impl<'a> SocketAncillary<'a> {
563557
/// }
564558
/// ```
565559
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
566-
pub fn add_fds(&mut self, fds: &[RawFd]) -> bool {
560+
pub fn add_fds(&mut self, fds: &[RawFd]) -> Result<(), AddAncillaryError> {
567561
self.truncated = false;
568562
add_to_ancillary_data(
569563
&mut self.buffer,
@@ -576,14 +570,13 @@ impl<'a> SocketAncillary<'a> {
576570

577571
/// Add credentials to the ancillary data.
578572
///
579-
/// The function returns `true` if there was enough space in the buffer.
580-
/// If there was not enough space then no credentials was appended.
581-
/// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
582-
/// and type `SCM_CREDENTIALS` or `SCM_CREDS`.
583-
///
573+
/// This function adds a control message with the level `SOL_SOCKET`
574+
/// and type `SCM_CREDENTIALS` or `SCM_CREDS` (depending on the platform).
575+
/// If there is not enough space in the buffer for all credentials,
576+
/// an error is returned and no credentials are added.
584577
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
585578
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
586-
pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool {
579+
pub fn add_creds(&mut self, creds: &[SocketCred]) -> Result<(), AddAncillaryError> {
587580
self.truncated = false;
588581
add_to_ancillary_data(
589582
&mut self.buffer,
@@ -642,3 +635,40 @@ impl<'a> SocketAncillary<'a> {
642635
self.truncated = false;
643636
}
644637
}
638+
639+
/// An error returned when trying to add anciallary data that exceeds the buffer capacity.
640+
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
641+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
642+
pub struct AddAncillaryError {
643+
_priv: (),
644+
}
645+
646+
impl AddAncillaryError {
647+
fn new() -> Self {
648+
Self { _priv: () }
649+
}
650+
}
651+
652+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
653+
impl fmt::Debug for AddAncillaryError {
654+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
655+
f.debug_struct("AddAncillaryError").finish()
656+
}
657+
}
658+
659+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
660+
impl fmt::Display for AddAncillaryError {
661+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
662+
write!(f, "could not add data to anciallary buffer")
663+
}
664+
}
665+
666+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
667+
impl crate::error::Error for AddAncillaryError {}
668+
669+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
670+
impl From<AddAncillaryError> for io::Error {
671+
fn from(other: AddAncillaryError) -> Self {
672+
Self::new(io::ErrorKind::Other, other)
673+
}
674+
}

library/std/src/os/unix/net/datagram.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ impl UnixDatagram {
521521
/// let fds = [0, 1, 2];
522522
/// let mut ancillary_buffer = [0; 128];
523523
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
524-
/// ancillary.add_fds(&fds[..]);
524+
/// ancillary.add_fds(&fds[..])?;
525525
/// sock.send_vectored_with_ancillary_to(bufs, &mut ancillary, "/some/sock")
526526
/// .expect("send_vectored_with_ancillary_to function failed");
527527
/// Ok(())
@@ -570,7 +570,7 @@ impl UnixDatagram {
570570
/// let fds = [0, 1, 2];
571571
/// let mut ancillary_buffer = [0; 128];
572572
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
573-
/// ancillary.add_fds(&fds[..]);
573+
/// ancillary.add_fds(&fds[..])?;
574574
/// sock.send_vectored_with_ancillary(bufs, &mut ancillary)
575575
/// .expect("send_vectored_with_ancillary function failed");
576576
/// Ok(())

library/std/src/os/unix/net/stream.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ impl UnixStream {
545545
/// let fds = [0, 1, 2];
546546
/// let mut ancillary_buffer = [0; 128];
547547
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
548-
/// ancillary.add_fds(&fds[..]);
548+
/// ancillary.add_fds(&fds[..])?;
549549
/// socket.send_vectored_with_ancillary(bufs, &mut ancillary)
550550
/// .expect("send_vectored_with_ancillary function failed");
551551
/// Ok(())

library/std/src/os/unix/net/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ fn test_send_vectored_fds_unix_stream() {
490490

491491
let mut ancillary1_buffer = [0; 128];
492492
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
493-
assert!(ancillary1.add_fds(&[s1.as_raw_fd()][..]));
493+
ancillary1.add_fds(&[s1.as_raw_fd()][..]).unwrap();
494494

495495
let usize = or_panic!(s1.send_vectored_with_ancillary(&bufs_send, &mut ancillary1));
496496
assert_eq!(usize, 8);
@@ -551,7 +551,7 @@ fn test_send_vectored_with_ancillary_to_unix_datagram() {
551551
cred1.set_pid(getpid());
552552
cred1.set_uid(getuid());
553553
cred1.set_gid(getgid());
554-
assert!(ancillary1.add_creds(&[cred1.clone()][..]));
554+
ancillary1.add_creds(&[cred1.clone()][..]).unwrap();
555555

556556
let usize =
557557
or_panic!(bsock1.send_vectored_with_ancillary_to(&bufs_send, &mut ancillary1, &path2));
@@ -608,7 +608,7 @@ fn test_send_vectored_with_ancillary_unix_datagram() {
608608

609609
let mut ancillary1_buffer = [0; 128];
610610
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
611-
assert!(ancillary1.add_fds(&[bsock1.as_raw_fd()][..]));
611+
ancillary1.add_fds(&[bsock1.as_raw_fd()][..]).unwrap();
612612

613613
or_panic!(bsock1.connect(&path2));
614614
let usize = or_panic!(bsock1.send_vectored_with_ancillary(&bufs_send, &mut ancillary1));

0 commit comments

Comments
 (0)