Clippy lints / Correctness
Clippy lint · Correctness · checked with Clippy 0.1.99
clippy::not_unsafe_ptr_arg_deref: this public function might dereference a raw pointer but is not marked `unsafe`
What it meansChecks for public functions that dereference raw pointer arguments but are not marked unsafe. It is in the correctness group and denied by default (the build fails).
What you see
error: this public function might dereference a raw pointer but is not marked `unsafe` --> main.rs:2:30 | 2 | println!("{}", unsafe { *x }); | ^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#not_unsafe_ptr_arg_deref = note: `#[deny(clippy::not_unsafe_ptr_arg_deref)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
The function should almost definitely be marked unsafe, since for an arbitrary raw pointer, there is no way of telling for sure if it is valid. In general, this lint should never be disabled unless it is definitely a false positive (please submit an issue if so) since it breaks Rust’s soundness guarantees, directly exposing API users to potentially dangerous program behavior. This is also true for internal APIs, as it is easy to leak unsoundness.
Code that is almost certainly wrong. Denied by default: the build fails until you fix it or allow it.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
pub fn foo(x: *const u8) {
println!("{}", unsafe { *x });
}
// this call "looks" safe but will segfault or worse!
// foo(invalid_ptr);
Write this instead
pub unsafe fn foo(x: *const u8) {
println!("{}", unsafe { *x });
}
// this would cause a compiler error for calling without `unsafe`
// foo(invalid_ptr);
// sound call if the caller knows the pointer is valid
unsafe { foo(valid_ptr); }
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
not_unsafe_ptr_arg_deref = "allow"One run
cargo clippy -- -A clippy::not-unsafe-ptr-arg-derefThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for public functions that dereference raw pointer
arguments but are not marked unsafe.
Why is this bad?
The function should almost definitely be marked unsafe, since for an
arbitrary raw pointer, there is no way of telling for sure if it is valid.
In general, this lint should never be disabled unless it is definitely a false positive (please submit an issue if so) since it breaks Rust’s soundness guarantees, directly exposing API users to potentially dangerous program behavior. This is also true for internal APIs, as it is easy to leak unsoundness.
Context
In Rust, an unsafe {...} block is used to indicate that the code in that
section has been verified in some way that the compiler can not. For a
function that accepts a raw pointer then accesses the pointer’s data, this is
generally impossible as the incoming pointer could point anywhere, valid or
not. So, the signature should be marked unsafe fn: this indicates that the
function’s caller must provide some verification that the arguments it sends
are valid (and then call the function within an unsafe block).
Known problems
- It does not check functions recursively so if the pointer is passed to a
private non-
unsafefunction which does the dereferencing, the lint won’t trigger (false negative). - It only checks for arguments whose type are raw pointers, not raw pointers
got from an argument in some other way (
fn foo(bar: &[*const u8])orsome_argument.get_raw_ptr()) (false negative).
Example
pub fn foo(x: *const u8) {
println!("{}", unsafe { *x });
}
// this call "looks" safe but will segfault or worse!
// foo(invalid_ptr);
Use instead:
pub unsafe fn foo(x: *const u8) {
println!("{}", unsafe { *x });
}
// this would cause a compiler error for calling without `unsafe`
// foo(invalid_ptr);
// sound call if the caller knows the pointer is valid
unsafe { foo(valid_ptr); }
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- never_loopthis loop never actually loops
- non_octal_unix_permissionsChecks for non-octal values used to set Unix file permissions
- nonsensical_open_optionsChecks for duplicate open options as well as combinations that make no sense
- option_env_unwrapthis will panic at run-time if the environment variable doesn't exist at compile-time
- out_of_bounds_indexingindex is out of bounds
- panicking_overflow_checksDetects C-style underflow/overflow checks
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::never_loopthis loop never actually loopsRead it next →Did this answer your question?