Skip to content

Commit 46a8698

Browse files
committed
misc clippy cleanup
1 parent af45859 commit 46a8698

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

src/fcntl.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ libc_bitflags!(
161161
}
162162
);
163163

164+
// The conversion is not identical on all operating systems.
165+
#[allow(clippy::identity_conversion)]
164166
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
165167
let fd = path.with_nix_path(|cstr| {
166168
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
@@ -169,6 +171,8 @@ pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<R
169171
Errno::result(fd)
170172
}
171173

174+
// The conversion is not identical on all operating systems.
175+
#[allow(clippy::identity_conversion)]
172176
#[cfg(not(target_os = "redox"))]
173177
pub fn openat<P: ?Sized + NixPath>(
174178
dirfd: RawFd,

src/sched.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ mod sched_linux_like {
8080
if field >= CpuSet::count() {
8181
Err(Error::Sys(Errno::EINVAL))
8282
} else {
83-
Ok(unsafe { libc::CPU_SET(field, &mut self.cpu_set) })
83+
unsafe { libc::CPU_SET(field, &mut self.cpu_set); }
84+
Ok(())
8485
}
8586
}
8687

@@ -90,7 +91,8 @@ mod sched_linux_like {
9091
if field >= CpuSet::count() {
9192
Err(Error::Sys(Errno::EINVAL))
9293
} else {
93-
Ok(unsafe { libc::CPU_CLR(field, &mut self.cpu_set) })
94+
unsafe { libc::CPU_CLR(field, &mut self.cpu_set);}
95+
Ok(())
9496
}
9597
}
9698

@@ -187,8 +189,8 @@ mod sched_linux_like {
187189

188190
let res = unsafe {
189191
let combined = flags.bits() | signal.unwrap_or(0);
190-
let ptr = stack.as_mut_ptr().offset(stack.len() as isize);
191-
let ptr_aligned = ptr.offset((ptr as usize % 16) as isize * -1);
192+
let ptr = stack.as_mut_ptr().add(stack.len());
193+
let ptr_aligned = ptr.sub(ptr as usize % 16);
192194
libc::clone(
193195
mem::transmute(
194196
callback as extern "C" fn(*mut Box<dyn FnMut() -> isize>) -> i32,

src/sys/mman.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,37 @@ libc_bitflags!{
260260
}
261261
}
262262

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

269-
/// Unlocks all memory pages that contain part of the address range with `length` bytes starting at
270-
/// `addr`.
275+
/// Unlocks all memory pages that contain part of the address range with
276+
/// `length` bytes starting at `addr`.
277+
///
278+
/// # Safety
279+
///
280+
/// `addr` must meet all the requirements described in the `munlock(2)` man
281+
/// page.
271282
pub unsafe fn munlock(addr: *const c_void, length: size_t) -> Result<()> {
272283
Errno::result(libc::munlock(addr, length)).map(drop)
273284
}
274285

275-
/// Locks all memory pages mapped into this process' address space. Locked pages never move to the
276-
/// swap area.
286+
/// Locks all memory pages mapped into this process' address space.
287+
///
288+
/// Locked pages never move to the swap area.
289+
///
290+
/// # Safety
291+
///
292+
/// `addr` must meet all the requirements described in the `mlockall(2)` man
293+
/// page.
277294
pub fn mlockall(flags: MlockAllFlags) -> Result<()> {
278295
unsafe { Errno::result(libc::mlockall(flags.bits())) }.map(drop)
279296
}
@@ -283,8 +300,11 @@ pub fn munlockall() -> Result<()> {
283300
unsafe { Errno::result(libc::munlockall()) }.map(drop)
284301
}
285302

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

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

318+
/// remove a mapping
319+
///
320+
/// # Safety
321+
///
322+
/// `addr` must meet all the requirements described in the `munmap(2)` man
323+
/// page.
298324
pub unsafe fn munmap(addr: *mut c_void, len: size_t) -> Result<()> {
299325
Errno::result(libc::munmap(addr, len)).map(drop)
300326
}
301327

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

367+
/// synchronize a mapped region
368+
///
369+
/// # Safety
370+
///
371+
/// `addr` must meet all the requirements described in the `msync(2)` man
372+
/// page.
335373
pub unsafe fn msync(addr: *mut c_void, length: size_t, flags: MsFlags) -> Result<()> {
336374
Errno::result(libc::msync(addr, length, flags.bits())).map(drop)
337375
}

src/ucontext.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ impl UContext {
3030
}
3131

3232
pub fn sigmask_mut(&mut self) -> &mut SigSet {
33-
unsafe { mem::transmute(&mut self.context.uc_sigmask) }
33+
unsafe {
34+
&mut *(&mut self.context.uc_sigmask as *mut libc::sigset_t as *mut SigSet)
35+
}
3436
}
3537

3638
pub fn sigmask(&self) -> &SigSet {
37-
unsafe { mem::transmute(&self.context.uc_sigmask) }
39+
unsafe {
40+
&*(&self.context.uc_sigmask as *const libc::sigset_t as *const SigSet)
41+
}
3842
}
3943
}

src/unistd.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,9 +1465,7 @@ pub fn getgroups() -> Result<Vec<Gid>> {
14651465
/// # Ok(())
14661466
/// # }
14671467
/// #
1468-
/// # fn main() {
1469-
/// # try_main().unwrap();
1470-
/// # }
1468+
/// # try_main().unwrap();
14711469
/// ```
14721470
#[cfg(not(any(target_os = "ios", target_os = "macos", target_os = "redox")))]
14731471
pub fn setgroups(groups: &[Gid]) -> Result<()> {
@@ -1589,9 +1587,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
15891587
/// # Ok(())
15901588
/// # }
15911589
/// #
1592-
/// # fn main() {
1593-
/// # try_main().unwrap();
1594-
/// # }
1590+
/// # try_main().unwrap();
15951591
/// ```
15961592
#[cfg(not(any(target_os = "ios", target_os = "macos", target_os = "redox")))]
15971593
pub fn initgroups(user: &CStr, group: Gid) -> Result<()> {

0 commit comments

Comments
 (0)