Bug 1241901 part 1 - Remove nsAutoPtr uses in nsNotifyAddrListener on Linux. r=bagder draft
authorXidorn Quan <quanxunzhen@gmail.com>
Wed, 27 Jan 2016 14:14:53 +1100
changeset 326691 5e9225a687a08e4c54dddc89c4691c3d234da8ec
parent 326536 067c1792e47db88ce8fa0e2d6974694851980b68
child 326692 800b4bcba7ed1cd8737375afb3ca0e2ba2b6cc17
push id10164
push userxquan@mozilla.com
push dateThu, 28 Jan 2016 11:17:52 +0000
reviewersbagder
bugs1241901
milestone47.0a1
Bug 1241901 part 1 - Remove nsAutoPtr uses in nsNotifyAddrListener on Linux. r=bagder
netwerk/system/linux/nsNotifyAddrListener_Linux.cpp
--- a/netwerk/system/linux/nsNotifyAddrListener_Linux.cpp
+++ b/netwerk/system/linux/nsNotifyAddrListener_Linux.cpp
@@ -13,17 +13,16 @@
 #include <net/if.h>
 #endif
 
 #include "nsThreadUtils.h"
 #include "nsIObserverService.h"
 #include "nsServiceManagerUtils.h"
 #include "nsNotifyAddrListener_Linux.h"
 #include "nsString.h"
-#include "nsAutoPtr.h"
 #include "mozilla/Logging.h"
 
 #include "mozilla/Services.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/FileUtils.h"
 
 #ifdef MOZ_NUWA_PROCESS
 #include "ipc/Nuwa.h"
@@ -153,32 +152,33 @@ void nsNotifyAddrListener::OnNetlinkMess
     // The buffer size below, (4095) was chosen partly based on testing and
     // partly on existing sample source code using this size. It needs to be
     // large enough to hold the netlink messages from the kernel.
     char buffer[4095];
     struct rtattr *attr;
     int attr_len;
     const struct ifaddrmsg* newifam;
 
-    // inspired by check_pf.c.
-    nsAutoPtr<char> addr;
-    nsAutoPtr<char> localaddr;
 
     ssize_t rc = EINTR_RETRY(recv(aNetlinkSocket, buffer, sizeof(buffer), 0));
     if (rc < 0) {
         return;
     }
     size_t netlink_bytes = rc;
 
     nlh = reinterpret_cast<struct nlmsghdr *>(buffer);
 
     bool networkChange = false;
 
     for (; NLMSG_OK(nlh, netlink_bytes);
          nlh = NLMSG_NEXT(nlh, netlink_bytes)) {
+        char prefixaddr[INET6_ADDRSTRLEN];
+        char localaddr[INET6_ADDRSTRLEN];
+        char* addr = nullptr;
+        prefixaddr[0] = localaddr[0] = '\0';
 
         if (NLMSG_DONE == nlh->nlmsg_type) {
             break;
         }
 
         LOG(("nsNotifyAddrListener::OnNetlinkMessage: new/deleted address\n"));
         newifam = reinterpret_cast<struct ifaddrmsg*>(NLMSG_DATA(nlh));
 
@@ -189,44 +189,41 @@ void nsNotifyAddrListener::OnNetlinkMess
 
         attr = IFA_RTA (newifam);
         attr_len = IFA_PAYLOAD (nlh);
         for (;attr_len && RTA_OK (attr, attr_len);
              attr = RTA_NEXT (attr, attr_len)) {
             if (attr->rta_type == IFA_ADDRESS) {
                 if (newifam->ifa_family == AF_INET) {
                     struct in_addr* in = (struct in_addr*)RTA_DATA(attr);
-                    addr = new char[INET_ADDRSTRLEN];
-                    inet_ntop(AF_INET, in, addr.get(), INET_ADDRSTRLEN);
+                    inet_ntop(AF_INET, in, prefixaddr, INET_ADDRSTRLEN);
                 } else {
                     struct in6_addr* in = (struct in6_addr*)RTA_DATA(attr);
-                    addr = new char[INET6_ADDRSTRLEN];
-                    inet_ntop(AF_INET6, in, addr.get(), INET6_ADDRSTRLEN);
+                    inet_ntop(AF_INET6, in, prefixaddr, INET6_ADDRSTRLEN);
                 }
             } else if (attr->rta_type == IFA_LOCAL) {
                 if (newifam->ifa_family == AF_INET) {
                     struct in_addr* in = (struct in_addr*)RTA_DATA(attr);
-                    localaddr = new char[INET_ADDRSTRLEN];
-                    inet_ntop(AF_INET, in, localaddr.get(), INET_ADDRSTRLEN);
+                    inet_ntop(AF_INET, in, localaddr, INET_ADDRSTRLEN);
                 } else {
                     struct in6_addr* in = (struct in6_addr*)RTA_DATA(attr);
-                    localaddr = new char[INET6_ADDRSTRLEN];
-                    inet_ntop(AF_INET6, in, localaddr.get(), INET6_ADDRSTRLEN);
+                    inet_ntop(AF_INET6, in, localaddr, INET6_ADDRSTRLEN);
                 }
             }
         }
-        if (localaddr) {
+        if (localaddr[0]) {
             addr = localaddr;
-        }
-        if (!addr) {
+        } else if (prefixaddr[0]) {
+            addr = prefixaddr;
+        } else {
             continue;
         }
         if (nlh->nlmsg_type == RTM_NEWADDR) {
             LOG(("nsNotifyAddrListener::OnNetlinkMessage: a new address "
-                 "- %s.", addr.get()));
+                 "- %s.", addr));
             struct ifaddrmsg* ifam;
             nsCString addrStr;
             addrStr.Assign(addr);
             if (mAddressInfo.Get(addrStr, &ifam)) {
                 LOG(("nsNotifyAddrListener::OnNetlinkMessage: the address "
                      "already known."));
                 if (memcmp(ifam, newifam, sizeof(struct ifaddrmsg))) {
                     LOG(("nsNotifyAddrListener::OnNetlinkMessage: but "
@@ -237,26 +234,22 @@ void nsNotifyAddrListener::OnNetlinkMess
             } else {
                 networkChange = true;
                 ifam = (struct ifaddrmsg*)malloc(sizeof(struct ifaddrmsg));
                 memcpy(ifam, newifam, sizeof(struct ifaddrmsg));
                 mAddressInfo.Put(addrStr,ifam);
             }
         } else {
             LOG(("nsNotifyAddrListener::OnNetlinkMessage: an address "
-                 "has been deleted - %s.", addr.get()));
+                 "has been deleted - %s.", addr));
             networkChange = true;
             nsCString addrStr;
             addrStr.Assign(addr);
             mAddressInfo.Remove(addrStr);
         }
-
-        // clean it up.
-        localaddr = nullptr;
-        addr = nullptr;
     }
 
     if (networkChange && mAllowChangedEvent) {
         NetworkChanged();
     }
 
     if (networkChange) {
         checkLink();