Subject: bad typecasts

bad typecasts

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Sat, 18 Dec 2010 17:39:58 +0100 (CET)

Hi friends,

Stefan Bühler pointed out a flaw in his pull request he submitted at github:
https://github.com/bagder/c-ares/pull/2

I've already merged and pushed the actual fix, but his point about the ugly
casts remains right now. We should not have code that does things like:

   (taken from ares_parse_mx_reply.c))

   mx_curr->priority = ntohs (*((unsigned short *)vptr));

Since it may read unaligned data which is illegal on some platforms.

I think the smallest fix that still is decent, is to just change that line to:

   mx_curr->priority = (vptr[0] << 8) | vptr[1];

Any objections or suggestions on a different approach? I spotted at least
three more such typecasts in ares_parse_srv_reply.c

-- 
  / daniel.haxx.se
Received on 2010-12-18