From 064bb0309eeb04bd0bb21c0ea5e82bf9fab7c924 Mon Sep 17 00:00:00 2001 From: wuhaoyang <2507865306@qq.com> Date: Fri, 26 Jan 2024 13:49:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=87=86=E5=88=99:=E5=9C=A8?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E4=B8=AD=E6=8C=87=E5=AE=9A=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E7=9A=84=E6=96=87=E4=BB=B6=E6=88=96=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=EF=BC=8C=E5=9C=A8=E8=AE=BF=E9=97=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=88=96=E7=9B=AE=E5=BD=95=E5=89=8D=E5=AF=B9=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=90=8D=E8=BF=9B=E8=A1=8C=E9=AA=8C=E8=AF=81=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E4=BB=85=E5=85=81=E8=AE=B8=E8=AE=BF=E9=97=AE=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E7=9A=84=E6=96=87=E4=BB=B6=E6=88=96=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cxx/rules/checkers/FileAccessChecker.java | 52 ++++++++----------- .../rules/checkers/FileAccessCheckerTest.java | 2 +- .../cxx/rules/checkers/FileAccessChecker.cc | 6 ++- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/FileAccessChecker.java b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/FileAccessChecker.java index bf43cc1..b54e44d 100644 --- a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/FileAccessChecker.java +++ b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/FileAccessChecker.java @@ -28,6 +28,7 @@ import org.sonar.cxx.squidbridge.checks.SquidCheck; @SqaleConstantRemediation("5min") public class FileAccessChecker extends SquidCheck { private boolean filePathChecked = false; + private boolean filePathUsedInIf = false; @Override public void init() { @@ -47,56 +48,47 @@ public class FileAccessChecker extends SquidCheck { private void checkIfStatement(AstNode ifStatement) { AstNode condition = ifStatement.getFirstChild(CxxGrammarImpl.condition); - - if (containsFilePathCheck(condition)) { - filePathChecked = true; + filePathUsedInIf = containsFilePathCheck(condition); + if (!filePathUsedInIf) { + createViolation(ifStatement, "在访问文件或目录前需要对路径名进行验证"); } + filePathUsedInIf = false; } private void checkDeclaration(AstNode declaration) { AstNode declaratorNode = declaration.getFirstDescendant(CxxGrammarImpl.declarator); - - if (declaratorNode != null) { + filePathChecked = containsFilePathCheck(declaratorNode); + if (declaratorNode != null && !filePathChecked) { String variableName = declaratorNode.getTokenOriginalValue(); - - if ("filePath".equals(variableName)) { - if (!filePathChecked) { - System.out.println("在访问文件或目录前需要对路径名进行验证:"+variableName); - getContext().createLineViolation(this, "在访问文件或目录前需要对路径名进行验证", declaratorNode); - } + if (variableName != null && variableName.equals("filePath")) { + System.out.println(variableName); + getContext().createLineViolation(this, "在访问文件或目录前需要对路径名进行验证", declaratorNode); } } } private void checkStatement(AstNode statement) { AstNode condition = statement.getFirstChild(CxxGrammarImpl.condition); - - if (containsFilePathCheck(condition)) { - filePathChecked = true; - } - } - - private boolean containsFilePathCheck(AstNode condition) { - return recursiveCheck(condition); + filePathChecked = containsFilePathCheck(condition); } - private boolean recursiveCheck(AstNode node) { - if (node == null) { - return false; + private boolean containsFilePathCheck(AstNode node) { + if (node != null && node.getTokenOriginalValue().equals("filePath")) { + return true; } - if (node.is(CxxGrammarImpl.identifierList)) { - // 如果是标识符,检查是否是 filePath - String identifierName = node.getTokenOriginalValue(); - return "filePath".equals(identifierName); - } else { - // 递归检查子节点 + if (node != null) { for (AstNode child : node.getChildren()) { - if (recursiveCheck(child)) { + // 如果 if 中使用了 filePath,则停止递归并跳过 ifstream file(filePath) 检查 + if (containsFilePathCheck(child)) { return true; } } - return false; } + return false; // 节点或其任何子节点中找不到“filePath” + } + + private void createViolation(AstNode node, String message) { + getContext().createLineViolation(this, message, node); } } diff --git a/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/FileAccessCheckerTest.java b/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/FileAccessCheckerTest.java index 5bc513a..1e0a0d2 100644 --- a/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/FileAccessCheckerTest.java +++ b/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/FileAccessCheckerTest.java @@ -27,7 +27,7 @@ public class FileAccessCheckerTest { var tester = CxxFileTesterHelper.create("FileAccessChecker.cc"); SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker); CheckMessagesVerifier.verify(file.getCheckMessages()) - .next().atLine(8).withMessage("在访问文件或目录前需要对路径名进行验证") + .next().atLine(13).withMessage("在访问文件或目录前需要对路径名进行验证") .noMore(); } } diff --git a/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/FileAccessChecker.cc b/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/FileAccessChecker.cc index 48dd046..41b8d46 100644 --- a/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/FileAccessChecker.cc +++ b/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/FileAccessChecker.cc @@ -5,10 +5,12 @@ using namespace std; int main() { - string filePath = "C:\\Users\\user\\Desktop\\test.txt";//error + string filePath = "C:\\Users\\user\\Desktop\\test.txt"; - if (filePath) { + int a = 5; + + if (a<5) { ifstream file(filePath); cout << "File opened successfully." << endl;