Menu

Uninitialized variable false positive - braced initialization of scoped class with new expression

2022-12-05
2022-12-14
  • Muhammad Javed

    Muhammad Javed - 2022-12-05

    Previously, it seems like cppcheck had an issue where an uninitvar error was raised when using braced initialization of a class with new (https://trac.cppcheck.net/ticket/10698). This was later fixed in cppcheck 2.8. However, it seems that this issue still persists in cppcheck 2.9 if the class being initialized is referenced using scope resolution. Here is a minimal case of the issue that I will also attach:

    #include <tuple>
    
    namespace foo
    {
        class bar{};
    }
    
    class foobar{};
    
    auto test()
    {
        foo::bar* inst1 = new foo::bar{};   // cppcheck 2.9 flags this as an uninitialized variable
        foo::bar* inst2 = new foo::bar;     // OK
        foo::bar* inst3 = new foo::bar();   // OK
    
        foobar* inst4   = new foobar{};     // OK
        foobar* inst5   = new foobar;       // OK
        foobar* inst6   = new foobar();     // OK
    
        return std::make_tuple(inst1, inst2, inst3, inst4, inst5, inst6);
    }
    
    int main()
    {
        test();
        return 0;
    }
    

    Cppcheck 2.9 will flag inst1 as an uninitialized variable.

     
  • CHR

    CHR - 2022-12-06

    Have you checked with head? Seems like the problem has been fixed already.

     
  • Muhammad Javed

    Muhammad Javed - 2022-12-14

    Sorry for the late reply. I checked the issue on head and it has been fixed. Thank you.

     

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.