优化:参数校验方法

wuhaoyang
RenFengJiang 10 months ago
parent 6ad1ea9a4a
commit b2e20c1dec
  1. 10
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.java
  2. 4
      sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/DLLVerifyCheckerTest.java
  3. 48
      sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/DLLVerifyChecker.cc

@ -68,17 +68,17 @@ public class DLLVerifyChecker extends SquidCheck<Grammar> {
//判断是否式动态加载库 //判断是否式动态加载库
if("dlopen".equals(desc.getTokenValue())){ if("dlopen".equals(desc.getTokenValue())){
//获取其中的参数列表 //获取其中的参数列表
AstNode firstDescendant = desc.getFirstDescendant(CxxGrammarImpl.additiveExpression); AstNode firstDescendant = desc.getFirstDescendant(CxxGrammarImpl.expressionList);
if(firstDescendant != null){ if(firstDescendant != null){
List<AstNode> children = firstDescendant.getChildren(); List<AstNode> children = firstDescendant.getChildren();
for(AstNode dren : children){ for(AstNode dren : children){
//获取参数并进行判断是否是传入的参数 //获取参数并进行判断是否是传入的参数
if("IDENTIFIER".equals(dren.getName())){ if("IDENTIFIER".equals(dren.getName()) || "initializerList".equals(dren.getName())){
if(map.containsKey(dren.getTokenValue())){ if(map.containsKey(dren.getTokenValue())){
//判断参数是否进行过校验 //判断参数是否进行过校验
Integer integer = map.get(dren.getTokenValue()); Integer integer = map.get(dren.getTokenValue());
//判断参数校验是否在使用之前 //判断参数校验是否在使用之前
if(dren.getTokenLine() > integer){ if(dren.getTokenLine() < integer){
getContext().createLineViolation(this,name,dren); getContext().createLineViolation(this,name,dren);
} }
}else { }else {
@ -86,8 +86,6 @@ public class DLLVerifyChecker extends SquidCheck<Grammar> {
} }
} }
} }
}else {
getContext().createLineViolation(this,name,desc);
} }
} }
} }
@ -108,7 +106,7 @@ public class DLLVerifyChecker extends SquidCheck<Grammar> {
if (map.containsKey(desc.getTokenValue())){ if (map.containsKey(desc.getTokenValue())){
//判断参数校验是否在使用之前 //判断参数校验是否在使用之前
int tokenLine = map.get(desc.getTokenValue()); int tokenLine = map.get(desc.getTokenValue());
if(desc.getTokenLine() > tokenLine){ if(desc.getTokenLine() < tokenLine){
getContext().createLineViolation(this,name,desc); getContext().createLineViolation(this,name,desc);
break; break;
} }

@ -27,8 +27,8 @@ public class DLLVerifyCheckerTest {
var tester = CxxFileTesterHelper.create("DLLVerifyChecker.cc"); var tester = CxxFileTesterHelper.create("DLLVerifyChecker.cc");
SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker); SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker);
CheckMessagesVerifier.verify(file.getCheckMessages()) CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(13).withMessage("在动态加载库前对输入数据进行验证") .next().atLine(22).withMessage("在动态加载库前对输入数据进行验证")
.next().atLine(36).withMessage("在动态加载库前对输入数据进行验证") .next().atLine(44).withMessage("在动态加载库前对输入数据进行验证")
.noMore(); .noMore();
} }
} }

@ -1,46 +1,54 @@
#include <iostream> #include <iostream>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include <dlfcn.h>
#else #else
#include <dlfcn.h> #include <dlfcn.h>
#endif #endif
int main() int main()
{ {
std::string a = "your_dll.dll";
std::String a = "your_dll.dll"; // if (a != "a") {
if(a != "a"){ // // 这个条件语句块目前为空,如果需要可以添加相关逻辑
} // }
HINSTANCE hInsts = LoadLibrary(a);//error //#ifdef _WIN32
//加载dll std::wstring wideDLLName(a.begin(), a.end()); // C++11及以后版本可以直接转换
// HINSTANCE hInst = LoadLibrary("your_dll.dll"); // 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) { if (hInst == NULL) {
std::cout << "无法加载库" << std::endl; std::cout << "无法加载库" << std::endl;
return -1; return -1;
} }
//获取函数
typedef void (*FuncType)(); typedef void (*FuncType)();
FuncType func = (FuncType)GetProcAddress(hInst, "函数名称"); FuncType func = (FuncType)GetProcAddress(hInst, "函数名称");
if (func == NULL) { if (func == NULL) {
std::cout << "无法获取函数" << std::endl; std::cout << "无法获取函数" << std::endl;
FreeLibrary(hInst);
return -1; return -1;
} }
//调用函数
func(); func();
//卸载dll
FreeLibrary(hInst); FreeLibrary(hInst);
//#else
std::string b = "c";
// if (b != "a") {
// // 这个条件语句块目前为空,如果需要可以添加相关逻辑
// }
std::String b = "c"; void *handle = dlopen(b.c_str(), RTLD_LAZY);//error
//加载so库
if(b != "a"){
}
void *handle = dlopen(b, RTLD_LAZY);//error
if (!handle) { if (!handle) {
std::cerr << "无法打开库:" << dlerror() << '\n'; std::cerr << "无法打开库:" << dlerror() << '\n';
return 1; return 1;
} }
//获取函数
dlerror(); // 清除上一次调用产生的错误信息
typedef void (*FuncType)(); typedef void (*FuncType)();
dlerror();
FuncType func = (FuncType)dlsym(handle, "函数名称"); FuncType func = (FuncType)dlsym(handle, "函数名称");
const char *dlsym_error = dlerror(); const char *dlsym_error = dlerror();
if (dlsym_error) { if (dlsym_error) {
@ -48,10 +56,10 @@ int main()
dlclose(handle); dlclose(handle);
return 1; return 1;
} }
//调用函数
func(); func();
//关闭库文件
dlclose(handle); dlclose(handle);
#endif //#endif
return 0; return 0;
} }
Loading…
Cancel
Save