Skip to content

Commit

Permalink
netbsd: Clarify type conversions are lossless.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jun 2, 2024
1 parent 86b6f50 commit 2fe1763
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/netbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use core::{ffi::c_void, mem::MaybeUninit, ptr};

fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND];
let mut len = buf.len();
let ret = unsafe {
#[allow(clippy::cast_possible_truncation)]
let mib_len = MIB.len() as libc::c_uint;

let mut len = core::cmp::min(buf.len(), libc::ssize_t::MAX.unsignd_abs());
let ret: libc::c_int = unsafe {
libc::sysctl(
MIB.as_ptr(),
MIB.len() as libc::c_uint,
mib_len,
buf.as_mut_ptr().cast::<c_void>(),
&mut len,
ptr::null(),
Expand All @@ -18,7 +21,13 @@ fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
if ret == -1 {
-1
} else {
len as libc::ssize_t
// c_int to ssize_t conversion is lossless.
const _: () =
assert!(core::mem::size_of::<libc::c_int>() == core::mem::size_of::<libc::ssize_t>());
// We clamped the request to `ssize_t::MAX` bytes so this lossless.
#[allow(clippy::cast_possible_truncation)]
let len = len as libc::ssize_t;
len
}
}

Expand Down

0 comments on commit 2fe1763

Please sign in to comment.