PATCH: Add -Waddress-of-packed-member to GCC 9 porting guide

H.J. Lu hjl.tools@gmail.com
Mon Jan 14 14:14:00 GMT 2019


On Mon, Jan 14, 2019 at 5:53 AM Richard Biener
<richard.guenther@gmail.com> wrote:
>
> On Mon, Jan 14, 2019 at 2:46 PM H.J. Lu <hongjiu.lu@intel.com> wrote:
> >
> > This patch adds -Waddress-of-packed-member to GCC 9 porting guide.
> >
> > OK to install?
>
> The docs fail to mention what to do when the unaligned pointer is _not_
> safe to use.  That is, how do I fix
>
> struct { char c; int i[4]; } s __attribute__((packed));
> int foo()
> {
>   int *p = s.i;
>   return bar (p);
> }
> int bar (int *q)
> {
>   return *q;
> }
>
> for the cases where eliding the pointer isn't easily possible?

You can't have both packed struct and aligned array at the same time.
The only thing I can say is "don't do it".

> Please also mention the new warning in changes.html
> (it seems to be enabled by default even?).

I will add a paragraph.

H.J.
> IIRC the frontends themselves build "bogus" pointer types
> to aligned data from a simple &s.i[1] because the FIELD_DECLs
> types are naturally aligned.
>
> Richard.
>
> > Thanks.
> >
> > H.J.
> > ---
> > Index: gcc-9/porting_to.html
> > ===================================================================
> > RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-9/porting_to.html,v
> > retrieving revision 1.1
> > diff -u -r1.1 porting_to.html
> > --- gcc-9/porting_to.html       11 Jan 2019 18:21:45 -0000      1.1
> > +++ gcc-9/porting_to.html       14 Jan 2019 13:46:07 -0000
> > @@ -56,13 +56,36 @@
> >        }
> >    </code></pre>
> >
> > +<h2 id="c/cxx">C/C++ language issues</h2>
> > +
> > +<h3 id="Waddress-of-packed-member"><code>-Waddress-of-packed-member</code>
> > +is enabled by default</h3>
> > +
> > +<p>
> > +  When address of packed member of struct or union is taken, it may result
> > +  in an unaligned pointer value.  A new warning
> > +  <code>-Waddress-of-packed-member</code> was added to check alignment at
> > +  pointer assignment.  It warns both unaligned address and unaligned
> > +  pointer.
> > +</p>
> > +
> > +<p>
> > +  If the pointer value is safe to use, you can suppress
> > +  <code>-Waddress-of-packed-member</code> warnings by using pragmas:
> > +</p>
> > +  <pre><code>
> > +    #pragma GCC diagnostic push
> > +    #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
> > +    /* (code for which the warning is to be disabled)  */
> > +    #pragma GCC diagnostic pop
> > +  </code></pre>
> > +
> >  <!--
> >  <h2 id="cxx">C++ language issues</h2>
> >  -->
> >
> >  <!--
> >  <h2 id="fortran">Fortran language issues</h2>
> > --->
> >
> >  <!--
> >  <h2 id="links">Links</h2>



-- 
H.J.



More information about the Gcc-patches mailing list