Menu

false positive? nullPointerRedundantCheck

2020-08-19
2020-08-19
  • Gunnar Pedersen

    Gunnar Pedersen - 2020-08-19

    I am not sure, but I get a warning about nullPointerRedundantCheck from the code below. By adding the commented line the warning is gone..
    Using latest build of cppcheck. The small little test program is extracted from something I found in a code review..

    ~/project/$ ../cppcheck/cppcheck --version
    Cppcheck 2.2 dev
    ~/project/$ ../cppcheck/cppcheck --enable=warning cppcheckrednullptr.c  
    Checking cppcheckrednullptr.c ...
    cppcheckrednullptr.c:16:53: warning: Either the condition 's1?s1+1:&uuid[5]' is redundant or there is possible null pointer dereference: s1. [nullPointerRedundantCheck]
        if (!strcmp("00000000000000000000000000000000", s1) )
                                                        ^
    cppcheckrednullptr.c:13:13: note: Assuming that condition 's1?s1+1:&uuid[5]' is not redundant
        s1 = s1 ? s1 + 1 : &uuid[5];
                ^
    cppcheckrednullptr.c:13:13: note: Assignment 's1=s1?s1+1:&uuid[5]', assigned value is 0
        s1 = s1 ? s1 + 1 : &uuid[5];
                ^
    cppcheckrednullptr.c:16:53: note: Null pointer dereference
        if (!strcmp("00000000000000000000000000000000", s1) )
                                                        ^
     include <string.h>
    
    int main(){
        char uuid[128];
        char *s1;
    
        memset(uuid, 0, sizeof(uuid));
        s1 = strchr(uuid, '=');
        s1 = s1 ? s1 + 1 : &uuid[5];
    
        //if (s1) s1 = s1+1; else s1 = &uuid[5]; // warning if this line is added
        if (!strcmp("00000000000000000000000000000000", s1) )
            return -1;
    
        return 0;
    }
    
     
  • Daniel Marjamäki

    Thanks! Ouch thats not nice :-(. I created ticket https://trac.cppcheck.net/ticket/9842

     

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.