1 |
On Sat, Jun 17, 2006 at 01:57:33PM +0200, Diego 'Flameeyes' Pettenò wrote: |
2 |
> On Saturday 17 June 2006 13:43, Luca Barbato wrote: |
3 |
> > you can use unions or rewrite completely the line using it in another |
4 |
> > way, in certain case the type pun is the quickest solution so it's |
5 |
> > better to append -fno-strict-aliasing in the Makefile. |
6 |
> Err give me an example of the line, a lot of strict aliasing breakage was due |
7 |
> to double pointers. |
8 |
|
9 |
Using your own example from |
10 |
http://planet.gentoo.org/developers/flameeyes/2006/03/02/fixing_strict_aliasing_warnings |
11 |
|
12 |
struct dl_node { |
13 |
struct dl_node *next; |
14 |
struct dl_node *prev; |
15 |
}; |
16 |
struct dl_head { |
17 |
struct dl_node *first; |
18 |
struct dl_node *null; |
19 |
struct dl_node *last; |
20 |
}; |
21 |
and then accessing {first, null}, or {null, last} as a struct dl_node |
22 |
the way to fix the code would be to rewrite the code to use arrays |
23 |
of pointers instead of simply three pointer members. There is no |
24 |
valid way to do what the code does using pointer members (that's simple |
25 |
logic: it relies on the padding between first and null to be exactly the |
26 |
same as that between null and last, which is a guarantee standard C |
27 |
doesn't make), so fixing it requires quite a bit of work. |
28 |
|
29 |
If you don't mind keeping the code invalid, but in such a way that GCC |
30 |
will compile it as intended, you could do |
31 |
|
32 |
struct dl_head { |
33 |
union { |
34 |
struct { |
35 |
struct dl_node *first; |
36 |
struct dl_node *null; |
37 |
struct dl_node *last; |
38 |
}; |
39 |
struct { |
40 |
struct dl_node firstnode; |
41 |
struct dl_node *dummy1; |
42 |
}; |
43 |
struct { |
44 |
struct dl_node *dummy2; |
45 |
struct dl_node secondnode; |
46 |
}; |
47 |
}; |
48 |
} h; |
49 |
|
50 |
and then change |
51 |
(struct dl_node *) &h->first |
52 |
to |
53 |
&h->firstnode |
54 |
|
55 |
and similarly, change |
56 |
(struct dl_node *) &h->null |
57 |
to |
58 |
&h->secondnode |
59 |
|
60 |
Again, it's not valid, and it can still break if not handled with care |
61 |
even with GCC. |
62 |
-- |
63 |
gentoo-dev@g.o mailing list |