Busybox 1.16.x dnsd alignment problems

Joakim Tjernlund joakim.tjernlund at transmode.se
Thu Apr 15 11:08:59 UTC 2010


>
> Unfortunately
> # define move_from_unaligned16(v, u16p) {void* src=(void*)(u16p);memcpy(&(v), src, 2);}
>
> also didn't do it. The only solution I've found working is doing it the
> hard way by hand:
>
> # define move_from_unaligned16(v, u16p) do { \
>         *((uint8_t*)&v) = *((uint8_t*)u16p); \
>         *(((uint8_t*)&v)+1) = *(((uint8_t*)u16p)+1); \
> } while (0)

I think something like below would be cleaner:

union uu {
	unsigned short us;
	unsigned char b[2];
};

/* Endian independed version */
static inline unsigned short
get_unaligned16(void *p)
{
	union uu  mm;
	unsigned char *b = (unsigned char *)p;

	mm.b[0] = b[0];
	mm.b[1] = b[1];
	return mm.us;
}

and then adapt the code to it.
Or look at how the linux kernel does it.



More information about the busybox mailing list