Re: [PATCH] Altix system controller event handling

From: Andreas Schwab <schwab_at_suse.de>
Date: 2005-03-10 21:57:08
Christoph Hellwig <hch@infradead.org> writes:

>> +static unsigned int
>> +scdrv_buffer_to_int(char *buf)
>> +{
>> +	int i;
>> +	unsigned int n = 0;
>> +	for( i = 0; i < sizeof(n); i++ ) {
>> +		n |= ((unsigned)(*(unsigned char *)buf++)
>> +		      << (8 * ((sizeof(n) - i) - 1)));
>
> urgg.  the (*(unsigned char *)buf++) should be just *(buf++), no?
> address arithmetics on signed and unsigned char are the same.

But not value promotion.  The latter gives you a different value when *buf
is negative.  For example if *buf == -1 then (unsigned)*buf == 0xffffffff,
but (unsigned char)*buf == 0xff.

> So
> 	for (i = 0; i < sizeof(n); i++)
> 		n |= ((unsigned int)buf[i] << (8 * (sizeof(n) - i - 1)));
>
> should do the same, no?

This should be better:

 	for (i = 0; i < sizeof(n); i++)
 		n |= ((unsigned char)buf[i] << (8 * (sizeof(n) - i - 1)));

Or even better: replace scdrv_buffer_to_int by be32_to_cpup.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
-
To unsubscribe from this list: send the line "unsubscribe linux-ia64" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Received on Thu Mar 10 05:58:58 2005

This archive was generated by hypermail 2.1.8 : 2005-08-02 09:20:36 EST