]> granicus.if.org Git - php/commitdiff
Handle misaligned ifreq on macos
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 20 Jun 2019 14:13:16 +0000 (16:13 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 20 Jun 2019 15:24:16 +0000 (17:24 +0200)
ext/sockets/multicast.c

index e874803656439afaf35979f031388d68753dc2fe..481bf94f8ca4d30f7a8aaeb28d0a0e1703db91af 100644 (file)
@@ -788,28 +788,27 @@ int php_add4_to_if_index(struct in_addr *addr, php_socket *php_sock, unsigned *i
        for (p = if_conf.ifc_buf;
                 p < if_conf.ifc_buf + if_conf.ifc_len;
                 p += entry_len) {
-               struct ifreq *cur_req;
-
-               /* let's hope the pointer is aligned */
-               cur_req = (struct ifreq*) p;
+               /* p may be misaligned on macos. */
+               struct ifreq cur_req;
+               memcpy(&cur_req, p, sizeof(struct ifreq));
 
 #ifdef HAVE_SOCKADDR_SA_LEN
-               entry_len = cur_req->ifr_addr.sa_len + sizeof(cur_req->ifr_name);
+               entry_len = cur_req.ifr_addr.sa_len + sizeof(cur_req.ifr_name);
 #else
                /* if there's no sa_len, assume the ifr_addr field is a sockaddr */
-               entry_len = sizeof(struct sockaddr) + sizeof(cur_req->ifr_name);
+               entry_len = sizeof(struct sockaddr) + sizeof(cur_req.ifr_name);
 #endif
-               entry_len = MAX(entry_len, sizeof(*cur_req));
+               entry_len = MAX(entry_len, sizeof(cur_req));
 
-               if ((((struct sockaddr*)&cur_req->ifr_addr)->sa_family == AF_INET) &&
-                               (((struct sockaddr_in*)&cur_req->ifr_addr)->sin_addr.s_addr ==
+               if ((((struct sockaddr*)&cur_req.ifr_addr)->sa_family == AF_INET) &&
+                               (((struct sockaddr_in*)&cur_req.ifr_addr)->sin_addr.s_addr ==
                                        addr->s_addr)) {
 #if defined(SIOCGIFINDEX)
-                       if (ioctl(php_sock->bsd_socket, SIOCGIFINDEX, (char*)cur_req)
+                       if (ioctl(php_sock->bsd_socket, SIOCGIFINDEX, (char*)&cur_req)
                                        == -1) {
 #elif defined(HAVE_IF_NAMETOINDEX)
                        unsigned index_tmp;
-                       if ((index_tmp = if_nametoindex(cur_req->ifr_name)) == 0) {
+                       if ((index_tmp = if_nametoindex(cur_req.ifr_name)) == 0) {
 #else
 #error Neither SIOCGIFINDEX nor if_nametoindex are available
 #endif
@@ -819,7 +818,7 @@ int php_add4_to_if_index(struct in_addr *addr, php_socket *php_sock, unsigned *i
                                goto err;
                        } else {
 #if defined(SIOCGIFINDEX)
-                               *if_index = cur_req->ifr_ifindex;
+                               *if_index = cur_req.ifr_ifindex;
 #else
                                *if_index = index_tmp;
 #endif