Compare commits

...

2 Commits

Author SHA1 Message Date
yassin-kammoun-sonarsource
50343c7c59 Update RSPEC 2025-03-27 10:01:14 +01:00
yassin-kammoun-sonarsource
0b51ec0a2d Create rule S7462 2025-03-27 08:45:46 +00:00
3 changed files with 67 additions and 0 deletions

View File

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

View File

@ -0,0 +1,24 @@
{
"title": "`mem::uninitialized` and `mem::zeroed()` should not be used to replace values",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7462",
"sqKey": "S7462",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,41 @@
== Why is this an issue?
Using ``++mem::replace(&mut _, mem::uninitialized())++`` or ``++mem::replace(&mut _, mem::zeroed())++`` leads to undefined behavior even if the value is overwritten later. This is because the uninitialized value might be observed in the case of a panic, which can lead to unpredictable and dangerous consequences in your program.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
use std::mem;
fn may_panic(v: Vec<i32>) -> Vec<i32> { v }
#[allow(deprecated, invalid_value)]
fn myfunc(v: &mut Vec<i32>) {
let taken_v = unsafe { mem::replace(v, mem::uninitialized()) }; // Noncompliant
let new_v = may_panic(taken_v); // undefined behavior on panic
mem::forget(mem::replace(v, new_v));
}
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
use std::mem;
use take_mut::take;
fn may_panic(v: Vec<i32>) -> Vec<i32> { v }
fn myfunc(v: &mut Vec<i32>) {
let new_v = take(v, |old_v| may_panic(old_v)); // Compliant
mem::forget(mem::replace(v, new_v));
}
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_uninit