Skip to content

Commit 82c49ad

Browse files
committed
SONARJAVA-1515 Make call to system exit sink of execution
1 parent 0a4120d commit 82c49ad

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

java-frontend/src/main/java/org/sonar/java/se/ExplodedGraphWalker.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.slf4j.LoggerFactory;
3131
import org.sonar.java.cfg.CFG;
3232
import org.sonar.java.cfg.LiveVariables;
33+
import org.sonar.java.matcher.MethodMatcher;
3334
import org.sonar.java.model.JavaTree;
3435
import org.sonar.java.se.checks.ConditionAlwaysTrueOrFalseCheck;
3536
import org.sonar.java.se.checks.LocksNotUnlockedCheck;
@@ -89,6 +90,7 @@ public class ExplodedGraphWalker extends BaseTreeVisitor {
8990

9091
private static final boolean DEBUG_MODE_ACTIVATED = false;
9192
private static final int MAX_EXEC_PROGRAM_POINT = 2;
93+
private static final MethodMatcher SYSTEM_EXIT_MATCHER = MethodMatcher.create().typeDefinition("java.lang.System").name("exit").addParameter("int");
9294
private final ConditionAlwaysTrueOrFalseCheck alwaysTrueOrFalseChecker;
9395
private MethodTree methodTree;
9496
private ExplodedGraph explodedGraph;
@@ -353,7 +355,12 @@ private void visit(Tree tree, @Nullable Tree terminator) {
353355
}
354356
switch (tree.kind()) {
355357
case METHOD_INVOCATION:
356-
executeMethodInvocation((MethodInvocationTree) tree);
358+
MethodInvocationTree mit = (MethodInvocationTree) tree;
359+
if(SYSTEM_EXIT_MATCHER.matches(mit)) {
360+
//System exit is a sink of execution
361+
return;
362+
}
363+
executeMethodInvocation(mit);
357364
break;
358365
case LABELED_STATEMENT:
359366
case SWITCH_STATEMENT:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class A {
2+
void foo() {
3+
boolean a = true;
4+
System.exit(-1);
5+
if (a) {
6+
7+
}
8+
}
9+
10+
void bar(boolean a) {
11+
if (a) {
12+
System.exit(-1);
13+
}
14+
if(a) { // Noncompliant {{Change this condition so that it does not always evaluate to "false"}}
15+
}
16+
}
17+
}

java-frontend/src/test/java/org/sonar/java/se/ExplodedGraphWalkerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ public void visitNode(Tree tree) {
118118
});
119119
}
120120

121+
122+
@Test
123+
public void system_exit() throws Exception {
124+
JavaCheckVerifier.verify("src/test/files/se/SystemExit.java", new IssueVisitor());
125+
}
126+
121127
class IssueVisitor implements JavaFileScanner {
122128

123129
@Override

0 commit comments

Comments
 (0)