Skip to content

Commit

Permalink
misc clippy cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers committed May 17, 2020
1 parent a423242 commit 441f1ab
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/fcntl.rs
Expand Up @@ -151,6 +151,8 @@ libc_bitflags!(
}
);

// The conversion is not identical on all operating systems.
#[allow(clippy::identity_conversion)]
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = path.with_nix_path(|cstr| {
let modebits = c_uint::from(mode.bits());
Expand All @@ -160,6 +162,8 @@ pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<R
Errno::result(fd)
}

// The conversion is not identical on all operating systems.
#[allow(clippy::identity_conversion)]
pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = path.with_nix_path(|cstr| {
let modebits = c_uint::from(mode.bits());
Expand Down
10 changes: 6 additions & 4 deletions src/sched.rs
Expand Up @@ -81,7 +81,8 @@ mod sched_linux_like {
if field >= CpuSet::count() {
Err(Error::Sys(Errno::EINVAL))
} else {
Ok(unsafe { libc::CPU_SET(field, &mut self.cpu_set) })
unsafe { libc::CPU_SET(field, &mut self.cpu_set); }
Ok(())
}
}

Expand All @@ -91,7 +92,8 @@ mod sched_linux_like {
if field >= CpuSet::count() {
Err(Error::Sys(Errno::EINVAL))
} else {
Ok(unsafe { libc::CPU_CLR(field, &mut self.cpu_set) })
unsafe { libc::CPU_CLR(field, &mut self.cpu_set);}
Ok(())
}
}

Expand Down Expand Up @@ -188,8 +190,8 @@ mod sched_linux_like {

let res = unsafe {
let combined = flags.bits() | signal.unwrap_or(0);
let ptr = stack.as_mut_ptr().offset(stack.len() as isize);
let ptr_aligned = ptr.offset((ptr as usize % 16) as isize * -1);
let ptr = stack.as_mut_ptr().add(stack.len());
let ptr_aligned = ptr.sub(ptr as usize % 16);
libc::clone(
mem::transmute(
callback as extern "C" fn(*mut Box<dyn FnMut() -> isize>) -> i32,
Expand Down
54 changes: 46 additions & 8 deletions src/sys/mman.rs
Expand Up @@ -260,20 +260,37 @@ libc_bitflags!{
}
}

/// Locks all memory pages that contain part of the address range with `length` bytes starting at
/// `addr`. Locked pages never move to the swap area.
/// Locks all memory pages that contain part of the address range with `length`
/// bytes starting at `addr`.
///
/// Locked pages never move to the swap area.
///
/// # Safety
///
/// `addr` must meet all the requirements described in the `mlock(2)` man page.
pub unsafe fn mlock(addr: *const c_void, length: size_t) -> Result<()> {
Errno::result(libc::mlock(addr, length)).map(drop)
}

/// Unlocks all memory pages that contain part of the address range with `length` bytes starting at
/// `addr`.
/// Unlocks all memory pages that contain part of the address range with
/// `length` bytes starting at `addr`.
///
/// # Safety
///
/// `addr` must meet all the requirements described in the `munlock(2)` man
/// page.
pub unsafe fn munlock(addr: *const c_void, length: size_t) -> Result<()> {
Errno::result(libc::munlock(addr, length)).map(drop)
}

/// Locks all memory pages mapped into this process' address space. Locked pages never move to the
/// swap area.
/// Locks all memory pages mapped into this process' address space.
///
/// Locked pages never move to the swap area.
///
/// # Safety
///
/// `addr` must meet all the requirements described in the `mlockall(2)` man
/// page.
pub fn mlockall(flags: MlockAllFlags) -> Result<()> {
unsafe { Errno::result(libc::mlockall(flags.bits())) }.map(drop)
}
Expand All @@ -283,8 +300,11 @@ pub fn munlockall() -> Result<()> {
unsafe { Errno::result(libc::munlockall()) }.map(drop)
}

/// Calls to mmap are inherently unsafe, so they must be made in an unsafe block. Typically
/// a higher-level abstraction will hide the unsafe interactions with the mmap'd region.
/// allocate memory, or map files or devices into memory
///
/// # Safety
///
/// See the `mmap(2)` man page for detailed requirements.
pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: MapFlags, fd: RawFd, offset: off_t) -> Result<*mut c_void> {
let ret = libc::mmap(addr, length, prot.bits(), flags.bits(), fd, offset);

Expand All @@ -295,10 +315,22 @@ pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: Ma
}
}

/// remove a mapping
///
/// # Safety
///
/// `addr` must meet all the requirements described in the `munmap(2)` man
/// page.
pub unsafe fn munmap(addr: *mut c_void, len: size_t) -> Result<()> {
Errno::result(libc::munmap(addr, len)).map(drop)
}

/// give advice about use of memory
///
/// # Safety
///
/// See the `madvise(2)` man page. Take special care when using
/// `MmapAdvise::MADV_FREE`.
pub unsafe fn madvise(addr: *mut c_void, length: size_t, advise: MmapAdvise) -> Result<()> {
Errno::result(libc::madvise(addr, length, advise as i32)).map(drop)
}
Expand Down Expand Up @@ -332,6 +364,12 @@ pub unsafe fn mprotect(addr: *mut c_void, length: size_t, prot: ProtFlags) -> Re
Errno::result(libc::mprotect(addr, length, prot.bits())).map(drop)
}

/// synchronize a mapped region
///
/// # Safety
///
/// `addr` must meet all the requirements described in the `msync(2)` man
/// page.
pub unsafe fn msync(addr: *mut c_void, length: size_t, flags: MsFlags) -> Result<()> {
Errno::result(libc::msync(addr, length, flags.bits())).map(drop)
}
Expand Down
1 change: 1 addition & 0 deletions src/sys/ptrace/linux.rs
Expand Up @@ -173,6 +173,7 @@ libc_bitflags! {
since="0.10.0",
note="usages of `ptrace()` should be replaced with the specialized helper functions instead"
)]
#[allow(clippy::missing_safety_doc)] // It's deprecated anyway
pub unsafe fn ptrace(request: Request, pid: Pid, addr: AddressType, data: *mut c_void) -> Result<c_long> {
use self::Request::*;
match request {
Expand Down
8 changes: 6 additions & 2 deletions src/ucontext.rs
Expand Up @@ -30,10 +30,14 @@ impl UContext {
}

pub fn sigmask_mut(&mut self) -> &mut SigSet {
unsafe { mem::transmute(&mut self.context.uc_sigmask) }
unsafe {
&mut *(&mut self.context.uc_sigmask as *mut libc::sigset_t as *mut SigSet)
}
}

pub fn sigmask(&self) -> &SigSet {
unsafe { mem::transmute(&self.context.uc_sigmask) }
unsafe {
&*(&self.context.uc_sigmask as *const libc::sigset_t as *const SigSet)
}
}
}
8 changes: 2 additions & 6 deletions src/unistd.rs
Expand Up @@ -1499,9 +1499,7 @@ pub fn getgroups() -> Result<Vec<Gid>> {
/// # Ok(())
/// # }
/// #
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// # try_main().unwrap();
/// ```
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
pub fn setgroups(groups: &[Gid]) -> Result<()> {
Expand Down Expand Up @@ -1623,9 +1621,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
/// # Ok(())
/// # }
/// #
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// # try_main().unwrap();
/// ```
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
pub fn initgroups(user: &CStr, group: Gid) -> Result<()> {
Expand Down

0 comments on commit 441f1ab

Please sign in to comment.