Menu

Using uninitialized member not reported

2020-10-27
2020-10-27
  • Ronnie Smets

    Ronnie Smets - 2020-10-27

    Hi,

    I found an unitialized member in my code, that was not detected by cppcheck. See reproduction code below. Theoretically cppcheck is correct, the member is always true at the end of the constructor, but I was expecting some sort of message. Is there a way to convince cppcheck to give me a warning for this, or is this a bug?

    Code:
    `
    struct A
    {
    A();
    bool m_a;
    };

    void doSomething()
    {
    }

    A::A()
    {
    if (m_a) {
    doSomething();
    }
    else {
    m_a = true;
    }
    }
    `

     

    Last edit: Ronnie Smets 2020-10-27
    • Daniel Marjamäki

      Is there a way to convince cppcheck to give me a warning for this, or is this a bug?

      It's a bug. I think that it might be relatively easy to fix.

      For constructors, CheckUninitVar::checkScope could first determine members that are not initialized in the initialization list. Then for each such variable, execute CheckUninitVar::checkScopeForVariable.

      Would you be interested to investigate this fix in Cppcheck?

       
  • Ronnie Smets

    Ronnie Smets - 2020-10-27

    Hi Daniel,

    Thanks for the quick response ! I would be interested in fixing this issue, but truth be told, I don't think I will have time in the next couple of weeks. If that is not an issue, I'll have a look at it when I have time.

    Best regards,
    Ronnie

     

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.