Menu

False positive: Array access out of bounds

2022-09-18
2022-09-18
  • Aleksander Zdunek

    I get this error output when when running following minimal test program through cppcheck (2.9). I think this is incorrect.

    Checking test.c ...
    test.c:6:23: error: Array 's[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]
            printf("%c", s[i]);
                          ^
    

    test.c:

    #include <stdio.h>
    
    void fun(const char *s)
    {
        for(int i = 0; s[i]; ++i)
            printf("%c", s[i]);
    }
    
    int main()
    {
        fun("abc");
        fun("a");
        return 0;
    }
    

    If I swap the order of the two fun() calls in main(), or reduce the length difference between the passed strings to one character, I do not get the error.

     
  • CHR

    CHR - 2022-09-18

    Thanks for reporting. It looks like this has been fixed in head.

     

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.