Menu

Incorrect style:variableScope warning

2021-10-04
2021-10-04
  • Piyush Kumar

    Piyush Kumar - 2021-10-04
    #include <charconv>
    #include <iostream>
    #include <string>
    #include <system_error>
    
    int main()
    {
        std::string token{"42"};
    
        int value{};
        if (const auto [p, ec] = std::from_chars(token.data(),
                                                 token.data() + token.size(),
                                                 value);
            ec == std::errc())
        {
            std::cout << value;
        }
    }
    

    Got a false positive
    "CWE-398 The scope of the variable 'value' can be reduced.cppcheck(style:variableScope)"

    value's scope cannot be reduced since std::from_chars takes in value as a out-parameter.
    See : https://en.cppreference.com/w/cpp/utility/from_chars.

     

    Last edit: Piyush Kumar 2021-10-04
  • CHR

    CHR - 2021-10-04

    Reduced example:

    int main()
    {
        int value{};
        if (int s = f(value);
            s == 1)
        {
            return s;
        }
    }
    
     
  • CHR

    CHR - 2021-10-04

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/10527

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.