Create rule S7429: Null function pointers should not be created through transmute (#4770)

* Create rule S7429

* 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>
This commit is contained in:
github-actions[bot] 2025-03-19 13:06:59 +00:00 committed by GitHub
parent ef9ace8117
commit 31e8111116
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,24 @@
{
"title": "Null function pointers should not be created through `transmute`",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7429",
"sqKey": "S7429",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,24 @@
== Why is this an issue?
Creating a null function pointer using `transmute` results in undefined behavior, as null function pointers are not valid in Rust. Instead, use `Option<fn()>` to safely represent a nullable function pointer.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
let null_fn: fn() = unsafe { std::mem::transmute(std::ptr::null::<()>()) }; // Noncompliant
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
let null_fn: Option<fn()> = None; // Compliant
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#transmute_null_to_fn