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

--- Comment #10 from stephane.goujet at wanadoo dot fr ---
(In reply to stephane.goujet from comment #9)

> 2. There are inconsistencies in the Warning:

Another inconsistency:

2.c The documentation of the packed attribute says "This attribute, attached to
a struct, [...] is equivalent to specifying the packed attribute on each of the
members." Well, guess what? Waddress-of-packed-member is triggered when the
attribute is at the struct level, but is not triggered when it is at members
level.


  Example code:

---------------------------
# 1 "indiv2.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "indiv2.c"


struct __attribute__((packed)) Sw {
 char a;
 int b;
};


struct Si {
 char a __attribute__((packed));
 int b __attribute__((packed));
};

int main(void) {
 struct Sw sw;
 struct Si si;

 int * pw = (int *) &sw;
 int * pi = (int *) &si;

 return 0;
}
---------------------------


  Compilation result (setting aside the Wattributes warning,
Waddress-of-packed-member warns for Sw, not for Si):

---------------------------
~/test/c/gcc9packed $LC_ALL=C gcc -Wextra indiv2.c -o indiv2_gcc -save-temps
indiv2.c:10:2: warning: 'packed' attribute ignored for field of type 'char'
[-Wattributes]
   10 |  char a __attribute__((packed));
      |  ^~~~
indiv2.c: In function 'main':
indiv2.c:18:2: warning: converting a packed 'struct Sw' pointer (alignment 1)
to a 'int' pointer (alignment 4) may result in an unaligned pointer value
[-Waddress-of-packed-member]
   18 |  int * pw = (int *) &sw;
      |  ^~~
indiv2.c:3:32: note: defined here
    3 | struct __attribute__((packed)) Sw {
      |                                ^~
---------------------------

Reply via email to