Why is this an issue?

The standard Rust library includes a variety of functions for pointer manipulations. Many of these functions require non-null ponter parameters, including:

Using ptr::null in unsafe code can result in undefined behavior, compromising the stability and safety of the program. Undefined behavior occurs when the program does something the Rust language specifications do not define, often leading to crashes or security vulnerabilities.

Code examples

Noncompliant code example

unsafe { std::slice::from_raw_parts(ptr::null(), 0); } // Noncompliant: Usage of `ptr::null()`.

Compliant solution

unsafe { std::slice::from_raw_parts(NonNull::dangling().as_ptr(), 0); } // Compliant: Uses `NonNull::dangling().as_ptr()` to avoid undefined behavior.

Resources

Documentation