On Sat, Jun 17, 2006 at 01:57:33PM +0200, Diego 'Flameeyes' Pettenò wrote:
> On Saturday 17 June 2006 13:43, Luca Barbato wrote:
> > you can use unions or rewrite completely the line using it in another
> > way, in certain case the type pun is the quickest solution so it's
> > better to append -fno-strict-aliasing in the Makefile.
> Err give me an example of the line, a lot of strict aliasing breakage was due 
> to double pointers.

Using your own example from
http://planet.gentoo.org/developers/flameeyes/2006/03/02/fixing_strict_aliasing_warnings

struct dl_node {
  struct dl_node *next;
  struct dl_node *prev;
};
struct dl_head {
  struct dl_node *first;
  struct dl_node *null;
  struct dl_node *last;
};
and then accessing {first, null}, or {null, last} as a struct dl_node
the way to fix the code would be to rewrite the code to use arrays
of pointers instead of simply three pointer members. There is no
valid way to do what the code does using pointer members (that's simple
logic: it relies on the padding between first and null to be exactly the
same as that between null and last, which is a guarantee standard C
doesn't make), so fixing it requires quite a bit of work.

If you don't mind keeping the code invalid, but in such a way that GCC
will compile it as intended, you could do

struct dl_head {
  union {
    struct {
      struct dl_node *first;
      struct dl_node *null;
      struct dl_node *last;
    };
    struct {
      struct dl_node firstnode;
      struct dl_node *dummy1;
    };
    struct {
      struct dl_node *dummy2;
      struct dl_node secondnode;
    };
  };
} h;

and then change
 (struct dl_node *) &h->first
to
 &h->firstnode

and similarly, change
 (struct dl_node *) &h->null
to
 &h->secondnode

Again, it's not valid, and it can still break if not handled with care
even with GCC.
-- 
gentoo-dev@gentoo.org mailing list

Reply via email to