Menu

Short circuit logic and uninitialized variable false positive

2019-05-06
2021-01-26
  • Alexey Savin

    Alexey Savin - 2019-05-06

    Hello,

    I guess I found false positive warning related short circuit logic behavior.
    Look at the following code please. It seems that cppcheck don't tke in account b is initialized anyway in left part of the expression.

    class A
    {
    public:
        A()
        {
            valid = false;
            value = false;
        }
    
        bool get(bool& v) const
        {
            v = value;
            return valid;
        }
    
        void set(bool v)
        {
            value = v;
            valid = true;
        }
    
    private:
        bool value;
        bool valid;
    };
    
    int main()
    {
        A a;
        a.set(true);
    
        // cppcheck says b is uninitialized
        bool b = a.get(b) && b;
    
        // cppcheck says nothing
        bool c;
        bool isOk = a.get(c) && c;
    
        return !isOk;
    }
    
     
  • CHR

    CHR - 2021-01-26

    No repro with 2.3.

     

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.