|
|
@ -16,10 +16,12 @@ import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault; |
|
|
|
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation; |
|
|
|
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation; |
|
|
|
import org.sonar.cxx.squidbridge.checks.SquidCheck; |
|
|
|
import org.sonar.cxx.squidbridge.checks.SquidCheck; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.io.BufferedReader; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.io.FileReader; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import static com.sonar.cxx.sslr.api.GenericTokenType.IDENTIFIER; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Rule(key = "VerificationPathChecker", name = "在构建路径名前对数据进行校验", description = "对输入数据进行校验", priority = Priority.INFO, tags = {"28suo"}) |
|
|
|
@Rule(key = "VerificationPathChecker", name = "在构建路径名前对数据进行校验", description = "对输入数据进行校验", priority = Priority.INFO, tags = {"28suo"}) |
|
|
|
@ActivatedByDefault |
|
|
|
@ActivatedByDefault |
|
|
@ -28,45 +30,66 @@ public class VerificationPathChecker extends SquidCheck<Grammar> { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void init() { |
|
|
|
public void init() { |
|
|
|
// 订阅要检查AST节点类型,用于在visitNode方法中检查该类型节点
|
|
|
|
this.subscribeTo(CxxGrammarImpl.functionBody); |
|
|
|
this.subscribeTo( |
|
|
|
|
|
|
|
CxxGrammarImpl.functionBody |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void visitNode(AstNode astNode) { |
|
|
|
public void visitNode(AstNode astNode) { |
|
|
|
List<AstNode> descendants = astNode.getDescendants(CxxGrammarImpl.statement); |
|
|
|
List<AstNode> descendants = astNode.getDescendants(CxxGrammarImpl.statement); |
|
|
|
for (AstNode ast:descendants) { |
|
|
|
for (AstNode ast : descendants) { |
|
|
|
List<AstNode> descendants1 = ast.getDescendants(CxxGrammarImpl.shiftExpression); |
|
|
|
if (processAst(ast)) { |
|
|
|
for (AstNode desc :descendants1) { |
|
|
|
break; |
|
|
|
if("cin".equals(desc.getTokenValue())){ |
|
|
|
} |
|
|
|
List<AstNode> children = desc.getChildren(); |
|
|
|
} |
|
|
|
for (AstNode chil:children) { |
|
|
|
} |
|
|
|
if("IDENTIFIER".equals(chil.getName())){ |
|
|
|
|
|
|
|
if(chil.getTokenValue().toLowerCase().contains("path")){ |
|
|
|
private boolean processAst(AstNode ast) { |
|
|
|
boolean boo = true; |
|
|
|
List<AstNode> innerDescendants = ast.getDescendants(CxxGrammarImpl.shiftExpression); |
|
|
|
List<AstNode> exprs = astNode.getDescendants(CxxGrammarImpl.postfixExpression); |
|
|
|
for (AstNode desc : innerDescendants) { |
|
|
|
for (AstNode expr:exprs) { |
|
|
|
if ("cin".equals(desc.getTokenValue())) { |
|
|
|
if(expr.getTokenValue().contains("check") || expr.getTokenValue().contains("verify") || expr.getTokenValue().contains("valid")){ |
|
|
|
List<AstNode> children = desc.getChildren(); |
|
|
|
List<AstNode> astNodeList = expr.getDescendants(CxxGrammarImpl.expressionList); |
|
|
|
for (AstNode chil : children) { |
|
|
|
for (AstNode asrList:astNodeList) { |
|
|
|
if ("IDENTIFIER".equals(chil.getName())) { |
|
|
|
if(chil.getTokenValue().equals(asrList.getTokenValue())){ |
|
|
|
if (chil.getTokenValue().toLowerCase().contains("path")) { |
|
|
|
boo = false; |
|
|
|
boolean boo = true; |
|
|
|
} |
|
|
|
List<AstNode> exprs = ast.getDescendants(CxxGrammarImpl.postfixExpression); |
|
|
|
|
|
|
|
for (AstNode expr : exprs) { |
|
|
|
|
|
|
|
if (expr.getTokenValue().contains("check") || expr.getTokenValue().contains("verify") || expr.getTokenValue().contains("valid")) { |
|
|
|
|
|
|
|
List<AstNode> astNodeList = expr.getDescendants(CxxGrammarImpl.expressionList); |
|
|
|
|
|
|
|
for (AstNode asrList : astNodeList) { |
|
|
|
|
|
|
|
if (chil.getTokenValue().equals(asrList.getTokenValue())) { |
|
|
|
|
|
|
|
boo = false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(boo){ |
|
|
|
} |
|
|
|
getContext().createLineViolation(this, "在构建路径名前对数据进行校验", chil); |
|
|
|
int lineNumber = chil.getTokenLine(); |
|
|
|
} |
|
|
|
if (boo && isLineInFile(lineNumber)) { |
|
|
|
|
|
|
|
getContext().createLineViolation(this, "在构建路径名前对数据进行校验", chil); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isLineInFile(int lineNumber) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
int totalLines = countLines(getContext().getFile()); |
|
|
|
|
|
|
|
return lineNumber <= totalLines; |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int countLines(File file) throws IOException { |
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) { |
|
|
|
|
|
|
|
int lines = 0; |
|
|
|
|
|
|
|
while (reader.readLine() != null) lines++; |
|
|
|
|
|
|
|
return lines; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|