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>
This commit is contained in:
github-actions[bot] 2025-03-19 13:07:03 +00:00 committed by GitHub
parent 31e8111116
commit e1ee16f500
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 pointers should not be transmuted",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7427",
"sqKey": "S7427",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "HIGH"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,24 @@
== 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