Re: [dev] network protocol packing

From: Dimitris Papastamos <sin_AT_2f30.org>
Date: Tue, 1 Jul 2014 16:19:20 +0100

On Tue, Jul 01, 2014 at 05:01:43PM +0200, Markus Teich wrote:
> Rob wrote:
> > You've got alignment issues here - msg will be aligned to support any
> > type (as malloc's interface specifies) so msg+1 will most likely be on
> > an odd address, one byte off a highly aligned address. This means if
> > your struct contains anything other than chars, you'll have UB. This is
> > fine on x86, which allows unaligned access with a performance penalty
> > but on something like an ARM machine you'll have issues.
>
> Heyho,
>
> so if every field in the message is a multiple of 4 bytes long, this should fix
> the problem? In this case I would just make op an uint32_t and htonl it before
> sending, ntohl after receiving.

Do not assume that. It is wrong in the general case.
Received on Tue Jul 01 2014 - 17:19:20 CEST

This archive was generated by hypermail 2.3.0 : Tue Jul 01 2014 - 17:24:06 CEST