下面的代码抛出Unvalidated integer value is received from std::stoi klocwork 错误。如果 *it 包含无效范围或非整数值,则将执行 catch 块。但是我们在第二个 for 循环中遇到 klocwork 错误,因为循环边界中使用了受污染的数据“值”。如何解决这个问题?
#include <vector>
#include <string>
#include <iostream>
int main()
{
int value = 0;
std::vector<std::string> test;
test.push_back("1");
test.push_back("2");
for (std::vector<std::string>::iterator it = test.begin(); it != test.end(); ++it)
{
try
{
value = std::stoi(*it);
}
catch (...)
{
return -1;
}
for (int i = 0; i < value; i++)
{
//...
//...
}
}
return 0;
}