Menu

The address of local variable x is accessed at non-zero index

Tom
2020-09-14
2021-01-30
  • Tom

    Tom - 2020-09-14

    I am using Cppcheck 2.1 and I get an error indicating 'the address of location variable "checksum" is accessed at non-zero index'.

    Here is the code:

    void functionA(void)
    {
        uint32_t checksum = adler32(checksum, buf_ptr, len);
    
        checksum = ((((uint8_t*)&checksum)[0u] << 24u) + (((uint8_t*)&checksum)[1u] << 16u) + (((uint8_t*)&checksum)[2u] << 8u) + ((uint8_t*)&checksum)[3u]);
    }
    

    This looks like a false positive to me?!

     

    Last edit: Tom 2020-09-14
  • CHR

    CHR - 2021-01-28

    I can reproduce this with v2.3 and head.

     
  • CHR

    CHR - 2021-01-30

    I have created a ticket: https://trac.cppcheck.net/ticket/10156

    Out of curiosity, I tried to rewrite that code to make it more readable. At least in clang, all the variants become equivalent at -O3: https://godbolt.org/z/6q49sY
    And I know that the union trick is UB, but the major compilers accept it anyway.

     

Log in to post a comment.