https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86259

--- Comment #5 from Vladimir Panteleev <gcc at thecybershadow dot net> ---
(In reply to Andrew Pinski from comment #2)
> Note gcc thinks strlen(s) is less than or equal to 3 as s is really T.s
> which is an array of 4 in size and there for the last element has to be a
> null char.

Hmm. Here is a simpler example which illustrates this:

/////////////////// test.c ///////////////////
#include <stdio.h>
#include <string.h>

struct S
{
    int x[1];
};

union U
{
    struct S arr[64];
    char s[256];
};

int main()
{
    union U u;
    strcpy(u.s, "abcdefghijklmnopqrstuvwxyz");
    size_t len = strlen((char*)&u.arr[1].x);
    puts(len > 10 ? "YES" : "NO");
    return 0;
}
//////////////////////////////////////////////

This prints "NO" with -O1 and above. clang always prints "YES".

Are you sure this is an optimization the compiler is allowed to make, though? I
would think that the explicit cast to char* removes all bets as to how long the
string really is.

Reply via email to