Create rule S6466: Accessing an array element should not trigger a panic (#4684)
* Add rust to rule S6466 * Update RSPEC * Remove tag --------- 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
5def9014ed
commit
aa593087ba
24
rules/S6466/rust/metadata.json
Normal file
24
rules/S6466/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"title": "Accessing an array element should not trigger a panic",
|
||||||
|
"type": "BUG",
|
||||||
|
"code": {
|
||||||
|
"impacts": {
|
||||||
|
"RELIABILITY": "HIGH"
|
||||||
|
},
|
||||||
|
"attribute": "LOGICAL"
|
||||||
|
},
|
||||||
|
"status": "ready",
|
||||||
|
"remediation": {
|
||||||
|
"func": "Constant\/Issue",
|
||||||
|
"constantCost": "10min"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"clippy"
|
||||||
|
],
|
||||||
|
"defaultSeverity": "Critical",
|
||||||
|
"ruleSpecification": "RSPEC-6466",
|
||||||
|
"sqKey": "S6466",
|
||||||
|
"scope": "All",
|
||||||
|
"defaultQualityProfiles": ["Sonar way"],
|
||||||
|
"quickfix": "unknown"
|
||||||
|
}
|
37
rules/S6466/rust/rule.adoc
Normal file
37
rules/S6466/rust/rule.adoc
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
== Why is this an issue?
|
||||||
|
|
||||||
|
An array index out-of-bounds panic is a bug class that occurs in Rust when a
|
||||||
|
program tries to access an array element that does not exist.
|
||||||
|
This bug can cause your program to crash or behave unexpectedly.
|
||||||
|
|
||||||
|
include::../impact.adoc[]
|
||||||
|
|
||||||
|
== How to fix it
|
||||||
|
|
||||||
|
To fix an array index out of bounds panic in Rust, you should always ensure
|
||||||
|
that you are accessing array elements within the bounds of the array.
|
||||||
|
|
||||||
|
=== Code examples
|
||||||
|
|
||||||
|
==== Noncompliant code example
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||||
|
----
|
||||||
|
let x = [1, 2, 3, 4];
|
||||||
|
|
||||||
|
x[9]; // Out of bounds indexing
|
||||||
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||||||
|
----
|
||||||
|
let x = [1, 2, 3, 4];
|
||||||
|
|
||||||
|
x[0];
|
||||||
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
=== Documentation
|
||||||
|
|
||||||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#out_of_bounds_indexing
|
Loading…
x
Reference in New Issue
Block a user