There are now two remaining warnings.

Ihno Krumreich ihno at suse.de
Wed Sep 7 14:55:59 UTC 2005


On Wed, Sep 07, 2005 at 02:11:12PM +0200, Aurelien Jacobs wrote:
> On Wed, 7 Sep 2005 02:09:29 -0500
> Rob Landley <rob at landley.net> wrote:
> 
> > > /home/landley/busybox/busybox/networking/udhcp/options.c:160:
> > > warning: dereferencing type-punned pointer will break
> > > strict-aliasing rules 
> > 
> > This looks like an endianness problem to me.  (What's wrong with doing
> > this  through &mask |value the way everybody else does it?)
> 
> I think it's more related to alignment of different pointer types.
> What about the following patch ?
> 
> Aurel
> --- options.c.orig	2005-09-07 14:00:55.000000000 +0200
> +++ options.c	2005-09-07 14:06:48.000000000 +0200
> @@ -152,13 +152,11 @@
>  	char length = 0;
>  	int i;
>  	uint8_t option[2 + 4];
> -	uint8_t *u8;
> -	uint16_t *u16;
> -	uint32_t *u32;
> -	uint32_t aligned;
> -	u8 = (uint8_t *) &aligned;
> -	u16 = (uint16_t *) &aligned;
> -	u32 = &aligned;
> +	union {
> +		uint8_t u8;
> +		uint16_t u16;
> +		uint32_t u32;
> +	}aligned;
>  
>  	for (i = 0; dhcp_options[i].code; i++)
>  		if (dhcp_options[i].code == code) {
> @@ -174,9 +172,9 @@
>  	option[OPT_LEN] = length;
>  
>  	switch (length) {
> -		case 1: *u8 =  data; break;
> -		case 2: *u16 = data; break;
> -		case 4: *u32 = data; break;
> +		case 1: aligned.u8 =  data; break;
> +		case 2: aligned.u16 = data; break;
> +		case 4: aligned.u32 = data; break;
>  	}
>  	memcpy(option + 2, &aligned, length);
>  	return add_option_string(optionptr, option);

Hi,

why not just write:

		case 1: *u8 =  (uint8_t)data; break;
		case 2: *u16 = (uint16_t)data; break;
		case 4: *u32 = (uint32_t)data; break;


Regards

Ihno

"Never trust a computer you can lift."
--
Ihno Krumreich            ihno at suse.de
SUSE LINUX Products GmbH  Projectmanager S390 & zSeries
Maxfeldstr. 5             +49-911-74053-439
D-90409 Nürnberg          http://www.suse.de



More information about the busybox mailing list