优化:参数校验方法

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())){
//获取其中的参数列表
AstNode firstDescendant = desc.getFirstDescendant(CxxGrammarImpl.additiveExpression);
AstNode firstDescendant = desc.getFirstDescendant(CxxGrammarImpl.expressionList);
if(firstDescendant != null){
List<AstNode> 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<Grammar> {
}
}
}
}else {
getContext().createLineViolation(this,name,desc);
}
}
}
@ -108,7 +106,7 @@ public class DLLVerifyChecker extends SquidCheck<Grammar> {
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;
}

@ -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();
}
}

@ -1,46 +1,54 @@
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#include <dlfcn.h>
#else
#include <dlfcn.h>
#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;
}
Loading…
Cancel
Save