On 30/06/14, Markus Teich wrote:
Heyho,

Hello there,

since I did not find any suckless project regarding this issue, I
would like to ask you guys for some feedback:


unsigned char *msg;
size_t msg_size;
struct foo *msg_data;
struct bar *msg_signature;

msg_size = sizeof(unsigned char)        // op
        + sizeof(struct foo)    // data
        + sizeof(struct bar)    // signature
msg = malloc(msg_size);

*msg = MSG_OP_SIGNED_DATA;

msg_data = (struct foo *)(msg + 1);

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.

You probably want to memcpy the struct in from an existing one.

msg_data->field0 = bla;
msg_data->field1 = blub;

msg_signature = (struct bar *)(msg_data + 1);
create_signature(msg, msg_signature);

sendmsg(msg);

free(msg);


I feel it is pretty good already compared to other message packing
I've seen, but do you know a way to make it suck even less?

Seems pretty straightforward otherwise :)

Cheers,
Rob

Reply via email to