Menu

False positive: Either the condition is redundant or there is pointer arithmetic with NULL pointer

raynebc
2019-09-03
2019-09-03
  • raynebc

    raynebc - 2019-09-03

    Staring in Cppcheck 1.89 I'm seeing a false positive that I can reproduce with this minimal example:

    void function(int value, char *string)
    {
        char *ptr1 = NULL, *ptr2 = NULL;
        unsigned long count = 0;
    
        if(!string)
            return;
        ptr1 = string;
        ptr2 = strrchr(string, 'a');    //The last instance
    
        if(ptr2 == NULL)
            return; //No such instances
    
        while(ptr1 < ptr2)
        {   //For each character up to the last instance of 'a'
            count++;
            ptr1++;
        }
    }
    

    I know Cppcheck can't assume the programmer is using pointers properly in such an example (ie. both are pointers to the same string), but the warning doesn't seem accurate because both pointers are already checked for NULL.

     
  • Daniel Marjamäki

     

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.