Skip to content

Commit

Permalink
Cryptovec unix error msg: fix clippy & MSRV
Browse files Browse the repository at this point in the history
  • Loading branch information
amtelekom committed Jan 10, 2025
1 parent 8bec932 commit 2e0f0a1
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions cryptovec/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use libc::c_void;
/// Unlock memory on drop for Unix-based systems.
pub fn munlock(ptr: *const u8, len: usize) {
unsafe {
#[allow(clippy::panic)]
if libc::munlock(ptr as *const c_void, len) != 0 {
panic_libc_error("Failed to unlock memory");
}
Expand All @@ -14,7 +13,6 @@ pub fn munlock(ptr: *const u8, len: usize) {

pub fn mlock(ptr: *const u8, len: usize) {
unsafe {
#[allow(clippy::panic)]
if libc::mlock(ptr as *const c_void, len) != 0 {
panic_libc_error("Failed to lock memory");
}
Expand All @@ -27,17 +25,25 @@ pub fn memset(ptr: *mut u8, value: i32, size: usize) {
}
}

#[allow(clippy::panic)]
unsafe fn panic_libc_error(msg: &str) {
let errno = *libc::__errno_location();
const ERRMAXLEN: usize = 255;
const INVALID_ERR: &str = "Unknown";
let mut errdesc = [0u8; ERRMAXLEN];
let errdesc = if libc::strerror_r(errno, errdesc.as_mut_ptr() as _, ERRMAXLEN) == 0 {
CStr::from_bytes_until_nul(&errdesc)
.ok()
.and_then(|msg| msg.to_str().ok())
.unwrap_or("Invalid error description")
if let Some(nul_pos) = errdesc.iter().position(|b| *b == 0) {
#[allow(clippy::indexing_slicing)]
// safety: the position was just checked, so it is guaranteed to be in range
CStr::from_bytes_with_nul(&errdesc[0..nul_pos])
.ok()
.and_then(|msg| msg.to_str().ok())
.unwrap_or(INVALID_ERR)
} else {
INVALID_ERR
}
} else {
"Invalid error"
INVALID_ERR
};
// Note: if you get 'Cannot allocate memory (0xc)' here,
// check if your RLIMIT_MEMLOCK (`ulimit -l`) is configured low!
Expand Down

0 comments on commit 2e0f0a1

Please sign in to comment.