From 50cd1ce1b0d05eed304603143be3ff46cc3b2e5c Mon Sep 17 00:00:00 2001 From: auronandace Date: Sat, 7 Feb 2026 19:38:07 +0000 Subject: [PATCH] add unpredictable_function_pointer_comparisons lint --- Cargo.toml | 2 ++ src/header/signal/mod.rs | 11 +++++++++-- src/header/stdlib/mod.rs | 16 ++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a6c204d461..9b94043323 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,12 +41,14 @@ zero_ptr = "warn" # must allow on public constants due to cbindgen issue dangling_pointers_from_temporaries = "deny" irrefutable_let_patterns = "deny" non_camel_case_types = "allow" +unpredictable_function_pointer_comparisons = "deny" unsafe_op_in_unsafe_fn = "deny" [lints.rust] dangling_pointers_from_temporaries = "deny" irrefutable_let_patterns = "deny" non_camel_case_types = "allow" +unpredictable_function_pointer_comparisons = "deny" unsafe_op_in_unsafe_fn = "deny" [build-dependencies] diff --git a/src/header/signal/mod.rs b/src/header/signal/mod.rs index e0309b90c0..18c6d6a24e 100644 --- a/src/header/signal/mod.rs +++ b/src/header/signal/mod.rs @@ -456,7 +456,14 @@ pub unsafe extern "C" fn sigset( if unsafe { sigaddset(&mut set, sig) } < 0 { return sig_err; } else { - if func == sig_hold { + let is_equal = { + match (func, sig_hold) { + (None, None) => true, + (Some(_), None) | (None, Some(_)) => false, + (Some(f), Some(sh)) => ptr::fn_addr_eq(f, sh), + } + }; + if is_equal { if unsafe { sigaction(sig, ptr::null_mut(), old_sa.as_mut_ptr()) } < 0 || unsafe { sigprocmask(SIG_BLOCK, &mut set, &mut set) } < 0 { @@ -588,7 +595,7 @@ pub unsafe extern "C" fn psignal(sig: c_int, prefix: *const c_char) { #[unsafe(no_mangle)] pub unsafe extern "C" fn psiginfo(info: *const siginfo_t, prefix: *const c_char) { unsafe { - psignal(unsafe { &*info }.si_signo, prefix); + psignal((*info).si_signo, prefix); } } diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index f6698fe7da..f43b80c508 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -156,7 +156,7 @@ pub unsafe extern "C" fn aligned_alloc(alignment: size_t, size: size_t) -> *mut #[unsafe(no_mangle)] pub unsafe extern "C" fn at_quick_exit(func: Option) -> c_int { for i in 0..unsafe { AT_QUICK_EXIT_FUNCS.unsafe_ref().len() } { - if unsafe { AT_QUICK_EXIT_FUNCS.unsafe_ref() }[i] == None { + if unsafe { AT_QUICK_EXIT_FUNCS.unsafe_ref() }[i].is_none() { (unsafe { AT_QUICK_EXIT_FUNCS.unsafe_mut() })[i] = func; return 0; } @@ -169,7 +169,7 @@ pub unsafe extern "C" fn at_quick_exit(func: Option) -> c_int { #[unsafe(no_mangle)] pub unsafe extern "C" fn atexit(func: Option) -> c_int { for i in 0..unsafe { ATEXIT_FUNCS.unsafe_ref().len() } { - if unsafe { ATEXIT_FUNCS.unsafe_ref() }[i] == None { + if unsafe { ATEXIT_FUNCS.unsafe_ref() }[i].is_none() { (unsafe { ATEXIT_FUNCS.unsafe_mut() })[i] = func; return 0; } @@ -219,7 +219,7 @@ macro_rules! dec_num_from_ascii { /// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn atoi(s: *const c_char) -> c_int { - unsafe { dec_num_from_ascii!(s, c_int) } + dec_num_from_ascii!(s, c_int) } /// See . @@ -792,7 +792,7 @@ where fn get_nstime() -> u64 { unsafe { let mut ts = mem::MaybeUninit::uninit(); - Sys::clock_gettime(CLOCK_MONOTONIC, Out::from_uninit_mut(&mut ts)); + if let Ok(_) = Sys::clock_gettime(CLOCK_MONOTONIC, Out::from_uninit_mut(&mut ts)) {}; // TODO what to do if Err? ts.assume_init().tv_nsec as u64 } } @@ -1018,7 +1018,7 @@ unsafe fn put_new_env(insert: *mut c_char) { // chance of a memory leak. But we can check if it was the same as before, like musl does. if unsafe { platform::environ } == unsafe { platform::OUR_ENVIRON.unsafe_mut().as_mut_ptr() } { { - let mut our_environ = unsafe { &mut *platform::OUR_ENVIRON.as_mut_ptr() }; + let our_environ = unsafe { &mut *platform::OUR_ENVIRON.as_mut_ptr() }; *our_environ.last_mut().unwrap() = insert; our_environ.push(core::ptr::null_mut()); } @@ -1026,7 +1026,7 @@ unsafe fn put_new_env(insert: *mut c_char) { unsafe { platform::environ = platform::OUR_ENVIRON.unsafe_mut().as_mut_ptr() }; } else { { - let mut our_environ = unsafe { &mut *platform::OUR_ENVIRON.as_mut_ptr() }; + let our_environ = unsafe { &mut *platform::OUR_ENVIRON.as_mut_ptr() }; our_environ.clear(); our_environ.extend(platform::environ_iter()); our_environ.push(insert); @@ -1640,7 +1640,7 @@ pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int { // No need to worry about updating the pointer, this does not // reallocate in any way. And the final null is already shifted back. { - let mut our_environ = unsafe { &mut *platform::OUR_ENVIRON.as_mut_ptr() }; + let our_environ = unsafe { &mut *platform::OUR_ENVIRON.as_mut_ptr() }; our_environ.remove(i); } @@ -1648,7 +1648,7 @@ pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int { unsafe { platform::environ = platform::OUR_ENVIRON.unsafe_mut().as_mut_ptr() }; } else { { - let mut our_environ = unsafe { &mut *platform::OUR_ENVIRON.as_mut_ptr() }; + let our_environ = unsafe { &mut *platform::OUR_ENVIRON.as_mut_ptr() }; our_environ.clear(); our_environ.extend( platform::environ_iter()