From 4d428aad9671fcbca5ae1c5f07a18eb490f28d63 Mon Sep 17 00:00:00 2001 From: Ivan Galkin Date: Tue, 27 Nov 2018 15:42:49 +0100 Subject: [PATCH] Eliminate wrongly used static variables There is no need to share state between classes. This is error-prone and prohibits any possible parallelization of code analysis * BullseyeParser * HardcodedAccountCheck - refactored * HardcodedIpCheck - refactored --- .../cxx/checks/HardcodedAccountCheck.java | 34 +++++++----------- .../sonar/cxx/checks/HardcodedIpCheck.java | 35 ++++++++----------- .../cxx/sensors/coverage/BullseyeParser.java | 26 +++++++------- 3 files changed, 41 insertions(+), 54 deletions(-) diff --git a/cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedAccountCheck.java b/cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedAccountCheck.java index b564b42f71..4cedd7dee4 100644 --- a/cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedAccountCheck.java +++ b/cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedAccountCheck.java @@ -24,6 +24,8 @@ import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.check.RuleProperty; @@ -55,45 +57,35 @@ public class HardcodedAccountCheck extends SquidCheck { * */ private static final String DEFAULT_REGULAR_EXPRESSION = "\\bDSN\\b.*=.*;\\b(UID|PWD)\\b=.*;"; - private static Matcher reg; - /** - * regularExpression - * - */ @RuleProperty( key = "regularExpression", description = "literal regular expression rule", defaultValue = DEFAULT_REGULAR_EXPRESSION) - public String regularExpression = DEFAULT_REGULAR_EXPRESSION; + private String regularExpression = DEFAULT_REGULAR_EXPRESSION; - public String getRegularExpression() { - return regularExpression; - } + private Pattern pattern; - @SuppressWarnings("squid:S2696") // ... initialize SquidAstVisitor @Override public void init() { - String regEx = getRegularExpression(); Objects.requireNonNull(regularExpression, "getRegularExpression() should not return null"); - if (null != regEx && !regEx.isEmpty()) { + if (!regularExpression.isEmpty()) { try { - reg = Pattern.compile(regEx).matcher(""); - } catch (RuntimeException e) { - throw new IllegalStateException("Unable to compile regular expression: " + regEx, e); + pattern = Pattern.compile(regularExpression); + } catch (PatternSyntaxException e) { + throw new IllegalStateException("Unable to compile regular expression: " + regularExpression, e); } + subscribeTo(CxxGrammarImpl.LITERAL); } - subscribeTo(CxxGrammarImpl.LITERAL); } @Override public void visitNode(AstNode node) { - if (node.is(CxxGrammarImpl.LITERAL)) { - reg.reset(node.getTokenOriginalValue().replaceAll("\\s", "")); - if (reg.find()) { - getContext().createLineViolation(this, "Do not hard code sensitive data in programs.", node); - } + final String tokenValue = node.getTokenOriginalValue().replaceAll("\\s", ""); + final Matcher matcher = pattern.matcher(tokenValue); + if (matcher.find()) { + getContext().createLineViolation(this, "Do not hard code sensitive data in programs.", node); } } } diff --git a/cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedIpCheck.java b/cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedIpCheck.java index 455cbfdec4..3f72e916c2 100644 --- a/cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedIpCheck.java +++ b/cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedIpCheck.java @@ -24,6 +24,8 @@ import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.check.RuleProperty; @@ -54,42 +56,35 @@ public class HardcodedIpCheck extends SquidCheck { // (?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?))(?::(\d{2,5}))?(?:\s|$) private static final String DEFAULT_REGULAR_EXPRESSION = "^.*((? cover parser.parse(report); } - private static void collectCoverageLeafNodes(String refPath, SMInputCursor folder, + private void collectCoverageLeafNodes(String refPath, SMInputCursor folder, final Map coverageData) throws XMLStreamException { @@ -80,7 +80,7 @@ private static void collectCoverageLeafNodes(String refPath, SMInputCursor folde } } - private static void recTreeTopWalk(File fileName, SMInputCursor folder, + private void recTreeTopWalk(File fileName, SMInputCursor folder, final Map coverageData) throws XMLStreamException { SMInputCursor child = folder.childElementCursor(); @@ -92,7 +92,7 @@ private static void recTreeTopWalk(File fileName, SMInputCursor folder, } } - private static void collectCoverage2(String refPath, SMInputCursor folder, + private void collectCoverage2(String refPath, SMInputCursor folder, final Map coverageData) throws XMLStreamException { @@ -107,7 +107,7 @@ private static void collectCoverage2(String refPath, SMInputCursor folder, } } - private static void probWalk(SMInputCursor prob, CoverageMeasures fileMeasuresBuilderIn) throws XMLStreamException { + private void probWalk(SMInputCursor prob, CoverageMeasures fileMeasuresBuilderIn) throws XMLStreamException { String line = prob.getAttrValue("line"); String kind = prob.getAttrValue("kind"); String event = prob.getAttrValue("event"); @@ -118,7 +118,7 @@ private static void probWalk(SMInputCursor prob, CoverageMeasures fileMeasuresBu prevLine = line; } - private static void funcWalk(SMInputCursor func, CoverageMeasures fileMeasuresBuilderIn) throws XMLStreamException { + private void funcWalk(SMInputCursor func, CoverageMeasures fileMeasuresBuilderIn) throws XMLStreamException { SMInputCursor prob = func.childElementCursor(); while (prob.getNext() != null) { probWalk(prob, fileMeasuresBuilderIn); @@ -126,14 +126,14 @@ private static void funcWalk(SMInputCursor func, CoverageMeasures fileMeasuresBu saveConditions(fileMeasuresBuilderIn); } - private static void fileWalk(SMInputCursor file, CoverageMeasures fileMeasuresBuilderIn) throws XMLStreamException { + private void fileWalk(SMInputCursor file, CoverageMeasures fileMeasuresBuilderIn) throws XMLStreamException { SMInputCursor func = file.childElementCursor(); while (func.getNext() != null) { funcWalk(func, fileMeasuresBuilderIn); } } - private static void recTreeWalk(String refPath, SMInputCursor folder, List path, + private void recTreeWalk(String refPath, SMInputCursor folder, List path, final Map coverageData) throws XMLStreamException { @@ -160,7 +160,7 @@ private static void recTreeWalk(String refPath, SMInputCursor folder, List 0) { if (totalcoveredconditions == 0) { fileMeasuresBuilderIn.setHits(Integer.parseInt(prevLine), 0); @@ -173,7 +173,7 @@ private static void saveConditions(CoverageMeasures fileMeasuresBuilderIn) { totalcoveredconditions = 0; } - private static void updateMeasures(String kind, String event, String line, CoverageMeasures fileMeasuresBuilderIn) { + private void updateMeasures(String kind, String event, String line, CoverageMeasures fileMeasuresBuilderIn) { switch (kind.toLowerCase(Locale.ENGLISH)) { case "decision": @@ -207,7 +207,7 @@ private static void updateMeasures(String kind, String event, String line, Cover /** * @param event */ - private static void setTotalCoveredConditions(String event) { + private void setTotalCoveredConditions(String event) { switch (event.toLowerCase(Locale.ENGLISH)) { case "full": totalcoveredconditions += 2;