diff --git a/rules/S7444/metadata.json b/rules/S7444/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S7444/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7444/rust/metadata.json b/rules/S7444/rust/metadata.json new file mode 100644 index 0000000000..5143017127 --- /dev/null +++ b/rules/S7444/rust/metadata.json @@ -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" + } +} diff --git a/rules/S7444/rust/rule.adoc b/rules/S7444/rust/rule.adoc new file mode 100644 index 0000000000..d6a3e9ff10 --- /dev/null +++ b/rules/S7444/rust/rule.adoc @@ -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