Menu

False positive after macro usage

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

    Lukas Sommer - 2023-01-05

    Consider the following code:

        QStringList candidates;
    

    ifdef Q_OS_DARWIN

        candidates.append(QStringLiteral(u"/color/icc/"));
    

    elif defined(Q_OS_UNIX)

        candidates.append(QStringLiteral(u"/color/icc/"));
    

    elif defined(Q_OS_WIN)

        candidates.append(QStringLiteral(u"/color/icc/"));
    

    endif

        int i = 0;
        for (const QString &path : candidates) {
            if (path.isEmpty()) {
                i = i * 3;
            }
        }
    

    cppcheck now complains:

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

    However, 'candidates' is not empty for at least many platforms.

    Lukas Sommer

     
  • CHR

    CHR - 2023-01-05

    Have you considered passing e.g. -DQ_OS_DARWIN to cppcheck?

     
  • Lukas Sommer

    Lukas Sommer - 2023-01-05

    Wouldn't this implicitly disable cppcheck for the other paths (Q_OS_UNIX and Q_OS_WIN) for the hole project?

    If I want to avoid this, maybe I could “--inline-suppr” and manually suppress this single warning for this line of the code? This might the better choice for me because I have only one offending line with a theoretically possibly empty container, but I have much more lines in the different code paths which could profit from cppcheck.

     

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.