Skip to content

Commit

Permalink
Modify rule S1696: promote C# to SonarWay (#3693)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristian-ambrosini-sonarsource authored Feb 27, 2024
1 parent e6ecfb4 commit cbe555b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 47 deletions.
69 changes: 25 additions & 44 deletions rules/S1696/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,70 +1,51 @@
== Why is this an issue?

``++NullReferenceException++`` should be avoided, not caught. Any situation in which ``++NullReferenceException++`` is explicitly caught can easily be converted to a ``++null++`` test, and any behavior being carried out in the catch block can easily be moved to the "is null" branch of the conditional.
Catching `NullReferenceException` is generally considered a bad practice because it can hide bugs in your code. Instead of catching this exception, you should aim to prevent it. This makes your code more robust and easier to understand.
In addition, constantly catching and handling `NullReferenceException` can lead to performance issues. Exceptions are expensive in terms of system resources, so they should be used cautiously and only for exceptional conditions, not for regular control flow.

=== Noncompliant code example
== How to fix it

[source,csharp]
Instead of catching NullReferenceException, it's better to prevent it from happening in the first place. You can do this by using null checks or null conditional operators (`?.`) before accessing members of an object.

=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
public int GetLengthPlusTwo(string str)
{
int length = 2;
try
try
{
length += str.Length;
return str.Length + 2;
}
catch (NullReferenceException e)
catch (NullReferenceException e)
{
log.info("argument was null");
return 2;
}
return length;
}
----

=== Compliant solution
==== Compliant solution

[source,csharp]
[source,csharp,diff-id=1,diff-type=compliant]
----
public int GetLengthPlusTwo(string str)
public int GetLengthPlusTwo(string str)
{
int length = 2;
if (str != null)
if (str is null)
{
length += str.Length;
return 2;
}
else
{
log.info("argument was null");
}
return length;
return str.Length + 2;
}
----

== Resources

* CWE - https://cwe.mitre.org/data/definitions/395[CWE-395 - Use of NullPointerException Catch to Detect NULL Pointer Dereference]

ifdef::env-github,rspecator-view[]
=== Documentation

'''
== Implementation Specification
(visible only on this page)

=== Message

Do not catch NullReferenceException; test for null instead.


=== Highlighting

throw new NullReferenceException()


'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]
* CWE - https://cwe.mitre.org/data/definitions/395[CWE-395 - Use of NullPointerException Catch to Detect NULL Pointer Dereference]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.nullreferenceexception[NullReferenceException class]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-[Null-conditional operators ?. and ?[]]

endif::env-github,rspecator-view[]
include::../rspecator.adoc[]
5 changes: 4 additions & 1 deletion rules/S1696/java/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"CWE": [
395
]
}
},
"defaultQualityProfiles": [

]
}
4 changes: 2 additions & 2 deletions rules/S1696/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
]
},
"defaultQualityProfiles": [

"Sonar way"
],
"quickfix": "unknown"
"quickfix": "infeasible"
}
21 changes: 21 additions & 0 deletions rules/S1696/rspecator.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Do not catch NullReferenceException; test for null instead.

=== Highlighting

throw new NullReferenceException()

'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]

endif::env-github,rspecator-view[]

0 comments on commit cbe555b

Please sign in to comment.