Poll: does this violate strict aliasing? struct b { int x; } S; int *p = (int*)&S; *p = 1;
(*(struct sockaddr *)sin).sa_family is not the same as *(sa_family_t *)sa. Latter is well-defined C but assumes sa_family is first member.
-
-
The former is an aliasing violation because it accesses the object pointed to by sin with the wrong type, struct sockaddr.
-
ah, but accessing the family directly as (sa_family_t*)sockaddr would be okay I guess.
-
Yes, but I don't think POSIX imposes a requirement that sa_family be the first member...
-
You could do *(sa_family_t*)((char *)p+offsetof(struct sockaddr, sa_family)) :-)
-
The right solution is treating "struct sockaddr *" as an abstract/opaque type and using it just with getaddrinfo/getnameinfo/bind/connect.
-
That's not an option if you need to e.g. extract a port number from a sockaddr or manipulate/compare an address sadly.
-
Sure it is. getnameinfo (use NI_NUMERICHOST|NI_NUMERICSERV) converts them to string forms you can manipulate and match.
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.