Menu

false positive uninitvar with try/catch

2023-01-13
2023-01-15
  • Daniel Black

    Daniel Black - 2023-01-13

    $ bin/cppcheck /tmp/m.c++
    Checking /tmp/m.c++ ...
    /tmp/m.c++:13:40: warning: Uninitialized variable: i [uninitvar]
    for (size_t j = 0; j < i; j++) {
    ^

    $ cat /tmp/m.c++

    include <string></string>

    int main()
    {
    size_t i, n_elements= 5;
    std::string *p = new std::string[n_elements];
    try {
    for (i = 0; i < n_elements; i++) {
    new(p) std::string;
    ++p;
    }
    } catch (...) {
    for (size_t j = 0; j < i; j++) {
    --p;
    }

                throw;
        }
    

    }

    "i" will always be initialized in the try clause.

     
  • CHR

    CHR - 2023-01-13

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

     
  • Daniel Black

    Daniel Black - 2023-01-15

    Thanks for fixing.

     

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.