Menu

False positive "Iterating over container 'candidates' that is always empty. (CWE-398)"

2023-01-04
2023-01-05
  • Lukas Sommer

    Lukas Sommer - 2023-01-04

    Consider the following code:

    Header:

    ifndef ASYNCIMAGERENDERTHREAD_H

    define ASYNCIMAGERENDERTHREAD_H

    include <qthread.h></qthread.h>

    class AsyncImageRenderThread : public QThread
    {
    public:
    using pointerToRenderFunction =
    std::function<void(asyncimagerenderthread &callbackobject)="">;
    virtual void run() override;
    std::atomic_bool m_loopAbort = false;
    const pointerToRenderFunction m_renderFunction;
    };</void(asyncimagerenderthread>

    endif // ASYNCIMAGERENDERTHREAD_H

    Source:

    include "asyncimagerenderthread.h"

    void AsyncImageRenderThread::run()
    {
    while (true) {
    if (m_loopAbort) {
    return;
    }
    m_renderFunction(*this);
    if (m_loopAbort) {
    return;
    }
    // In original code, here more commands.
    }
    }

    This produces a false positive "Iterating over container 'candidates' that
    is always empty. (CWE-398)", wrongly complaining about the double call of
    if(m_loopAbort). Indeed, m_renderFunction(*this); can change the state of
    m_loopAbort.

    Best regards

    Lukas Sommer

     
  • CHR

    CHR - 2023-01-05

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11478

     

Log in to post a comment.