This is the mail archive of the gcc-patches@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: [PATCH] C/C++: Add -Waddress-of-packed-member


On Sat, Jan 20, 2018 at 8:31 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Fri, Jan 19, 2018 at 7:57 PM, Martin Sebor <msebor@gmail.com> wrote:
>> On 01/19/2018 10:14 AM, Martin Sebor wrote:
>>>
>
>> After reading the Clang code review for the warning
>> (https://reviews.llvm.org/D20561) and experimenting with a few
>> more test cases I noticed some additional false negatives that
>> I think would be worthwhile diagnosing:
>>
>>   struct A {
>>     int i;
>>   } __attribute__ ((packed));
>>
>>   int f (struct A *p)
>>   {
>>     return *&p->i;
>>   }
>
> I now got
>
> [hjl@gnu-tools-1 pr51628]$ cat a1.i
> struct A {
>   int i;
> } __attribute__ ((packed));
>
> int f (struct A *p)
> {
>   return *&p->i;
> }
> [hjl@gnu-tools-1 pr51628]$ make a1.s
> /export/build/gnu/gcc/build-x86_64-linux/gcc/xgcc
> -B/export/build/gnu/gcc/build-x86_64-linux/gcc/ -O2 -S a1.i
> a1.i: In function ‘f’:
> a1.i:7:10: warning: taking address of packed member of ‘struct A’ may
> result in an unaligned pointer value [-Waddress-of-packed-member]
>    return *&p->i;
>           ^~~~~~
> [hjl@gnu-tools-1 pr51628]$


It is wrong to warn "*&p->i;" since GCC always converts to "p->i;".  Unless
we want to warn

[hjl@gnu-tools-1 pr51628]$ cat g5.i
struct A {
  int i;
} __attribute__ ((packed));

int f (struct A *p)
{
  return p->i;
}

we shouldn't warn:

[hjl@gnu-tools-1 pr51628]$ cat a1.i
struct A {
  int i;
} __attribute__ ((packed));

int f (struct A *p)
{
  return *&p->i;
}
[hjl@gnu-tools-1 pr51628]$

There is no unaligned load here.  I am testing a new patch.

-- 
H.J.


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