Skip to content

Don't use mem::transmute in SignalFd #1489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2021
Merged
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
20 changes: 6 additions & 14 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 = 128;
#[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,14 @@ 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 size = mem::size_of_val(&buffer);
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)
libc::read(self.0, buffer.as_mut_ptr() as *mut libc::c_void, size)
}).map(|r| r as usize);
match res {
Ok(SIGNALFD_SIGINFO_SIZE) => Ok(Some(unsafe { mem::transmute(buffer.assume_init()) })),
Ok(x) if x == size => Ok(Some(unsafe { buffer.assume_init() })),
Ok(_) => unreachable!("partial read on signalfd"),
Err(Errno::EAGAIN) => Ok(None),
Err(error) => Err(error)
Expand Down Expand Up @@ -144,14 +144,6 @@ impl Iterator for SignalFd {
#[cfg(test)]
mod tests {
use super::*;
use std::mem;
use libc;


#[test]
fn check_siginfo_size() {
assert_eq!(mem::size_of::<libc::signalfd_siginfo>(), SIGNALFD_SIGINFO_SIZE);
}

#[test]
fn create_signalfd() {
Expand Down