Update RSPEC

This commit is contained in:
yassin-kammoun-sonarsource 2025-03-27 09:56:00 +01:00
parent 0b51ec0a2d
commit 50343c7c59
2 changed files with 28 additions and 32 deletions

View File

@ -1,12 +1,13 @@
{
"title": "FIXME",
"type": "CODE_SMELL",
"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",
@ -16,10 +17,8 @@
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM",
"SECURITY": "LOW"
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
"attribute": "LOGICAL"
}
}

View File

@ -1,16 +1,6 @@
FIXME: add a description
// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]
== Why is this an issue?
FIXME: remove the unused optional headers (that are commented out)
//=== What is the potential impact?
== How to fix it
//== How to fix it in FRAMEWORK NAME
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
@ -18,27 +8,34 @@ FIXME: remove the unused optional headers (that are commented out)
[source,rust,diff-id=1,diff-type=noncompliant]
----
FIXME
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]
----
FIXME
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));
}
----
//=== How does this work?
== Resources
=== Documentation
//=== Pitfalls
//=== Going the extra mile
//== Resources
//=== Documentation
//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== External coding guidelines
//=== Benchmarks
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_uninit