Invalid integer value obtained from std :: stoi

Below code snippet An invalid integer value is obtained from std :: stoi klocwork error. If * contains an invalid range or not an integer value, then the catch block will be executed. But we get a klocwork error in the second loop, since the value of tvalted data is used at the loop boundary. How to fix this problem?

#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;
}
+4
source share

Source: https://habr.com/ru/post/1693254/


All Articles