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:
github-actions[bot] 2025-03-19 13:06:24 +00:00 committed by GitHub
parent cb4497e15d
commit 0360734651
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 0 deletions

View File

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

View 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"
}
}

View 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