ANNOUNCEMENT: Live Wireshark University & Allegro Packets online APAC Wireshark Training Session
July 17th, 2024 | 10:00am-11:55am SGT (UTC+8) | Online

Ethereal-dev: Re: [Ethereal-dev] A gcc 3.3 warning I don't understand

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Guy Harris <guy@xxxxxxxxxxxx>
Date: Sun, 14 Sep 2003 17:52:32 -0700
On Mon, Sep 15, 2003 at 12:10:25AM +0200, Joerg Mayer wrote:
> I don't understand what it it talking about: I have read the manpage
> (excerpt included below) but still haven't really understood what
> type-punned means,

It means referring to an object of one type as if its raw in-memory data
representation were the raw in-memory data representation of an object
of another type, e.g., in packet-dns.c, copying some bytes from a packet
into an array of "guint8", and then treating that array as if it were an
IPv6 address:

		ip6_to_str((struct e_in6_addr *)&suffix),

The GCC documentation refers to doing that by copying the bytes to one
member of a union and referring them as another member of the union, but
you can do that by casting pointers as well.

> nor what strict-aliasing (or aliasing at all) means.

"Aliasing" refers to more than one pointer pointing to the same object
in memory - the pointers are "aliases", or, in a sense, alternate names
for the same object.  Aliasing can cause problems for optimizing
compilers, because, if they have multiple pointers and don't know which
pointers are *not* pointing to the same object, they might have to
assume that they might be, so that if, for example, a value was copied
using one pointer, and the copy is still accessible, but a value is
stored using another pointer, the copy might not still be equal to the
first value, so, if the first value is needed, it might have to fetch it
through the pointer again, rather than using the copy.

The strict aliasing rules say that a pointer to an object of type "foo"
won't be an alias of a pointer to an object of type "bar" if "foo" and
"bar" aren't "almost the same", so that if you have a pointer to a
"struct foo" and a pointer to a "struct bar", you can assume the
pointers don't point to the same thing.

Unfortunately, type-punning can violate those rules.

A Google search for "type punning" turned up a bunch of discussion of
what I presume are those GCC warnings, and checkins to various
free-software programs to suppress those warnings.