# HG changeset patch # User Maxim Dounin # Date 1348513608 0 # Node ID 880dedfa4008494d4642dffcf42c08976f7d7b40 # Parent 43b7b9572fe14b66327818b616e427234ed6ef17 Merge of r4829: fixed strict aliasing with ipv6 (ticket #201). Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses. diff --git a/src/http/modules/ngx_http_geo_module.c b/src/http/modules/ngx_http_geo_module.c --- a/src/http/modules/ngx_http_geo_module.c +++ b/src/http/modules/ngx_http_geo_module.c @@ -233,12 +233,21 @@ ngx_http_geo_addr(ngx_http_request_t *r, #if (NGX_HAVE_INET6) if (addr.sockaddr->sa_family == AF_INET6) { + u_char *p; + in_addr_t inaddr; struct in6_addr *inaddr6; inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr; if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { - return ntohl(*(in_addr_t *) &inaddr6->s6_addr[12]); + p = inaddr6->s6_addr; + + inaddr = p[12] << 24; + inaddr += p[13] << 16; + inaddr += p[14] << 8; + inaddr += p[15]; + + return inaddr; } } diff --git a/src/http/modules/ngx_http_geoip_module.c b/src/http/modules/ngx_http_geoip_module.c --- a/src/http/modules/ngx_http_geoip_module.c +++ b/src/http/modules/ngx_http_geoip_module.c @@ -226,12 +226,21 @@ ngx_http_geoip_addr(ngx_http_request_t * #if (NGX_HAVE_INET6) if (addr.sockaddr->sa_family == AF_INET6) { + u_char *p; + in_addr_t inaddr; struct in6_addr *inaddr6; inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr; if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { - return ntohl(*(in_addr_t *) &inaddr6->s6_addr[12]); + p = inaddr6->s6_addr; + + inaddr = p[12] << 24; + inaddr += p[13] << 16; + inaddr += p[14] << 8; + inaddr += p[15]; + + return inaddr; } } diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -2733,7 +2733,15 @@ ngx_http_get_forwarded_addr(ngx_http_req if (IN6_IS_ADDR_V4MAPPED(inaddr6)) { family = AF_INET; - inaddr = *(in_addr_t *) &inaddr6->s6_addr[12]; + + p = inaddr6->s6_addr; + + inaddr = p[12] << 24; + inaddr += p[13] << 16; + inaddr += p[14] << 8; + inaddr += p[15]; + + inaddr = htonl(inaddr); } } #endif