diff --git a/rules/S7446/metadata.json b/rules/S7446/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S7446/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7446/rust/metadata.json b/rules/S7446/rust/metadata.json new file mode 100644 index 0000000000..84c08b2fc1 --- /dev/null +++ b/rules/S7446/rust/metadata.json @@ -0,0 +1,24 @@ +{ + "title": "Functions expecting raw pointer arguments should be marked as unsafe", + "type": "BUG", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "clippy" + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-7446", + "sqKey": "S7446", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "RELIABILITY": "MEDIUM" + }, + "attribute": "LOGICAL" + } +} diff --git a/rules/S7446/rust/rule.adoc b/rules/S7446/rust/rule.adoc new file mode 100644 index 0000000000..8c019ce3bd --- /dev/null +++ b/rules/S7446/rust/rule.adoc @@ -0,0 +1,37 @@ +== Why is this an issue? + +For any raw pointer passed as an argument, it is not possible to guarantee its validity. Dereferencing an invalid pointer can lead to undefined behavior, causing potential segmentation faults or other critical issues. By marking such functions as `unsafe`, it notifies the caller that they need to ensure the pointer's validity and surround the function call with an `unsafe` block to acknowledge the potential risks involved. + +=== Code examples + +==== Noncompliant code example + +[source,rust,diff-id=1,diff-type=noncompliant] +---- +pub fn foo(x: *const u8) { + println!("{}", unsafe { *x }); +} + +// This call "looks" safe but will segfault or worse! +// foo(invalid_ptr); +---- + +==== Compliant solution + +[source,rust,diff-id=1,diff-type=compliant] +---- +pub unsafe fn foo(x: *const u8) { + println!("{}", unsafe { *x }); +} + +// This would cause a compiler error for calling without `unsafe` +// foo(invalid_ptr); + +// Sound call if the caller knows the pointer is valid +unsafe { foo(valid_ptr); } +---- + +== Resources +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref