Menu

False positive: knownConditionTrueFalse

CHR
2020-09-11
2020-09-11
  • CHR

    CHR - 2020-09-11

    The value of Ret is conditionally overwritten, so the second check for Ret != 0 is not always true.

    long GetRet();
    
    long foo(long N) {
    
        for (long I = 0; I < N; I++)
        {
            long Ret = GetRet();
    
            if (Ret != 0)
            {
                if (Ret == 1)
                    Ret = GetRet();
    
                if (Ret != 0) // style: Condition 'Ret!=0' is always true [knownConditionTrueFalse]
                    break;
            }
    
        }
        return Ret;
    }
    
     
  • Daniel Marjamäki

    Thanks! I can reproduce. I have created ticket https://trac.cppcheck.net/ticket/9893

     
  • CHR

    CHR - 2020-09-11

    Thanks for your response. This one is probably related:

    bool cond();
    
    long bar() {
    
        bool stop = false;
        while (!stop)
        {
            if (cond())
                stop = true;
            break;
        }
        if (!stop)
            return 1;
        return 0;
    }
    
     
    • Daniel Marjamäki

      Thanks! I can reproduce. I created ticket https://trac.cppcheck.net/ticket/9894

       
  • CHR

    CHR - 2020-09-11

    Another instance:

    long GetN();
    bool cond();
    void calc(long& N);
    
    long baz() {
    
        long N = GetN();
        if (N == 0)
            return 0;
        if (N > 1) // note that cond() does not trigger the warning
             calc(N);
        if (N == 0)
            return 0;
        return N + 1;
    }
    
     

    Last edit: CHR 2020-09-11
    • Daniel Marjamäki

      Thanks! I can reproduce. I created ticket https://trac.cppcheck.net/ticket/9895

       

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.