This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: No warning when breaking strict-aliasing rules despite -Wstrict-aliasing


Anders Torger <torger@ludd.luth.se> wrote:

>   I found a case where GCC does not warn even with
>   -Wstrict-aliasing, but makes incorrect asm code anyway.

> unsigned long
> hash(void *key)
> {
>     unsigned long long u[1];
>     u[0] = *(unsigned long long *)key;
>     return ((unsigned long *)u)[0] ^ ((unsigned long *)u)[1];
> }

There is absolutely no guarantee that we always generate a warning for every
possible type-aliasing violation. For instance, if you passed the pointer to a
double as "key" to this function, we could not possibly find it out and emit a
warning.

Nonetheless, there is a problem here: we could easily emit a warning on this
line:

>     return ((unsigned long *)u)[0] ^ ((unsigned long *)u)[1];

but we do not. The problem appears to be in c-typeck.c, in build_c_cast():

      if (TREE_CODE (type) == POINTER_TYPE
          && TREE_CODE (otype) == POINTER_TYPE
          && TREE_CODE (expr) == ADDR_EXPR
          && DECL_P (TREE_OPERAND (expr, 0))
          && flag_strict_aliasing && warn_strict_aliasing
          && !VOID_TYPE_P (TREE_TYPE (type)))

The code only warns for expressions like  "(cast*)&decl", but
"(cast*)array_decl" is also something to warn about.

This is a possible enhancement. Would you please file a bug report in Bugzilla
about this?

Thanks,
Giovanni Bajo



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]