Skip to content

Commit

Permalink
Merge pull request #1605 from ivangalkin/eliminate_static
Browse files Browse the repository at this point in the history
Eliminate wrongly used static variables
  • Loading branch information
guwirth authored Nov 28, 2018
2 parents 1b9362c + 4d428aa commit ee91258
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,45 +57,35 @@ public class HardcodedAccountCheck extends SquidCheck<Grammar> {
*
*/
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);
}
}
}
35 changes: 15 additions & 20 deletions cxx-checks/src/main/java/org/sonar/cxx/checks/HardcodedIpCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,42 +56,35 @@ public class HardcodedIpCheck extends SquidCheck<Grammar> {
// (?: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
= "^.*((?<![\\d|\\.])(?:\\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b\\.){3}\\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b(?!\\d|\\.)).*$";
private static Matcher IP;
private Pattern pattern;

@RuleProperty(
key = "regularExpression",
description = "The regular expression",
defaultValue = DEFAULT_REGULAR_EXPRESSION)
public String regularExpression = DEFAULT_REGULAR_EXPRESSION;

public String getRegularExpression() {
return regularExpression;
}
private String regularExpression = DEFAULT_REGULAR_EXPRESSION;

@SuppressWarnings("squid:S2696") // ... initialize SquidAstVisitor
@Override
public void init() {
String regEx = getRegularExpression();
Objects.requireNonNull(regEx, "getRegularExpression() should not return null");
Objects.requireNonNull(regularExpression, "getRegularExpression() should not return null");

if (!regEx.isEmpty()) {
if (!regularExpression.isEmpty()) {
try {
IP = 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)) {
IP.reset(node.getTokenOriginalValue());
if (IP.find()) {
String ip = IP.group(0).replaceAll("\"", "");
getContext().createLineViolation(this, "Make this IP \"" + ip + "\" address configurable.", node);
}
final String tokenValue = node.getTokenOriginalValue();
final Matcher matcher = pattern.matcher(tokenValue);
if (matcher.find()) {
final String ip = tokenValue.replaceAll("\"", "");
getContext().createLineViolation(this, "Make this IP \"" + ip + "\" address configurable.", node);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
public class BullseyeParser extends CxxCoverageParser {

private static final Logger LOG = Loggers.get(BullseyeParser.class);
private static String prevLine;
private static int totalconditions;
private static int totalcoveredconditions;
private String prevLine;
private int totalconditions;
private int totalcoveredconditions;

public BullseyeParser() {
// no operation but necessary for list of coverage parsers
Expand All @@ -68,7 +68,7 @@ public void processReport(File report, final Map<String, CoverageMeasures> cover
parser.parse(report);
}

private static void collectCoverageLeafNodes(String refPath, SMInputCursor folder,
private void collectCoverageLeafNodes(String refPath, SMInputCursor folder,
final Map<String, CoverageMeasures> coverageData)
throws XMLStreamException {

Expand All @@ -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<String, CoverageMeasures> coverageData)
throws XMLStreamException {
SMInputCursor child = folder.childElementCursor();
Expand All @@ -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<String, CoverageMeasures> coverageData)
throws XMLStreamException {

Expand All @@ -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");
Expand All @@ -118,22 +118,22 @@ 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);
}
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<String> path,
private void recTreeWalk(String refPath, SMInputCursor folder, List<String> path,
final Map<String, CoverageMeasures> coverageData)
throws XMLStreamException {

Expand All @@ -160,7 +160,7 @@ private static void recTreeWalk(String refPath, SMInputCursor folder, List<Strin
}
}

private static void saveConditions(CoverageMeasures fileMeasuresBuilderIn) {
private void saveConditions(CoverageMeasures fileMeasuresBuilderIn) {
if (totalconditions > 0) {
if (totalcoveredconditions == 0) {
fileMeasuresBuilderIn.setHits(Integer.parseInt(prevLine), 0);
Expand All @@ -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":
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ee91258

Please sign in to comment.