Compare commits
2 Commits
master
...
rule/add-R
Author | SHA1 | Date | |
---|---|---|---|
![]() |
50343c7c59 | ||
![]() |
0b51ec0a2d |
2
rules/S7462/metadata.json
Normal file
2
rules/S7462/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
24
rules/S7462/rust/metadata.json
Normal file
24
rules/S7462/rust/metadata.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
41
rules/S7462/rust/rule.adoc
Normal file
41
rules/S7462/rust/rule.adoc
Normal 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
|
Loading…
x
Reference in New Issue
Block a user