[cfe-users] Clang Analyzer: false positive or am I missing something?

Jeffrey Walton via cfe-users cfe-users at lists.llvm.org
Sat Jun 25 18:25:53 PDT 2016


On Sat, Jun 25, 2016 at 2:01 PM, Andrew Fuller via cfe-users
<cfe-users at lists.llvm.org> wrote:
> I'm trying to understand an issue reported by Clang's static analysis tool.
> The code below demonstrates the issue:
>
> $ cat problem.c
> #include <stdint.h>
>
> int main() {
> #if VARIANT==1
>    uint32_t data = 0xdeadbeef;
>    uint8_t* byte = (uint8_t*)&data;
>    uint8_t value = byte[0];
> #elif VARIANT==2
>    uint32_t data = 0xdeadbeef;
>    uint8_t* byte = (uint8_t*)&data;
>    uint8_t value = byte[1];
> #elif VARIANT==3
>    uint32_t data[1] = {0xdeadbeef};
>    uint8_t* byte = (uint8_t*)&data[0];
>    uint8_t value = byte[0];
> #elif VARIANT==4
>    uint32_t data[1] = {0xdeadbeef};
>    uint8_t* byte = (uint8_t*)&data[0];
>    uint8_t value = byte[1];
> #else
> #error "Define VARIANT={1,2,3,4}"
> #endif
>    return value;
> }
>

I recall seeing this before, but I don't recall if its valid C:

    uint8_t* byte = (uint8_t*)&data;
    uint8_t value = byte[0];

Are you getting other compiler warnings with it? If not, try -Wall -Wextra.

Jeff


More information about the cfe-users mailing list