From be3ef800599c4bcb756bc8ae0518f60cf9aa8e1a Mon Sep 17 00:00:00 2001 From: RenFengJiang <1111> Date: Sun, 14 Jan 2024 15:57:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rules/checkers/ReallocMainChecker.java | 119 ++++++++---------- 1 file changed, 54 insertions(+), 65 deletions(-) diff --git a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ReallocMainChecker.java b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ReallocMainChecker.java index f96a4e6..95aeea4 100644 --- a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ReallocMainChecker.java +++ b/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; +import com.keyware.sonar.cxx.SubscriptionAstVisitor; import com.sonar.cxx.sslr.api.AstNode; +import com.sonar.cxx.sslr.api.AstNodeType; import com.sonar.cxx.sslr.api.Grammar; import org.sonar.check.Priority; 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.checks.SquidCheck; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; @@ -31,83 +34,69 @@ public class ReallocMainChecker extends SquidCheck { @Override public void init() { - // 订阅要检查AST节点类型,用于在visitNode方法中检查该类型节点 + // 指定当前访问器需要访问的节点类型,functionBody(函数)主体节点 this.subscribeTo( - CxxGrammarImpl.expression + CxxGrammarImpl.functionBody ); } - private List lists = new ArrayList(); /** - * 检查AST节点 + * 访问AST节点 * - * @param astNode 要处理的AST节点,该节点类型为通过subscribeTo方法订阅的类型 + * @param node 要处理的AST节点,该节点类型为通过subscribeTo方法订阅的类型 */ @Override - public void visitNode(AstNode astNode) { - //判断是否是memset方法调用 - if("memset".equals(astNode.getToken().getValue())){ - //获取到此 方法调用中的参数列表 - List listAsts = astNode.getDescendants(CxxGrammarImpl.expressionList); - for (AstNode listAst :listAsts) { - //将此参数列表存入list中 - lists.add(listAst.getToken().getValue()); - } + public void visitNode(@Nonnull AstNode node) { + IfChildren ifChildren = new IfChildren(this); + ifChildren.accept(node); + + } + + class IfChildren extends SubscriptionAstVisitor { + + public IfChildren(SquidCheck checker){ + super(checker); } - List astNodeDescendants = astNode.getDescendants(CxxGrammarImpl.postfixExpression); - for (AstNode ast :astNodeDescendants) { - //判断是不是realloc方法调用 - if("realloc".equals(ast.getToken().getValue())){ - //获取到参数列表 - List astNodes = ast.getDescendants(CxxGrammarImpl.expressionList); - for (AstNode as:astNodes) { - //获取到参数 - String name = as.getToken().getValue(); - //判断参数是否存在在集合中 - if(!lists.contains(name)){ - getContext().createLineViolation(this,"使用realloc函数前应先清楚敏感信息",as); + + @Override + public List visitNodeTypes() { + // 指定当前访问器需要访问的节点类型,这里指定了simpleDeclaration(简单声明)节点类型 + return List.of(CxxGrammarImpl.expression); + } + + private List lists = new ArrayList(); + /** + * 检查AST节点 + * + * @param astNode 要处理的AST节点,该节点类型为通过subscribeTo方法订阅的类型 + */ + @Override + public void visitNode(AstNode astNode) { + //判断是否是memset方法调用 + if("memset".equals(astNode.getToken().getValue())){ + //获取到此 方法调用中的参数列表 + List listAsts = astNode.getDescendants(CxxGrammarImpl.expressionList); + for (AstNode listAst :listAsts) { + //将此参数列表存入list中 + lists.add(listAst.getToken().getValue()); + } + } + List astNodeDescendants = astNode.getDescendants(CxxGrammarImpl.postfixExpression); + for (AstNode ast :astNodeDescendants) { + //判断是不是realloc方法调用 + if("realloc".equals(ast.getToken().getValue())){ + //获取到参数列表 + List 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{ -// 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 astNodes = astNode.getDescendants(CxxGrammarImpl.expressionStatement); -// for (AstNode as :astNodes) { -// if("memset".equals(as.getToken().getValue())){ -// List descendants = astNode.getDescendants(CxxGrammarImpl.expressionList); -// for (AstNode ast :descendants) { -// if(name.equals(ast.getToken().getValue())){ -// boo = false; -// } -// } -// } -// } -// -// List descendants = astNode.getDescendants(CxxGrammarImpl.postfixExpression); -// for (AstNode ast :descendants) { -// if(name.equals(ast.getToken().getValue())){ -// boo = false; -// } -// } -// } -// -// } }