A variable-length array pointer 'dereference' inside sizeof() is causing GCC to
give an aliasing warning. I can think of no reason why completely
compiletime-static sizeof() activity could create an aliasing violation.

I've included a contrived example showing 4.5.0 producing the warning while
4.4.1 does not complain:

[gmaxw...@floodlamp tmp]$ cat test.c
#include <string.h>
int main(int argc, char *argv[])
{
    float x[argc];
    float y[argc];
    return NULL == memcpy(y, x, argc * sizeof(*x));
}
[gmaxw...@floodlamp tmp]$ /usr/local/bin/gcc -std=gnu99 -O2 -Wall -o test
test.c
test.c: In function ‘main’:
test.c:6:5: warning: dereferencing type-punned pointer will break
strict-aliasing rules
[gmaxw...@floodlamp tmp]$ /usr/local/bin/gcc -v
Using built-in specs.
COLLECT_GCC=/usr/local/bin/gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.5.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --with-ppl --with-cloog --with-gmp --enable-lto
Thread model: posix
gcc version 4.5.0 20091008 (experimental) (GCC) 
[gmaxw...@floodlamp tmp]$ /usr/bin/gcc -std=gnu99 -O2 -Wall -o test test.c
[gmaxw...@floodlamp tmp]$ /usr/bin/gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib
--with-ppl --with-cloog --with-tune=generic --with-arch_32=i586
--build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC) 

The -O2 plus sizeof(*x) anywhere where x is a var-array is necessary and
sufficient to trigger this. Switching to a statically sized array avoids the
warning.

The actual use-case where I hit this is where sizeof() is used in this manner
when a type is selected by some compile time configuration and manually
restating the type in sizeof() would require a great many more ifdefs.


-- 
           Summary: 4.5.0 20091008 variable-length array dereference inside
                    sizeof gives "warning: dereferencing type-punned pointer
                    will break strict-aliasing rules"
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gmaxwell at gmail dot com
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


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

Reply via email to