Below piece of code throws Unvalidated integer value is received from std::stoi klocwork error. If *it contains invalid range or non integer value then catch block will be executed. But we are getting klocwork error in second for loop as tainted data 'value' is used in loop boundary. How to fix this issue?
#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;
}