With new gcc9 how to handle error from -Werror=address-of-packed-member

Martin Sebor msebor@gmail.com
Fri Feb 15 00:48:00 GMT 2019


On 2/14/19 3:46 AM, Normand wrote:
> 
> 
> On 14/02/2019 11:10, Florian Weimer wrote:
>> * Normand:
>>
>>> With new gcc9 there are new error reported by
>>> -Werror=address-of-packed-member
>>> How to handle such error ?
>>>
>>> eg:
>>> libflash/ecc.c:419:24: error: taking address of packed member of
>>> 'struct ecc64' may result in an unaligned pointer value
>>> [-Werror=address-of-packed-member]
>>>    419 |   memcpy(inc_uint64_by(&ecc_word.data, alignment), src,
>>> bytes_wanted);
>>>        |                        ^~~~~~~~~~~~~~

Isn't the problem here that the address of the unaligned ecc_word.data
is being passed to inc_uint64_by() declared to take a pointer to
a naturally aligned uint64_t:

   static uint64_t *inc_uint64_by(const uint64_t *p, uint64_t i)
   {
     return (uint64_t *)(((char *)p) + i);
   }

If the purpose of inc_uint64_by() is to align a pointer to a misaligned
integer (or something like that) then declaring it to take an argument
of that type should avoid the warning.  E.g., like so:

   typedef uint64_t unaligned_uint64_t __attribute__ ((aligned (1)));

   static uint64_t* inc_uint64_by (const unaligned_uint64_t *p, uint64_t i)
{
   return (uint64_t *)(((char *)p) + i);
}

(I'm just guessing here.  I have no idea if this is what the function
does, or if the return type should also be unaligned_int64_t.)

Martin

>>
>> What does the definition of struct ecc64 look like?
>>
>> Thanks,
>> Florian
>>
>>
> 
> Florian, the related struct is:
> struct ecc64 {
>      beint64_t data;
>      uint8_t ecc;
> } __attribute__((__packed__));
> 
> 
> as per header file from:
> https://github.com/open-power/skiboot/blob/skiboot-6.2.x/libflash/ecc.h#L25
> 



More information about the Gcc-help mailing list