Create rule S7444: checked_add
and overflowing_add
should be used to prevent overflows (#4788)
* Create rule S7444 * 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:
parent
cb4497e15d
commit
0360734651
2
rules/S7444/metadata.json
Normal file
2
rules/S7444/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7444/rust/metadata.json
Normal file
24
rules/S7444/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"title": "`checked_add` and `overflowing_add` should be used to prevent overflows",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7444",
|
||||
"sqKey": "S7444",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "MEDIUM"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
34
rules/S7444/rust/rule.adoc
Normal file
34
rules/S7444/rust/rule.adoc
Normal file
@ -0,0 +1,34 @@
|
||||
== Why is this an issue?
|
||||
|
||||
C-style underflow/overflow checks will panic in debug builds. Using safe methods like `checked_add` or `overflowing_add` ensures that overflow conditions are explicitly handled, preventing unexpected panics.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
let a = 1i32;
|
||||
let b = 2i32;
|
||||
if a + b < a {
|
||||
// Noncompliant: This will panic in debug builds
|
||||
// handle overflow
|
||||
}
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
let a = 1i32;
|
||||
let b = 2i32;
|
||||
if a.checked_add(b).is_none() {
|
||||
// Compliant: Explicitly handle overflow
|
||||
// handle overflow
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#panicking_overflow_checks
|
Loading…
x
Reference in New Issue
Block a user