Menu

[pointerOutOfBounds] false positive

Fan Zhang
2022-06-29
2022-06-29
  • Fan Zhang

    Fan Zhang - 2022-06-29

    How to reproduce:

    #include "stdio.h"
    
    void foo(const char* message, short length)
    {
        const char *end = message + length;
        printf("%s, %d\n", message, *end);
    }
    
    int main()
    {
        foo("a", 1); // Remove this line the error is gone
        foo("bbb", 3);
    }
    

    Output

    cppcheck.exe --enable=portability test.c 
    Checking test.c ...
    test.c:6:31: portability: Undefined behaviour, pointer arithmetic 'message+length' is out of bounds. [pointerOutOfBounds]
        const char *end = message + length;
                                                                                  ^
    test.c:13:16: note: Calling function 'foo', 2nd argument '3' value is 3
        foo("bbb", 3);
                                       ^
    test.c:6:31: note: Pointer arithmetic overflow
        const char *end = message + length;
                                                                                  ^
    

    It seems it uses the size of "a" to check "bbb"

     
  • CHR

    CHR - 2022-06-29

    Thanks for reporting. I think this issue is covered by https://trac.cppcheck.net/ticket/11026

     

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.