something to warry about...

Denys Vlasenko vda.linux at googlemail.com
Mon Jan 17 02:29:20 UTC 2011


On Sunday 16 January 2011 23:45, Cristian Ionescu-Idbohrn wrote:
> On Sun, 16 Jan 2011, Denys Vlasenko wrote:
> > On Sunday 16 January 2011 22:58, Douglas Mencken wrote:
> > > Here - only using union.
> >
> > Can you show me *the code* how to do it?
> >
> > Any ways to do it *I* see are so ugly
> > I don't dare to post them.
> >
> > > You know, casting pointers doesn't mean that
> > > they point to the same area. And doing something like
> > > my_super_data_type* ptr = (void*)blah is obviously a way to void.
> 
> What about the articles Douglas posted?
>
> On Sat, 15 Jan 2011, Douglas Mencken wrote:
> >
> > Understanding Strict Aliasing:
> > http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html
> > Strict aliasing in C: http://xania.org/200712/cpp-strict-aliasing
>                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> I'm not a C-expert, but there seems to be something there that makes
> sense, does it not?

I need a concrete example how to make warning go away in this case:

static smallint detect_link_priv(void)
{
        struct ifreq ifreq;
        struct mii_ioctl_data *mii = (void *)&ifreq.ifr_data;
...
        mii->reg_num = 1;

Neither struct ifreq nor struct mii_ioctl_data are defined by busybox -
they come from kernel.
The only thing we know about struct ifreq in this code is that
it contains a char ifr_data[N] vector somewhere inside.

Can we redeclare structures with __attribute__(__may_alias__)?
No, because they are already declared.

Can we create a union where ifreq.ifr_data and mii overlap?
Well, not easily, because we don't know the offset of ifreq.ifr_data.
It can look like this:

union {
	struct ifreq ifreq;
	struct {
		char padding[offsetof(struct ifreq, ifr_data)];
		struct mii_ioctl_data mii;
	} s;
} u;
...
u.s.mii.reg_num = 1;

Does anyone think that this is acceptable (as in "not too ugly")?
Or even safe wrt alignment or possible #define tricks
with ifr_data name in system headers?

-- 
vda


More information about the busybox mailing list