Source-Changes-D archive

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

Re: CVS commit: src/sys/arch/atari/stand/installboot



On Fri, Nov 14, 2014 at 02:43:39AM +0900, Izumi Tsutsui wrote:
> christos@ wrote:
> 
> > Module Name:	src
> > Committed By:	christos
> > Date:		Thu Nov 13 17:19:29 UTC 2014
> > 
> > Modified Files:
> > 	src/sys/arch/atari/stand/installboot: installboot.c
> > 
> > Log Message:
> > fix strict aliasing violations
> 
> > -	*((u_int16_t *)bb->bb_xxboot + 255) = 0;
> > -	*((u_int16_t *)bb->bb_xxboot + 255) = 0x1234 - abcksum(bb->bb_xxboot);
> > +	sum = 0;
> > +	memcpy(bb->bb_xxboot + 255, &sum, sizeof(sum));
> > +	sum = 0x1234 - abcksum(bb->bb_xxboot);
> > +	memcpy(bb->bb_xxboot + 255, &sum, sizeof(sum));
> 
> I doubt your "bb->bb_xxboot + 255" is the same place
> as the original "(u_int16_t *)bb->bb_xxboot + 255"
> (the cksum word looks at the end of 512 byte sector).
> 
> memcpy(9) looks also awful for readers because it hides endianness..

(Having read all the thread...)

I'm pretty sure you can cast between a pointer to a union and
a pointer to one of its mmembers.

So under the assumption that the compiler will let you alias between
members of a union (if it doesn't then far more stuff will break)
the following is safe:

Define a union that contains a 'bb' and a u_int16_t [].
Declare a variable that is a pointer to the union.
Assign (with cast) the address of 'bb' to the pointer variable.
Access the other member of the union.

The only other alternative is to do all the accesses as 'char'.

I'd guess that if the compiler inlines memcpy() it can 'know' about the
types of the variables involved - and apply the 'strict aliasing'
rules in the same way that is applied the 'alignment' ones.

	David

-- 
David Laight: david%l8s.co.uk@localhost


Home | Main Index | Thread Index | Old Index