A field in a packed struct will often not have the normal alignment for its
type.  So taking the address of such a field may yield a misaligned pointer,
but gcc does not warn about that.  This is similar to casting a pointer from a
less aligned type to a more aligned type, which does give a warning (with
-Wcast-align).

Example:
> cat aopf.c
#include <stdio.h>

void __attribute__((noinline)) f(int *p)
{
    printf("%p\n", p);
}

struct s {
    int x;
    char c;
} __attribute__((__packed__));

struct s A[10];

int main(void)
{
    unsigned int i;

    for (i = 0; i < sizeof(A)/sizeof(A[0]); ++i) {
        f(&A[i].x);
        f((int*)(char*)&A[i].x);
    }    
    return 0;
}
> gcc -Wall -Wextra -Wcast-align -O aopf.c
aopf.c: In function 'main':
aopf.c:21:4: warning: cast increases required alignment of target type

Ideally both calls to f() should trigger alignment warnings.


-- 
           Summary: escaping address of packed field should trigger warning
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: other
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mikpe at it dot uu dot se


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

Reply via email to