diff options
author | Alan Somers <asomers@gmail.com> | 2020-05-16 16:13:12 -0600 |
---|---|---|
committer | Alan Somers <asomers@gmail.com> | 2020-06-27 17:34:16 -0600 |
commit | f4fc06dbe0008768ccce068f9a9b11d91a4190d2 (patch) | |
tree | 2007ef0a6a442df7850b0f111c5d757c94ca61d7 /src/sys/inotify.rs | |
parent | 2c42b302ed78b8cfc45560b26f01487005c4ccd1 (diff) | |
download | nix-f4fc06dbe0008768ccce068f9a9b11d91a4190d2.zip |
Fix an unaligned pointer read in Inotify::read_events
Reported-by: Clippy
Diffstat (limited to 'src/sys/inotify.rs')
-rw-r--r-- | src/sys/inotify.rs | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/src/sys/inotify.rs b/src/sys/inotify.rs index 078015b6..e73c3fcb 100644 --- a/src/sys/inotify.rs +++ b/src/sys/inotify.rs @@ -29,8 +29,9 @@ use libc::{ }; use std::ffi::{OsString,OsStr,CStr}; use std::os::unix::ffi::OsStrExt; -use std::mem::size_of; +use std::mem::{MaybeUninit, size_of}; use std::os::unix::io::{RawFd,AsRawFd,FromRawFd}; +use std::ptr; use crate::unistd::read; use crate::Result; use crate::NixPath; @@ -172,7 +173,8 @@ impl Inotify { /// events could be read then the EAGAIN error is returned. pub fn read_events(&self) -> Result<Vec<InotifyEvent>> { let header_size = size_of::<libc::inotify_event>(); - let mut buffer = [0u8; 4096]; + const BUFSIZ: usize = 4096; + let mut buffer = [0u8; BUFSIZ]; let mut events = Vec::new(); let mut offset = 0; @@ -180,11 +182,13 @@ impl Inotify { while (nread - offset) >= header_size { let event = unsafe { - &*( - buffer - .as_ptr() - .offset(offset as isize) as *const libc::inotify_event - ) + let mut event = MaybeUninit::<libc::inotify_event>::uninit(); + ptr::copy_nonoverlapping( + buffer.as_ptr().add(offset), + event.as_mut_ptr() as *mut u8, + (BUFSIZ - offset).min(header_size) + ); + event.assume_init() }; let name = match event.len { @@ -193,7 +197,7 @@ impl Inotify { let ptr = unsafe { buffer .as_ptr() - .offset(offset as isize + header_size as isize) + .add(offset + header_size) as *const c_char }; let cstr = unsafe { CStr::from_ptr(ptr) }; |