Greetings,
I am currently doing some network programming and ran into the problem of strict-aliasing when I wanted to cast a portion of an incoming buffer to a struct modeling a network header. Unfortunately, this violates strict-aliasing, and therefore has undefined behavior. I'm looking for the most efficient way to do this that is correct, portable, and well-defined.
It seems like common advice to use packed structs and unions for this kind of thing, but none of the resources I've found offer a definitive opinion on the safety or portability of doing so. Looking for real-world examples, I've found lwip and a case that matches what I described above. Concretely, in the udp_input function in udp.c a void * is cast to a udp_hdr *.
My questions are:
1. Am I correct in diagnosing this as a violation of strict aliasing and therefore undefined behavior?
2. If so, are the developers aware?
3. If so, what are the reasons for using this implementation?
There is an old thread in the archives relating to the subject, which seems to suggest that it is an issue:
Additionally, here is a more thorough write-up of my understanding of the issue as a stack-overflow question:
Thanks,
Sam Caldwell