diff --git a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.java b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.java index 9a34a8c..25bf269 100644 --- a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.java +++ b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.java @@ -68,17 +68,17 @@ public class DLLVerifyChecker extends SquidCheck { //判断是否式动态加载库 if("dlopen".equals(desc.getTokenValue())){ //获取其中的参数列表 - AstNode firstDescendant = desc.getFirstDescendant(CxxGrammarImpl.additiveExpression); + AstNode firstDescendant = desc.getFirstDescendant(CxxGrammarImpl.expressionList); if(firstDescendant != null){ List children = firstDescendant.getChildren(); for(AstNode dren : children){ //获取参数并进行判断是否是传入的参数 - if("IDENTIFIER".equals(dren.getName())){ + if("IDENTIFIER".equals(dren.getName()) || "initializerList".equals(dren.getName())){ if(map.containsKey(dren.getTokenValue())){ //判断参数是否进行过校验 Integer integer = map.get(dren.getTokenValue()); //判断参数校验是否在使用之前 - if(dren.getTokenLine() > integer){ + if(dren.getTokenLine() < integer){ getContext().createLineViolation(this,name,dren); } }else { @@ -86,8 +86,6 @@ public class DLLVerifyChecker extends SquidCheck { } } } - }else { - getContext().createLineViolation(this,name,desc); } } } @@ -108,7 +106,7 @@ public class DLLVerifyChecker extends SquidCheck { if (map.containsKey(desc.getTokenValue())){ //判断参数校验是否在使用之前 int tokenLine = map.get(desc.getTokenValue()); - if(desc.getTokenLine() > tokenLine){ + if(desc.getTokenLine() < tokenLine){ getContext().createLineViolation(this,name,desc); break; } diff --git a/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyCheckerTest.java b/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyCheckerTest.java index 1d9ea54..5b4aefc 100644 --- a/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyCheckerTest.java +++ b/sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyCheckerTest.java @@ -27,8 +27,8 @@ public class DLLVerifyCheckerTest { var tester = CxxFileTesterHelper.create("DLLVerifyChecker.cc"); SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker); CheckMessagesVerifier.verify(file.getCheckMessages()) - .next().atLine(13).withMessage("在动态加载库前对输入数据进行验证") - .next().atLine(36).withMessage("在动态加载库前对输入数据进行验证") + .next().atLine(22).withMessage("在动态加载库前对输入数据进行验证") + .next().atLine(44).withMessage("在动态加载库前对输入数据进行验证") .noMore(); } } diff --git a/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.cc b/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.cc index 8676653..2740cf2 100644 --- a/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.cc +++ b/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.cc @@ -1,46 +1,54 @@ #include #ifdef _WIN32 #include + #include #else #include #endif + int main() { - - std::String a = "your_dll.dll"; - if(a != "a"){ - } - HINSTANCE hInsts = LoadLibrary(a);//error - //加载dll -// HINSTANCE hInst = LoadLibrary("your_dll.dll"); + std::string a = "your_dll.dll"; +// if (a != "a") { +// // 这个条件语句块目前为空,如果需要可以添加相关逻辑 +// } +//#ifdef _WIN32 + std::wstring wideDLLName(a.begin(), a.end()); // C++11及以后版本可以直接转换 +// std::string a = "aa"; +// std::wstring wideA(a.begin(), a.end()); +// if(wideDLLName == wideA){ +// } + std::wstring wideDLLName(a.begin(), a.end()); + HINSTANCE hInst = LoadLibrary(wideDLLName.c_str());//error if (hInst == NULL) { std::cout << "无法加载库" << std::endl; return -1; } - //获取函数 + typedef void (*FuncType)(); FuncType func = (FuncType)GetProcAddress(hInst, "函数名称"); if (func == NULL) { std::cout << "无法获取函数" << std::endl; + FreeLibrary(hInst); return -1; } - //调用函数 + func(); - //卸载dll FreeLibrary(hInst); +//#else + std::string b = "c"; +// if (b != "a") { +// // 这个条件语句块目前为空,如果需要可以添加相关逻辑 +// } - std::String b = "c"; - //加载so库 - if(b != "a"){ - } - void *handle = dlopen(b, RTLD_LAZY);//error + void *handle = dlopen(b.c_str(), RTLD_LAZY);//error if (!handle) { std::cerr << "无法打开库:" << dlerror() << '\n'; return 1; } - //获取函数 + + dlerror(); // 清除上一次调用产生的错误信息 typedef void (*FuncType)(); - dlerror(); FuncType func = (FuncType)dlsym(handle, "函数名称"); const char *dlsym_error = dlerror(); if (dlsym_error) { @@ -48,10 +56,10 @@ int main() dlclose(handle); return 1; } - //调用函数 + func(); - //关闭库文件 dlclose(handle); -#endif +//#endif + return 0; } \ No newline at end of file