Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| avoid-raising-null-pointer-exceptions-when-a-null-check-is-possible.java:6:11:6:40 | catch (...) | This catch-clause handles NullPointerException. Consider using null checks instead of relying on exception handling to avoid performance overhead. |
| avoid-raising-null-pointer-exceptions-when-a-null-check-is-possible.java:14:11:14:40 | catch (...) | This catch-clause handles NullPointerException. Consider using null checks instead of relying on exception handling to avoid performance overhead. |
| avoid-raising-null-pointer-exceptions-when-a-null-check-is-possible.java:22:11:22:74 | catch (...) | This catch-clause handles NullPointerException. Consider using null checks instead of relying on exception handling to avoid performance overhead. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class nonCompliantNullCheck {
void nullCheckTest(String str) {
try {
String part = str.substring(5); // Noncompliant
System.out.println(part);
} catch (NullPointerException e) {
System.out.println("String is null");
}
}

void anotherNullCheckTest(Object obj) {
try {
obj.toString(); // Noncompliant
} catch (NullPointerException e) {
System.out.println("Object is null");
}
}

void multiCatchTest(String str) {
try {
int length = str.length(); // Noncompliant
} catch (NullPointerException | StringIndexOutOfBoundsException e) {
System.out.println("Error occurred");
}
}
}

class compliantNullCheck {
void nullCheckTest(String str) {
if (str != null && str.length() > 5) { // Compliant
String part = str.substring(5);
System.out.println(part);
} else {
System.out.println("String is null or too short");
}
}

void anotherNullCheckTest(Object obj) {
if (obj != null) { // Compliant
obj.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @name Avoid raising NullPointerExceptions when a null check is possible
* @description Identifies try-catch blocks that handle NullPointerException. Null checks should be performed to avoid NullPointerExceptions rather than relying on exception handling.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id java/avoid-raising-null-pointer-exceptions-when-a-null-check-is-possible
* @tags java/lang/avoid-raising-null-pointer-exceptions-when-a-null-check-is-possible
*/

import java

pragma[nomagic]
predicate isNullPointerException(RefType rt) {
rt.hasQualifiedName("java.lang", "NullPointerException")
}

private RefType caughtType(TryStmt try, int index) {
exists(CatchClause cc | cc = try.getCatchClause(index) |
if cc.isMultiCatch()
then result = cc.getVariable().getTypeAccess().(UnionTypeAccess).getAnAlternative().getType()
else result = cc.getVariable().getType()
)
}

from TryStmt try, int index, RefType npeType
where
try.getFile().isJavaSourceFile() and
npeType = caughtType(try, index) and
isNullPointerException(npeType)
select try.getCatchClause(index),
"This catch-clause handles NullPointerException. Consider using null checks instead of relying on exception handling to avoid performance overhead."
Loading