lwip-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-devel] Concern over strict aliasing and undefined behavior?


From: Sam Caldwell
Subject: [lwip-devel] Concern over strict aliasing and undefined behavior?
Date: Mon, 7 Oct 2013 10:40:18 -0500

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:
http://lists.nongnu.org/archive/html/lwip-devel/2008-10/msg00012.html

Additionally, here is a more thorough write-up of my understanding of the issue as a stack-overflow question:
http://stackoverflow.com/questions/19165134/correct-portable-way-to-interpret-buffer-as-a-struct

Thanks,
Sam Caldwell

reply via email to

[Prev in Thread] Current Thread [Next in Thread]