Menu

False warning: The address of variable ... might be accessed at non-zero index.

2026-06-21
2026-06-22
  • Martin Poupě

    Martin Poupě - 2026-06-21

    Hello,
    following code:

    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    static void Check(unsigned c){if(!c){exit(1);}}
    static void CheckArr(uint8_t byCount, const uint8_t *pData)
    {
        uint8_t i,j;
        for(i = 0; i < byCount;i++)
        {
            for(j = i + 1; j < byCount;j++)
            {
                Check(pData[i] != pData[j]);
            }
        }
    }
    int main()
    {
        uint8_t x = 1;
        CheckArr(1,&x);
        return 0;
    }
    

    Causes warning:
    tmp.c:14:36: warning: The address of variable 'x' might be accessed at non-zero index. [objectIndex]
    Check(pData[i] != pData[j]);
    ^
    tmp.c:22:16: note: Address of variable taken here.
    CheckArr(1,&x);
    ^
    tmp.c:22:16: note: Calling function 'CheckArr', 2nd argument '&x' value is lifetime=x
    CheckArr(1,&x);
    ^
    tmp.c:14:36: note: The address of variable 'x' might be accessed at non-zero index.
    Check(pData[i] != pData[j]);
    ^
    Cppcheck 2.21
    I think it is a false warning, the inner loop is not called at all.

    Martin

     
  • CHR

    CHR - 2026-06-22

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

     

Log in to post a comment.

Auth0 Logo