Arun
Arun

Reputation: 2373

Unvalidated integer value is received from std::stoi

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;
}

Upvotes: 4

Views: 712

Answers (1)

Sivanesh Waran
Sivanesh Waran

Reputation: 76

i have tried the same code with Klocwork version 2020.2 and not seeing any issues in the code. Possibly, this False Positive might have resolved in the latest version of Klocwork.

Please do try to test the code with Klocwork latest version.

Upvotes: 1

Related Questions