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

Andrew Fuller via cfe-users cfe-users at lists.llvm.org
Sat Jun 25 18:51:20 PDT 2016


On Sat, Jun 25, 2016 at 6:25 PM, Jeffrey Walton <noloader at gmail.com> wrote:

> 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
>

Thanks for the reply, Jeff.  Clang 3.8 is warning-free with -Wall and
-Weverything going through all 4 code paths (VARIANT set to 1-4).  Ditto
for gcc 5.2.1 with -Wall -Wextra.  Only scan-build grumbles.

-Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20160625/fa98be6d/attachment.html>


More information about the cfe-users mailing list