Menu

arrayIndexOutOfBounds: false positive for valid array

2 days ago
2 days ago
  • Marcin Pytel

    Marcin Pytel - 2 days ago

    I'm using cppcheck 2.18. This is code:

    struct S {
        int i;
    };
    
    enum {
        ZERO,
        ONE,
        TWO,
        THREE,
        FOUR,
        FIVE
    };
    
    struct S arr[] = {
        [ZERO] = {},
        [ONE] = {},
        [TWO] = {},
        [THREE] = {},
        [FOUR] = {},
        [FIVE] = {},
        [FIVE + 1] = {},
        [FIVE + 2] = {},
        [FIVE + 3] = {}
    };
    
    void foo()
    {
        struct S *s;
    
        _Static_assert(sizeof(arr) / sizeof(*arr) == 9, "assert 1");
        _Static_assert(FIVE == 5, "assert 2");
    
        for (int i = 0; i < FIVE + 3; i++) {
                s = &arr[i];
        }
    }
    

    When running cppcheck the output is:

    error: Array 'arr[6]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]
    
     
  • CHR

    CHR - 2 days ago

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14296

     

Log in to post a comment.