alignment of ai_addr in struct addrinfo

Rick Macklem rmacklem at uoguelph.ca
Thu Jun 7 20:15:59 UTC 2018


I have been doing "make universe" for the first time in a long time and
I ran into an interesting one.
I had code that looked like:
    struct sockaddr_in *sin;
    struct addrinfo *res;

    - did a getaddrinfo() and then after this I had:
   ...
   sin = (struct sockaddr_in *)res->ai_addr;

For mips, it complained that the alignment requirement for "struct sockaddr_in"
is different than "struct sockaddr" related to the type cast.

I've worked around this by:
   struct sockaddr_in sin;
   ...
   memcpy(&sin, res->ai_addr, sizeof(sin));

Is this a real problem or a compiler quirk?

If it is real, it seems to me it would be nice if the alignment requirement for
"struct sockaddr" was the same as "struct sockaddr_in" and "struct sockaddr_in6".
Is there a "trick" that could be applied to "struct sockaddr" to force good alignment?

rick


More information about the freebsd-net mailing list