-
Notifications
You must be signed in to change notification settings - Fork 724
SONARJAVA-6722 Implement S9142: Expensive compilation or preparation operations should not be performed inside loops #5893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
850fc56
f5ef064
cee8cea
15b7b16
b82ea91
8771e5c
c22a79d
9024910
ba917e1
a3a2611
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "ruleKey": "S9142", | ||
| "hasTruePositives": true, | ||
| "falseNegatives": 0, | ||
| "falsePositives": 0 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package checks; | ||
|
|
||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| class CompilationOrPreparationInLoopCheckSampleNonCompiling { | ||
| void test(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| Pattern.compile(unknownVar).matcher(input).find(); // Compliant - unresolved symbol is not a variable symbol | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package checks; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| class CompilationOrPreparationInLoopCheckSample { | ||
|
|
||
| private static final String CONSTANT_PATTERN = "[a-z]+"; | ||
| private String mutablePattern = "[a-z]+"; | ||
|
|
||
| void patternCompileNoncompliant(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| Pattern.compile("[a-z]+").matcher(input).find(); // Noncompliant {{Move this "compile" call outside the loop.}} | ||
| //^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| } | ||
|
|
||
| int i = 0; | ||
| while (i++ < inputs.size()) { | ||
| Pattern.compile("[a-z]+"); // Noncompliant | ||
| } | ||
|
|
||
| for (String input : inputs) { | ||
| Pattern.compile(CONSTANT_PATTERN).matcher(input).find(); // Noncompliant | ||
| } | ||
|
|
||
| String invariantPattern = "[a-z]+"; | ||
| for (String input : inputs) { | ||
| Pattern.compile(invariantPattern).matcher(input).find(); // Noncompliant | ||
| } | ||
|
|
||
| for (String input : inputs) { | ||
| Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE); // Noncompliant | ||
| int flags = input.isEmpty() ? 0 : Pattern.CASE_INSENSITIVE; | ||
| Pattern.compile("[a-z]+", flags); // Compliant: flags vary | ||
| } | ||
| } | ||
|
|
||
| void stringMethodsNoncompliant(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| input.matches("[a-z]+"); // Noncompliant {{Extract this regular expression to a Pattern compiled outside the loop.}} | ||
| input.replaceAll("[a-z]+", "X"); // Noncompliant | ||
| input.replaceFirst("[a-z]+", "X"); // Noncompliant | ||
| input.split("[,;]"); // Noncompliant | ||
| input.split("."); // Noncompliant | ||
| input.split("\\a"); // Noncompliant | ||
| input.split(","); // Compliant: single non-metacharacter fast path | ||
| input.split("\\."); // Compliant: escaped non-alphanumeric fast path | ||
| } | ||
| } | ||
|
|
||
| void prepareStatementNoncompliant(Connection conn, List<Integer> ids) throws SQLException { | ||
| for (int id : ids) { | ||
| PreparedStatement ps = conn.prepareStatement("SELECT * FROM t WHERE id = ?"); // Noncompliant | ||
| ps.setInt(1, id); | ||
| ps.execute(); | ||
| ps.close(); | ||
| } | ||
| } | ||
|
|
||
| void forInitializer(String s) { | ||
| for (Pattern p = Pattern.compile("[a-z]+"); p.matcher(s).find(); ) { // Compliant: initializer runs once | ||
| break; | ||
| } | ||
| for (; Pattern.compile("[a-z]+").matcher(s).find(); ) { // Noncompliant | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| void compliant(List<String> inputs, Connection conn, List<Integer> ids) throws SQLException { | ||
| Pattern p = Pattern.compile("[a-z]+"); | ||
| for (String input : inputs) { | ||
| p.matcher(input).find(); | ||
| } | ||
|
|
||
| for (String input : inputs) { | ||
| input.toLowerCase(); // not a regex method | ||
| } | ||
|
|
||
| PreparedStatement ps = conn.prepareStatement("SELECT * FROM t WHERE id = ?"); | ||
| for (int id : ids) { | ||
| ps.setInt(1, id); | ||
| ps.execute(); | ||
| } | ||
| } | ||
|
|
||
| void patternVariesPerIteration(List<String> patterns, List<String> inputs) { | ||
| for (int i = 0; i < inputs.size(); i++) { | ||
| String pattern = patterns.get(i); | ||
| Pattern.compile(pattern).matcher(inputs.get(i)).find(); // Compliant - pattern changes per iteration | ||
| } | ||
| } | ||
|
|
||
| void mutableFieldPattern(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| Pattern.compile(mutablePattern).matcher(input).find(); // Compliant - non-final field may be mutated via member select or method call | ||
| } | ||
| } | ||
|
|
||
| void localReassignedInLoop(List<String> inputs) { | ||
| String pattern = "[a-z]+"; | ||
| for (String input : inputs) { | ||
| pattern = input; // reassigned each iteration | ||
| Pattern.compile(pattern).matcher(input).find(); // Compliant - pattern changes per iteration | ||
| } | ||
| } | ||
|
|
||
| void doWhileLoop(List<String> inputs) { | ||
| int i = 0; | ||
| do { | ||
| Pattern.compile("[a-z]+").matcher(inputs.get(i)).find(); // Noncompliant | ||
| } while (i++ < inputs.size()); | ||
| } | ||
|
|
||
| void nonConstantArg(List<String> inputs) { | ||
| for (String input : inputs) { | ||
| Pattern.compile(input.trim()).matcher(input).find(); // Compliant - method call result is not a constant | ||
| } | ||
| } | ||
|
|
||
| void nonIncrementUnaryInLoop(List<String> inputs) { | ||
| boolean inverse = false; | ||
| for (String input : inputs) { | ||
| if (!inverse) { // non-increment unary expression | ||
| Pattern.compile("[a-z]+").matcher(input).find(); // Noncompliant | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void nonIdentifierMutationsInLoop(List<String> inputs) { | ||
| int[] counters = new int[2]; | ||
| for (String input : inputs) { | ||
| counters[0] = input.length(); // assignment to non-identifier target | ||
| counters[1]++; // increment on non-identifier target | ||
| Pattern.compile("[a-z]+").matcher(input).find(); // Noncompliant | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,208 @@ | ||||
| /* | ||||
| * SonarQube Java | ||||
| * Copyright (C) SonarSource Sàrl | ||||
| * mailto:info AT sonarsource DOT com | ||||
| * | ||||
| * You can redistribute and/or modify this program under the terms of | ||||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||||
| * | ||||
| * This program is distributed in the hope that it will be useful, | ||||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||
| * See the Sonar Source-Available License for more details. | ||||
| * | ||||
| * You should have received a copy of the Sonar Source-Available License | ||||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||||
| */ | ||||
| package org.sonar.java.checks; | ||||
|
|
||||
| import java.util.Collections; | ||||
| import java.util.EnumSet; | ||||
| import java.util.HashSet; | ||||
| import java.util.List; | ||||
| import java.util.Set; | ||||
| import org.apache.commons.lang3.StringEscapeUtils; | ||||
| import org.sonar.check.Rule; | ||||
| import org.sonar.java.checks.helpers.TreeHelper; | ||||
| import org.sonar.java.model.ExpressionUtils; | ||||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||||
| import org.sonar.plugins.java.api.semantic.MethodMatchers; | ||||
| import org.sonar.plugins.java.api.semantic.Symbol; | ||||
| import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; | ||||
| import org.sonar.plugins.java.api.tree.BaseTreeVisitor; | ||||
| import org.sonar.plugins.java.api.tree.ExpressionTree; | ||||
| import org.sonar.plugins.java.api.tree.ForEachStatement; | ||||
| import org.sonar.plugins.java.api.tree.ForStatementTree; | ||||
| import org.sonar.plugins.java.api.tree.IdentifierTree; | ||||
| import org.sonar.plugins.java.api.tree.MethodInvocationTree; | ||||
| import org.sonar.plugins.java.api.tree.Tree; | ||||
| import org.sonar.plugins.java.api.tree.UnaryExpressionTree; | ||||
| import org.sonar.plugins.java.api.tree.VariableTree; | ||||
|
|
||||
| @Rule(key = "S9142") | ||||
| public class CompilationOrPreparationInLoopCheck extends IssuableSubscriptionVisitor { | ||||
|
|
||||
| private static final String STRING_REGEX_MESSAGE = | ||||
| "Extract this regular expression to a Pattern compiled outside the loop."; | ||||
|
|
||||
| private static final Set<Tree.Kind> LOOP_KINDS = EnumSet.of( | ||||
| Tree.Kind.FOR_STATEMENT, Tree.Kind.FOR_EACH_STATEMENT, | ||||
| Tree.Kind.WHILE_STATEMENT, Tree.Kind.DO_STATEMENT | ||||
| ); | ||||
|
|
||||
| private static final MethodMatchers PATTERN_COMPILE = MethodMatchers.create() | ||||
| .ofTypes("java.util.regex.Pattern") | ||||
| .names("compile") | ||||
| .withAnyParameters() | ||||
| .build(); | ||||
|
|
||||
| private static final MethodMatchers STRING_REGEX_METHODS = MethodMatchers.create() | ||||
| .ofTypes("java.lang.String") | ||||
| .names("matches", "replaceAll", "replaceFirst", "split") | ||||
| .withAnyParameters() | ||||
| .build(); | ||||
|
|
||||
| private static final MethodMatchers SPLIT = MethodMatchers.create() | ||||
| .ofTypes("java.lang.String") | ||||
| .names("split") | ||||
| .withAnyParameters() | ||||
| .build(); | ||||
|
|
||||
| private static final MethodMatchers MATCHERS = MethodMatchers.or( | ||||
| PATTERN_COMPILE, | ||||
| STRING_REGEX_METHODS, | ||||
| MethodMatchers.create() | ||||
| .ofSubTypes("java.sql.Connection") | ||||
| .names("prepareStatement") | ||||
| .withAnyParameters() | ||||
| .build() | ||||
| ); | ||||
|
|
||||
| @Override | ||||
| public List<Tree.Kind> nodesToVisit() { | ||||
| return Collections.singletonList(Tree.Kind.METHOD_INVOCATION); | ||||
| } | ||||
|
|
||||
| @Override | ||||
| public void visitNode(Tree tree) { | ||||
| MethodInvocationTree mit = (MethodInvocationTree) tree; | ||||
| if (!MATCHERS.matches(mit) || mit.arguments().isEmpty()) { | ||||
| return; | ||||
| } | ||||
| Tree loop = TreeHelper.findClosestParentOfKind(mit, LOOP_KINDS); | ||||
| if (loop == null || isInForInitializer(mit, loop)) { | ||||
| return; | ||||
| } | ||||
| if (SPLIT.matches(mit) && isSplitFastPath(mit.arguments().get(0))) { | ||||
| return; | ||||
| } | ||||
| List<ExpressionTree> argsToCheck = PATTERN_COMPILE.matches(mit) ? mit.arguments() : List.of(mit.arguments().get(0)); | ||||
| if (argsToCheck.stream().allMatch(arg -> isLoopInvariant(arg, loop))) { | ||||
| reportIssue(mit, message(mit)); | ||||
| } | ||||
| } | ||||
|
|
||||
| private static String message(MethodInvocationTree mit) { | ||||
| if (STRING_REGEX_METHODS.matches(mit)) { | ||||
| return STRING_REGEX_MESSAGE; | ||||
| } | ||||
| return String.format("Move this \"%s\" call outside the loop.", ExpressionUtils.methodName(mit).name()); | ||||
| } | ||||
|
|
||||
| private static boolean isInForInitializer(Tree tree, Tree loop) { | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FP found by an LLM: The iterable expression in an enhanced for-loop (e.g. Because |
||||
| if (!loop.is(Tree.Kind.FOR_STATEMENT)) { | ||||
| return false; | ||||
| } | ||||
| Tree initializer = ((ForStatementTree) loop).initializer(); | ||||
| for (Tree current = tree; current != null && current != loop; current = current.parent()) { | ||||
| if (current == initializer) { | ||||
| return true; | ||||
| } | ||||
| } | ||||
| return false; | ||||
| } | ||||
|
|
||||
| private static boolean isSplitFastPath(ExpressionTree arg) { | ||||
| return ExpressionUtils.skipParentheses(arg).asConstant(String.class) | ||||
| .filter(CompilationOrPreparationInLoopCheck::exceptionSplitMethod) | ||||
| .isPresent(); | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Copy of {@link java.lang.String#split(String, int)} fast-path, matching {@link RegexPatternsNeedlesslyCheck}. | ||||
| */ | ||||
| private static boolean exceptionSplitMethod(String argValue) { | ||||
| String regex = StringEscapeUtils.unescapeJava(argValue); | ||||
| char ch; | ||||
| if (regex.length() == 1) { | ||||
| ch = regex.charAt(0); | ||||
| return ".$|()[{^?*+\\".indexOf(ch) == -1 && | ||||
| (ch < Character.MIN_HIGH_SURROGATE || ch > Character.MAX_LOW_SURROGATE); | ||||
| } | ||||
| if (regex.length() == 2 && regex.charAt(0) == '\\') { | ||||
| ch = regex.charAt(1); | ||||
| return (((ch - '0') | ('9' - ch)) < 0 && | ||||
| ((ch - 'a') | ('z' - ch)) < 0 && | ||||
| ((ch - 'A') | ('Z' - ch)) < 0) && | ||||
| (ch < Character.MIN_HIGH_SURROGATE || ch > Character.MAX_LOW_SURROGATE); | ||||
| } | ||||
| return false; | ||||
| } | ||||
|
|
||||
| private static boolean isLoopInvariant(ExpressionTree arg, Tree loop) { | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking for loop invariants sounds like something that would be nice to extract as a common helper, I checked and rule sonar-java/java-checks/src/main/java/org/sonar/java/checks/PreparedStatementLoopInvariantCheck.java Line 93 in c9f819a
|
||||
| ExpressionTree expression = ExpressionUtils.skipParentheses(arg); | ||||
| if (expression.is(Tree.Kind.IDENTIFIER)) { | ||||
| Symbol symbol = ((IdentifierTree) expression).symbol(); | ||||
| if (!symbol.isVariableSymbol()) { | ||||
| return false; | ||||
| } | ||||
| if (symbol.owner().isTypeSymbol()) { | ||||
| return symbol.isFinal(); | ||||
| } | ||||
| var collector = new DeclaredOrAssignedLocalsCollector(); | ||||
| loop.accept(collector); | ||||
| return !collector.names.contains(((IdentifierTree) expression).name()); | ||||
| } | ||||
| return ExpressionUtils.resolveAsConstant(expression) != null; | ||||
| } | ||||
|
|
||||
| private static class DeclaredOrAssignedLocalsCollector extends BaseTreeVisitor { | ||||
|
|
||||
| final Set<String> names = new HashSet<>(); | ||||
|
|
||||
| @Override | ||||
| public void visitVariable(VariableTree tree) { | ||||
|
gitar-bot[bot] marked this conversation as resolved.
|
||||
| super.visitVariable(tree); | ||||
| names.add(tree.simpleName().name()); | ||||
| } | ||||
|
|
||||
| @Override | ||||
| public void visitAssignmentExpression(AssignmentExpressionTree tree) { | ||||
| super.visitAssignmentExpression(tree); | ||||
| if (tree.variable().is(Tree.Kind.IDENTIFIER)) { | ||||
| names.add(((IdentifierTree) tree.variable()).name()); | ||||
| } | ||||
| } | ||||
|
|
||||
| @Override | ||||
| public void visitUnaryExpression(UnaryExpressionTree tree) { | ||||
| super.visitUnaryExpression(tree); | ||||
| switch (tree.kind()) { | ||||
| case POSTFIX_INCREMENT, POSTFIX_DECREMENT, PREFIX_INCREMENT, PREFIX_DECREMENT -> { | ||||
| if (tree.expression().is(Tree.Kind.IDENTIFIER)) { | ||||
| names.add(((IdentifierTree) tree.expression()).name()); | ||||
| } | ||||
| } | ||||
| default -> { | ||||
| // not a mutation | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| @Override | ||||
| public void visitForEachStatement(ForEachStatement tree) { | ||||
| super.visitForEachStatement(tree); | ||||
| names.add(tree.variable().simpleName().name()); | ||||
| } | ||||
| } | ||||
| } | ||||
Uh oh!
There was an error while loading. Please reload this page.