Skip to content

Commit

Permalink
Don't use mem::transmute in SignalFd
Browse files Browse the repository at this point in the history
There was a better case for using it before mem::uninitialized was
available, but not great.  Even before then, mem::zeroed could've been
used instead.

Issue nix-rust#373
  • Loading branch information
asomers committed Aug 11, 2021
1 parent 8ad5dc5 commit 5001986
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/sys/signalfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ libc_bitflags!{
}

pub const SIGNALFD_NEW: RawFd = -1;
pub const SIGNALFD_SIGINFO_SIZE: usize = mem::size_of::<libc::signalfd_siginfo>();
#[deprecated(since = "0.23.0", note = "use mem::size_of::<siginfo>() instead")]
pub const SIGNALFD_SIGINFO_SIZE: usize = mem::size_of::<siginfo>();

/// Creates a new file descriptor for reading signals.
///
Expand Down Expand Up @@ -98,15 +99,16 @@ impl SignalFd {
}

pub fn read_signal(&mut self) -> Result<Option<siginfo>> {
let mut buffer = mem::MaybeUninit::<[u8; SIGNALFD_SIGINFO_SIZE]>::uninit();
let mut buffer = mem::MaybeUninit::<siginfo>::uninit();

let res = Errno::result(unsafe {
libc::read(self.0,
buffer.as_mut_ptr() as *mut libc::c_void,
SIGNALFD_SIGINFO_SIZE as libc::size_t)
mem::size_of_val(*buffer))
}).map(|r| r as usize);
match res {
Ok(SIGNALFD_SIGINFO_SIZE) => Ok(Some(unsafe { mem::transmute(buffer.assume_init()) })),
Ok(mem::size_of_val(*buffer)) =>
Ok(Some(unsafe { buffer.assume_init() })),
Ok(_) => unreachable!("partial read on signalfd"),
Err(Errno::EAGAIN) => Ok(None),
Err(error) => Err(error)
Expand Down

0 comments on commit 5001986

Please sign in to comment.