http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46702

           Summary: inlining generates strict-aliasing warnings
           Product: gcc
           Version: 4.4.5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: g...@fastmail.fm


Inlining a function which does dereferences cast pointers can cause
strict-aliasing warnings, where the same function not inlined causes
no warnings.  This is surprising and counterintuitive; one would expect
the function would be correct or not regardless of the inlining context.

Test case:
#include <stdint.h>

extern int baz(const char *);

#ifdef NOWARNINGS
void foo(char *p) __attribute__((noinline));
#endif

void foo(char *p)
{
    *(uint32_t *)p = 0x42;
}

void bar(void)
{
    union { char c[4]; uint64_t alignment; } buf;
    char *x = (char *)&buf;
    foo(x);
    baz(x);
}


m...@mybox> gcc -c -o foo.o -O2 -Wall -Wextra foo.c
foo.c: In function ‘bar’:
foo.c:11: warning: dereferencing pointer ‘p.0’ does break strict-aliasing rules
foo.c:11: note: initialized from here

m...@mybox> gcc -c -o foo.o -O2 -Wall -Wextra -DNOWARNINGS foo.c

m...@mybox> gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Seen in the wild in this code:
http://git.cyrusimap.org/cyrus-imapd/tree/imap/mbdump.c#n109

Reply via email to