This Time Self-Hosted
dark mode light mode Search

Fixing strict aliasing warnings

So, up to now I usually responded to strict aliasing breakage from programs with adding -fno-strict-aliasing to be safe.

In the last days I tried, on Donnie’s suggestion, to investigate of possible solutions to those strict aliasing rules breakage and see if I can fix at least some of them.

I found interestingly that most of the cases that can be found patched on google, and that causes the warnings are not actually involving structs and unions as the gcc documentation shows, but rather double pointers. What’s a double pointer? A pointer to a pointer, of course!

Taking a look to XdTV, I found a quite interesting thing: there is a double-linked list implementation that comes out of AleVT code, that is used by XdTV too. The cause of the warnings come out of lines 20 and 22. I’ll look only at line 20:

h->first = (struct dl_node *)&h->null;

well seems fine no? But wait… &h->null is a struct dl_node **

And later on, h->first is going to be used as a struct dl_node *

So for some kind of miracle this seems to work, but I’m not that sure that this is something that’s going to work forever.

And there are other casts that works probably only for sheer luck in AleVT’s code. Upstream seems to be dead since 2 years (last update on the site is January 2004), and we don’t have really good alternatives to it…
If someone wants to write something useful to people.. probably a replacement for alevt using zvbi would be useful 😉

Comments 1
  1. [So for some kind of miracle this seems to work, but I’m not that sure that this is something that’s going to work forever.]if works because instruct dl_head{ struct dl_node *first; struct dl_node *null; struct dl_node *last;};you can view first+null as one dl_node and null+last as another one.the two overlapping.It is unlikely to break, even under the anti-aliasing rules.I guess you could fool the compiler with something likestatic inline struct dl_head* dl_init(struct dl_head* h){struct dl_node* n; n = (struct dl_node*)h; /* aliasing is verbotten, but not regular casting right ? */ h->last = n; /* &h->first is in fact h */ n += 1; /* n now is &h->null */ h->first = n h->null = NULL; return h;}it should work becasue there is no reason for the compiler to generate ‘gap’ between the members in the structure dl_head – no processor require alignment bigger than sizeof(void*).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.