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

            Bug ID: 89132
           Summary: missing -Wcast-align casting the address of a function
                    to a more aligned pointer type
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The -Wcast-align warning is documented to

    Warn whenever a pointer is cast such that the required alignment of the
target is increased.  For example, warn if a char * is cast to an int * on
machines where integers can only be accessed at two- or four-byte boundaries.

The strict form of the warning is documented to trigger "regardless of the
target machine."

The following test case shows that the warning fails to trigger when the
address of a function is cast to a pointer to a type with a stricter alignment.
 The test case was compiled for the strictly aligned SPARC but the same problem
affects even x86.

As an aside, it would be helpful to include the alignment in the diagnostic so
that users can more easily  tell what the expectations are and by how much
they're off.

$ cat u.c && /build/sparc-solaris2.11/gcc-svn/gcc/xgcc -B
/build/sparc-solaris2.11/gcc-svn/gcc -S -Wall -Wextra -Wcast-align=strict
-o/dev/stdout -xc u.c
int i = 1;
long long *p = (long long*)&i;   // -Wcast-align (good)

void f (void) { }
long long *q = (long long*)f;   // missing -Wcast-align

        .file   "u.c"
u.c:2:16: warning: cast increases required alignment of target type
[-Wcast-align]
    2 | long long *p = (long long*)&i;   // -Wcast-align (good)
      |                ^
        .section        ".text"
        .global i
        .section        ".data"
        .align 4
        .type   i, #object
        .size   i, 4
i:
        .long   1
        .global p
        .align 4
        .type   p, #object
        .size   p, 4
p:
        .long   i
        .section        ".text"
        .align 4
        .global f
        .type   f, #function
        .proc   020
f:
        save    %sp, -96, %sp
        nop
        return  %i7+8
         nop
        .size   f, .-f
        .global q
        .section        ".data"
        .align 4
        .type   q, #object
        .size   q, 4
q:
        .long   f
        .ident  "GCC: (GNU) 9.0.0 20190118 (experimental)"

Reply via email to