bionic: Fix aliasing violations
This makes bionic compile with compilers enforcing
strict adherence to aliasing rules (e.g. gcc 4.6+
with -Wstrict-aliasing=2 -Werror=strict-aliasing).
Signed-off-by: Bernhard Rosenkraenzer <Bernhard.Rosenkranzer@linaro.org>
Conflicts:
libc/netbsd/net/getaddrinfo.c
libc/netbsd/net/getnameinfo.c
Change-Id: I4a6fd36a66b23d963ce137916caece4a424e78ab
Conflicts:
libc/netbsd/resolv/res_send.c
diff --git a/libc/bionic/md5.c b/libc/bionic/md5.c
index ba4aaed..02785bd 100644
--- a/libc/bionic/md5.c
+++ b/libc/bionic/md5.c
@@ -231,7 +231,7 @@
}
calc(m, current);
#else
- calc(m, (u_int32_t*)m->save);
+ calc(m, m->save32);
#endif
offset = 0;
}
diff --git a/libc/bionic/md5.h b/libc/bionic/md5.h
index a381994..3b0a251 100644
--- a/libc/bionic/md5.h
+++ b/libc/bionic/md5.h
@@ -40,7 +40,10 @@
struct md5 {
unsigned int sz[2];
u_int32_t counter[4];
- unsigned char save[64];
+ union {
+ unsigned char save[64];
+ u_int32_t save32[16];
+ };
};
typedef struct md5 MD5_CTX;
diff --git a/libc/bionic/system_properties.c b/libc/bionic/system_properties.c
index 4c2e5a2..2a8b082 100644
--- a/libc/bionic/system_properties.c
+++ b/libc/bionic/system_properties.c
@@ -480,7 +480,10 @@
static int send_prop_msg(prop_msg *msg)
{
struct pollfd pollfds[1];
- struct sockaddr_un addr;
+ union {
+ struct sockaddr_un addr;
+ struct sockaddr addr_g;
+ } addr;
socklen_t alen;
size_t namelen;
int s;
@@ -494,11 +497,11 @@
memset(&addr, 0, sizeof(addr));
namelen = strlen(property_service_socket);
- strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
- addr.sun_family = AF_LOCAL;
+ strlcpy(addr.addr.sun_path, property_service_socket, sizeof addr.addr.sun_path);
+ addr.addr.sun_family = AF_LOCAL;
alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
- if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
+ if(TEMP_FAILURE_RETRY(connect(s, &addr.addr_g, alen) < 0)) {
close(s);
return result;
}
diff --git a/libc/include/netinet/in6.h b/libc/include/netinet/in6.h
index 7f3286a..ba24b6c 100644
--- a/libc/include/netinet/in6.h
+++ b/libc/include/netinet/in6.h
@@ -31,28 +31,28 @@
#include <linux/in6.h>
#define IN6_IS_ADDR_UNSPECIFIED(a) \
- ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[12]) == 0))
+ (((a)->s6_addr32[0] == 0) && \
+ ((a)->s6_addr32[1] == 0) && \
+ ((a)->s6_addr32[2] == 0) && \
+ ((a)->s6_addr32[3] == 0))
#define IN6_IS_ADDR_LOOPBACK(a) \
- ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[12]) == ntohl(1)))
+ (((a)->s6_addr32[0] == 0) && \
+ ((a)->s6_addr32[1] == 0) && \
+ ((a)->s6_addr32[2] == 0) && \
+ ((a)->s6_addr32[3] == ntohl(1)))
#define IN6_IS_ADDR_V4COMPAT(a) \
- ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[12]) != 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[12]) != ntohl(1)))
+ (((a)->s6_addr32[0] == 0) && \
+ ((a)->s6_addr32[1] == 0) && \
+ ((a)->s6_addr32[2] == 0) && \
+ ((a)->s6_addr32[3] != 0) && \
+ ((a)->s6_addr32[3] != ntohl(1)))
#define IN6_IS_ADDR_V4MAPPED(a) \
- ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \
- (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff)))
+ (((a)->s6_addr32[0] == 0) && \
+ ((a)->s6_addr32[1] == 0) && \
+ ((a)->s6_addr32[2] == ntohl(0x0000ffff)))
#define IN6_IS_ADDR_LINKLOCAL(a) \
(((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0x80))
@@ -65,7 +65,7 @@
(((a)->s6_addr[0] & 0xfe) == 0xfc)
#define IN6_IS_ADDR_MULTICAST(a) \
- (((__const uint8_t *) (a))[0] == 0xff)
+ ((a)->s6_addr[0] == 0xff)
#define IPV6_ADDR_SCOPE_NODELOCAL 0x01
diff --git a/libc/netbsd/gethnamaddr.c b/libc/netbsd/gethnamaddr.c
index 5b2f987..d7f9a0c 100644
--- a/libc/netbsd/gethnamaddr.c
+++ b/libc/netbsd/gethnamaddr.c
@@ -825,14 +825,14 @@
assert(addr != NULL);
if (af == AF_INET6 && len == IN6ADDRSZ &&
- (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) ||
- IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) {
+ (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) ||
+ IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) {
h_errno = HOST_NOT_FOUND;
return NULL;
}
if (af == AF_INET6 && len == IN6ADDRSZ &&
- (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) ||
- IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) {
+ (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) ||
+ IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) {
/* Unmap. */
addr += IN6ADDRSZ - INADDRSZ;
uaddr += IN6ADDRSZ - INADDRSZ;
diff --git a/libc/netbsd/net/getaddrinfo.c b/libc/netbsd/net/getaddrinfo.c
index 937c423..6895b3b 100644
--- a/libc/netbsd/net/getaddrinfo.c
+++ b/libc/netbsd/net/getaddrinfo.c
@@ -411,7 +411,11 @@
{
int sock;
const int one = 1;
- struct sockaddr_un proxy_addr;
+ union {
+ struct sockaddr_un un;
+ struct sockaddr generic;
+ } proxy_addr;
+ const char* cache_mode = getenv("ANDROID_DNS_MODE");
FILE* proxy = NULL;
int success = 0;
@@ -435,12 +439,12 @@
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
memset(&proxy_addr, 0, sizeof(proxy_addr));
- proxy_addr.sun_family = AF_UNIX;
- strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd",
- sizeof(proxy_addr.sun_path));
+ proxy_addr.un.sun_family = AF_UNIX;
+ strlcpy(proxy_addr.un.sun_path, "/dev/socket/dnsproxyd",
+ sizeof(proxy_addr.un.sun_path));
if (TEMP_FAILURE_RETRY(connect(sock,
- (const struct sockaddr*) &proxy_addr,
- sizeof(proxy_addr))) != 0) {
+ &proxy_addr.generic,
+ sizeof(proxy_addr.un))) != 0) {
close(sock);
return EAI_NODATA;
}
diff --git a/libc/netbsd/resolv/res_send.c b/libc/netbsd/resolv/res_send.c
index f65b015..9622952 100644
--- a/libc/netbsd/resolv/res_send.c
+++ b/libc/netbsd/resolv/res_send.c
@@ -417,7 +417,10 @@
*/
if (EXT(statp).nscount != 0) {
int needclose = 0;
- struct sockaddr_storage peer;
+ union {
+ struct sockaddr_storage storage;
+ struct sockaddr generic;
+ } peer;
socklen_t peerlen;
if (EXT(statp).nscount != statp->nscount)
@@ -433,13 +436,13 @@
if (EXT(statp).nssocks[ns] == -1)
continue;
- peerlen = sizeof(peer);
+ peerlen = sizeof(peer.storage);
if (getpeername(EXT(statp).nssocks[ns],
- (struct sockaddr *)(void *)&peer, &peerlen) < 0) {
+ &peer.generic, &peerlen) < 0) {
needclose++;
break;
}
- if (!sock_eq((struct sockaddr *)(void *)&peer,
+ if (!sock_eq(&peer.generic,
get_nsaddr(statp, (size_t)ns))) {
needclose++;
break;
@@ -760,13 +763,16 @@
/* Are we still talking to whom we want to talk to? */
if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
- struct sockaddr_storage peer;
- socklen_t size = sizeof peer;
+ union {
+ struct sockaddr_storage storage;
+ struct sockaddr generic;
+ } peer;
+ socklen_t size = sizeof peer.storage;
int old_mark;
int mark_size = sizeof(old_mark);
if (getpeername(statp->_vcsock,
- (struct sockaddr *)(void *)&peer, &size) < 0 ||
- !sock_eq((struct sockaddr *)(void *)&peer, nsap) ||
+ &peer.generic, &size) < 0 ||
+ !sock_eq(&peer.generic, nsap)) {
getsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &old_mark, &mark_size) < 0 ||
old_mark != statp->_mark) {
res_nclose(statp);
@@ -1054,7 +1060,10 @@
int nsaplen;
struct timespec now, timeout, finish;
fd_set dsmask;
- struct sockaddr_storage from;
+ union {
+ struct sockaddr_storage storage;
+ struct sockaddr generic;
+ } from;
socklen_t fromlen;
int resplen, seconds, n, s;
@@ -1155,9 +1164,9 @@
return (0);
}
errno = 0;
- fromlen = sizeof(from);
+ fromlen = sizeof(from.storage);
resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
- (struct sockaddr *)(void *)&from, &fromlen);
+ &from.generic, &fromlen);
if (resplen <= 0) {
Perror(statp, stderr, "recvfrom", errno);
res_nclose(statp);
@@ -1191,7 +1200,7 @@
goto retry;
}
if (!(statp->options & RES_INSECURE1) &&
- !res_ourserver_p(statp, (struct sockaddr *)(void *)&from)) {
+ !res_ourserver_p(statp, &from.generic)) {
/*
* response from wrong server? ignore it.
* XXX - potential security hazard could