Merge remote-tracking branch 'origin/master'

wuhaoyang
Guo XIn 8 months ago
commit 9aa48fb42e
  1. 119
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ReallocMainChecker.java

@ -6,7 +6,9 @@
*/ */
package com.keyware.sonar.cxx.rules.checkers; package com.keyware.sonar.cxx.rules.checkers;
import com.keyware.sonar.cxx.SubscriptionAstVisitor;
import com.sonar.cxx.sslr.api.AstNode; import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.AstNodeType;
import com.sonar.cxx.sslr.api.Grammar; import com.sonar.cxx.sslr.api.Grammar;
import org.sonar.check.Priority; import org.sonar.check.Priority;
import org.sonar.check.Rule; import org.sonar.check.Rule;
@ -15,6 +17,7 @@ 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 javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -31,83 +34,69 @@ public class ReallocMainChecker extends SquidCheck<Grammar> {
@Override @Override
public void init() { public void init() {
// 订阅要检查AST节点类型,用于在visitNode方法中检查该类型节点 // 指定当前访问器需要访问的节点类型,functionBody(函数)主体节点
this.subscribeTo( this.subscribeTo(
CxxGrammarImpl.expression CxxGrammarImpl.functionBody
); );
} }
private List<String> lists = new ArrayList();
/** /**
* 检查AST节点 * 访问AST节点
* *
* @param astNode 要处理的AST节点该节点类型为通过subscribeTo方法订阅的类型 * @param node 要处理的AST节点该节点类型为通过subscribeTo方法订阅的类型
*/ */
@Override @Override
public void visitNode(AstNode astNode) { public void visitNode(@Nonnull AstNode node) {
//判断是否是memset方法调用 IfChildren ifChildren = new IfChildren(this);
if("memset".equals(astNode.getToken().getValue())){ ifChildren.accept(node);
//获取到此 方法调用中的参数列表
List<AstNode> listAsts = astNode.getDescendants(CxxGrammarImpl.expressionList); }
for (AstNode listAst :listAsts) {
//将此参数列表存入list中 class IfChildren extends SubscriptionAstVisitor {
lists.add(listAst.getToken().getValue());
} public IfChildren(SquidCheck<Grammar> checker){
super(checker);
} }
List<AstNode> astNodeDescendants = astNode.getDescendants(CxxGrammarImpl.postfixExpression);
for (AstNode ast :astNodeDescendants) { @Override
//判断是不是realloc方法调用 public List<AstNodeType> visitNodeTypes() {
if("realloc".equals(ast.getToken().getValue())){ // 指定当前访问器需要访问的节点类型,这里指定了simpleDeclaration(简单声明)节点类型
//获取到参数列表 return List.of(CxxGrammarImpl.expression);
List<AstNode> astNodes = ast.getDescendants(CxxGrammarImpl.expressionList); }
for (AstNode as:astNodes) {
//获取到参数 private List<String> lists = new ArrayList();
String name = as.getToken().getValue(); /**
//判断参数是否存在在集合中 * 检查AST节点
if(!lists.contains(name)){ *
getContext().createLineViolation(this,"使用realloc函数前应先清楚敏感信息",as); * @param astNode 要处理的AST节点该节点类型为通过subscribeTo方法订阅的类型
*/
@Override
public void visitNode(AstNode astNode) {
//判断是否是memset方法调用
if("memset".equals(astNode.getToken().getValue())){
//获取到此 方法调用中的参数列表
List<AstNode> listAsts = astNode.getDescendants(CxxGrammarImpl.expressionList);
for (AstNode listAst :listAsts) {
//将此参数列表存入list中
lists.add(listAst.getToken().getValue());
}
}
List<AstNode> astNodeDescendants = astNode.getDescendants(CxxGrammarImpl.postfixExpression);
for (AstNode ast :astNodeDescendants) {
//判断是不是realloc方法调用
if("realloc".equals(ast.getToken().getValue())){
//获取到参数列表
List<AstNode> astNodes = ast.getDescendants(CxxGrammarImpl.expressionList);
for (AstNode as:astNodes) {
//获取到参数
String name = as.getToken().getValue();
//判断参数是否存在在集合中
if(!lists.contains(name)){
reportIssue(as, "使用realloc函数前应先清楚敏感信息");
}
} }
} }
} }
} }
} }
// class IfChildren extends SquidCheck<Grammar>{
// private final String name ;
// private boolean boo = true;
//
// IfChildren(String name){
// this.name = name;
// }
//
// @Override
// public void init() {
// // 订阅要检查AST节点类型,用于在visitNode方法中检查该类型节点
// this.subscribeTo(
// CxxGrammarImpl.expression
// );
// }
// @Override
// public void visitNode(AstNode astNode) {
//
// List<AstNode> astNodes = astNode.getDescendants(CxxGrammarImpl.expressionStatement);
// for (AstNode as :astNodes) {
// if("memset".equals(as.getToken().getValue())){
// List<AstNode> descendants = astNode.getDescendants(CxxGrammarImpl.expressionList);
// for (AstNode ast :descendants) {
// if(name.equals(ast.getToken().getValue())){
// boo = false;
// }
// }
// }
// }
//
// List<AstNode> descendants = astNode.getDescendants(CxxGrammarImpl.postfixExpression);
// for (AstNode ast :descendants) {
// if(name.equals(ast.getToken().getValue())){
// boo = false;
// }
// }
// }
//
// }
} }

Loading…
Cancel
Save