Menu

cppcheck 2.3: false positive: zerodivcond (introduced by generic reverse valueflow)

2020-12-17
2020-12-17
  • Michał Kępień

    Hi there,

    Thank you for your work on cppcheck!

    For the following C program:

    int main(int argc, char *argv[]) {
        int quotient, remainder;
    
        remainder = argc % 2;
        argc /= 2;
        quotient = argc;
    
        if (quotient != 0) {
            return quotient;
        }
    
        return remainder;
    }
    

    cppcheck 2.3 (with --enable=warning) reports:

    Checking test.c ...
    test.c:4:19: warning: Either the condition 'quotient!=0' is redundant or there is division by zero at line 4. [zerodivcond]
     remainder = argc % 2;
                      ^
    test.c:8:15: note: Assuming that condition 'quotient!=0' is not redundant
     if (quotient != 0) {
                  ^
    test.c:6:13: note: Assignment to 'quotient=argc'
     quotient = argc;
                ^
    test.c:5:2: note: Compound assignment '/=', assigned value is 0
     argc /= 2;
     ^
    test.c:5:10: note: Assignment to 'argc/=2'
     argc /= 2;
             ^
    test.c:4:19: note: Division by zero
     remainder = argc % 2;
                      ^
    

    Of course the sample program itself is silly and does not serve any real purpose, it is just the simplest piece of code that I was able to trigger this false positive with.

    Applying any of the following changes prevents the false positive from being triggered:

    • removing the if (quotient != 0) { ... } block
    • removing the argc /= 2; line
    • replacing argc /= 2; quotient = argc; with quotient = argc / 2;

    git bisect claims that commit bd7e915c208219d6a5e3efca40168d31f9c0248c, i.e. pull request #2878, is the culprit. I also carried out an additional bisect on that pull request's branch (pfultz2/valueflow-generic-reverse) and it pointed at commit 5c75d1f0c2b23f74694318a060b43e3724df2b73 ("Add support for reassignment").

    Please let me know if any further information and/or experiments would help.

     
  • Daniel Marjamäki

    Thanks! This is a great report we should have the info we need. I have created this ticket:
    https://trac.cppcheck.net/ticket/10049

     

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.