Menu

false positive in cppcheck 2.5: pointerOutOfBounds

Jo
2021-07-16
2021-07-17
  • Jo

    Jo - 2021-07-16

    Not sure if this has been reported by before, but there's another false positive in cppcheck 2.5 when run with --enable=portability. The test case is:

    #include <stdio.h>  
    int main(int argc, const char* argv[]) {
        argv++;
        if (argc>0) printf("%s\n", (argv-1)[0]);
        return 0;
    }
    

    Code compiles fine and is perfectly valid C:

    # gcc foo3.c -o foo3 && ./foo3 
    ./foo3
    

    ...but cppcheck complains:

    # cppcheck --enable=portability foo3.c 
    Checking foo3.c ...
    foo3.c:4:37: portability: Undefined behaviour, pointer arithmetic 'argv-1' is out of bounds. [pointerOutOfBounds]
        if (argc>0) printf("%s\n", (argv-1)[0]);
                                        ^
    
     
  • Daniel Marjamäki

     
    👍
    1

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.