diff --git a/rules/S7463/rust/metadata.json b/rules/S7463/rust/metadata.json index 9f325b027d..96d00e665a 100644 --- a/rules/S7463/rust/metadata.json +++ b/rules/S7463/rust/metadata.json @@ -1,12 +1,13 @@ { - "title": "FIXME", - "type": "CODE_SMELL", + "title": "`saturating_sub` should be used to avoid subtraction underflow", + "type": "BUG", "status": "ready", "remediation": { "func": "Constant\/Issue", "constantCost": "5min" }, "tags": [ + "clippy" ], "defaultSeverity": "Major", "ruleSpecification": "RSPEC-7463", @@ -16,10 +17,8 @@ "quickfix": "unknown", "code": { "impacts": { - "MAINTAINABILITY": "HIGH", - "RELIABILITY": "MEDIUM", - "SECURITY": "LOW" + "RELIABILITY": "MEDIUM" }, - "attribute": "CONVENTIONAL" + "attribute": "LOGICAL" } } diff --git a/rules/S7463/rust/rule.adoc b/rules/S7463/rust/rule.adoc index 368fb2954e..1705fad7f5 100644 --- a/rules/S7463/rust/rule.adoc +++ b/rules/S7463/rust/rule.adoc @@ -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 conditional subtraction ``++if a > b { b - a } else { 0 }++`` can lead to an unintended underflow, which can cause bugs or unexpected behaviors. The ``++saturating_sub++`` method ensures that the subtraction does not underflow by returning zero if the result would have been negative. === Code examples @@ -18,27 +8,21 @@ FIXME: remove the unused optional headers (that are commented out) [source,rust,diff-id=1,diff-type=noncompliant] ---- -FIXME +let a = 12u32; +let b = 13u32; +let result = if a > b { b - a } else { 0 }; // Noncompliant: Potential underflow condition. ---- ==== Compliant solution [source,rust,diff-id=1,diff-type=compliant] ---- -FIXME +let a = 12u32; +let b = 13u32; +let result = a.saturating_sub(b); // Compliant: Safe subtraction using saturating_sub. ---- -//=== 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#inverted_saturating_sub