rspec/rules/S7427/rust/rule.adoc
github-actions[bot] e1ee16f500
Create rule S7427: Null pointers should not be transmuted (#4768)
* Create rule S7427

* Update RSPEC

---------

Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com>
Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
2025-03-19 13:07:03 +00:00

25 lines
674 B
Plaintext

== Why is this an issue?
Transmuting a null pointer is undefined behavior and can lead to unpredictable program crashes and security vulnerabilities.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
let null_ref: &u64 = unsafe { std::mem::transmute(0 as *const u64) }; // Noncompliant: Transmuting a null pointer.
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
let null_ref: Option<&u64> = None; // Compliant: Use Option to represent nullable references.
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#transmuting_null