Re: Strict aliasing and pointers to functions

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

 



malc writes:
 > On Sun, 26 Nov 2006, Andrew Haley wrote:
 > 
 > [..snip..]
 > > union
 > > {
 > >  void *barf;
 > >  void (*fptr) (void);
 > > } bilge;
 > >
 > >  bilge.barf = dlsym (NULL, "dummy");
 > >  funcs.dummy = bilge.fptr;
 > 
 > The only thing, conformance wise, that can be different here is the
 > fact that (according to the link bellow) type-prunning is allowed
 > for unions. This does not change the fact that `void *' and any
 > pointer to function are incompatible.

Are we talking ISO C or GNU C here?  This is OK for GNU C, and I think
for POSIX.

 > However i'm not targetting standard C

So why are you using -pedantic?  -pedantic is only for strict ISO C.

 > (i wouldn't know if it's possible to implement `dlsym' without
 > falling into undefined/specified behavior on such a system at all).
 > 
 > http://standards.ieee.org/reading/ieee/interp/1003.1-2001/1003.1-2001-04.html
 > > > P.S. It seems like i can use the tripple casting trick once again
 > > >         instead of straightforward `struct.field', but this again begs
 > > >         the question of portability/conformance.
 > >
 > > Why are you using the "triple casting trick"?  All it can possibly do
 > > is silence a warning; it won't make the code correct.
 > 
 > Less typing and:
 >   http://www.opensolaris.org/jive/thread.jspa?threadID=1923&tstart=0

Ths one, I guess:
...
* The cast below is the correct way to handle the problem.
* The (void *) cast is to avoid a GCC warning like:
* "warning: dereferencing type-punned pointer will break \
* strict-aliasing rules"
* which is wrong this code. (void *) introduces a compatible
* intermediate type in the cast list.
*/
count -= got, *(char **)(void *)&buffer += size * got;

I'm not convinced this hackery is correct by my reading of ISO C, and
in any case it's pointless.  It could be replaced by:

count -= got, buffer = (char*)buffer + (size * got);

Andrew.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux