Menu

latest cppcheck commits: new FPs for "constant" conditions in loops

Martin
2021-08-03
2021-08-03
  • Martin

    Martin - 2021-08-03

    Hi,

    recently (f.e. as a result of the main branch commits of the last few days) a lot of false-positives showed up like this one:

          for (size_t i = bodyStartIdx; i < m_TextChunks.size(); ++i)
          {
             if (i != bodyStartIdx)
             {
                bodyText += "\n";
             }
             bodyText += m_TextChunks[i];
          }
    

    (style) Condition 'i!=bodyStartIdx' is always true
    This is only true for the first iteration.

    I've seen findings like this also for other kind of loops.

    Could you please check?

    Thanks,
    Martin

     
  • Martin

    Martin - 2021-08-03

    Second example:

             for (auto sectIdx = linkSectionIdx; sectIdx < getVector().size(); ++sectIdx)
             {
                if (sectIdx == linkSectionIdx)
                {
                   result += 1;
                }
                else
                {
                   result += 2;
                }
             }
    

    --> (style) Condition 'sectIdx==linkSectionIdx' is always false

    Maybe there should be a bailout for conditions where variables are involved which may be changed in each iteration of a loop?

     

    Last edit: Martin 2021-08-03
  • Martin

    Martin - 2021-08-03

    Yes, with that commit I got the FPs mentioned above. Maybe I missed to copy some parts of the file that trigger these FPs in the analysis, I'll look into that later.

    Btw, I can't build the current head; for 7a6d7f7c2 I get:

    lib/valueflow.cpp:2609:1: error: expected initializer before ‘Analyzer’
    Analyzer::Result result{};
    ^~~~~~~~
    lib/valueflow.cpp:2610:1: error: expected unqualified-id before ‘for’
    for (const ValueFlow::Value& v : values)
    ^~~
    lib/valueflow.cpp:2614:1: error: expected unqualified-id before ‘return’
    return result;
    ^~~~~~
    lib/valueflow.cpp:2615:1: error: expected declaration before ‘}’ token
    }
    ^

     
  • Martin

    Martin - 2021-08-03

    I retried, with the latest commit I get the FP for a file containing only this code:

       void foo()
       {
          auto iterBegin = container.begin();
          for (auto iter = iterBegin; iter != iterEnd; ++iter)
          {
             if(iter != iterBegin)
             {
                result << delimiter;
             }
          }
       }
    

    ./cppcheck --enable=all /home/user/Desktop/21-08-03.cpp
    Checking /home/user/Desktop/21-08-03.cpp ...
    /home/user/Desktop/21-08-03.cpp:6:18: style: Condition 'iter!=iterBegin' is always true [knownConditionTrueFalse]
    if(iter != iterBegin)
    ^
    /home/user/Desktop/21-08-03.cpp:4:22: note: iter is assigned 'iterBegin' here.
    for (auto iter = iterBegin; iter != iterEnd; ++iter)
    ^
    /home/user/Desktop/21-08-03.cpp:4:54: note: iter is incremented', new value is symbolic=iterBegin+1
    for (auto iter = iterBegin; iter != iterEnd; ++iter)
    ^
    /home/user/Desktop/21-08-03.cpp:6:18: note: Condition 'iter!=iterBegin' is always true
    if(iter != iterBegin)

    ^
    /home/user/Desktop/21-08-03.cpp:1:0: style: The function 'foo' is never used. [unusedFunction]

    ^

     
  • CHR

    CHR - 2021-08-03

    Thanks for the example, ticket is here: https://trac.cppcheck.net/ticket/10395

     

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.