Fix folly::SocketAddress::reset
authorNaizhi Li <naizhi@fb.com>
Wed, 31 Aug 2016 05:56:57 +0000 (22:56 -0700)
committerFacebook Github Bot 1 <facebook-github-bot-1-bot@fb.com>
Wed, 31 Aug 2016 06:08:30 +0000 (23:08 -0700)
Summary:
Currently reset does not actually reset it. This change
make sure it does the job.

Reviewed By: yfeldblum

Differential Revision: D3788869

fbshipit-source-id: 771a641e31bb3cf307e5bebb979b81a3d3feea55

folly/SocketAddress.cpp
folly/SocketAddress.h
folly/test/SocketAddressTest.cpp

index 034b1b6fe070559ab15cdad9af8e4e0761769a69..bdafdb02bceaff1615dea9d78ec7bf376a23cf85 100644 (file)
@@ -325,7 +325,10 @@ void SocketAddress::setFromSockaddr(const struct sockaddr_un* address,
       "with length too long for a sockaddr_un");
   }
 
-  prepFamilyChange(AF_UNIX);
+  if (!external_) {
+    storage_.un.init();
+  }
+  external_ = true;
   memcpy(storage_.un.addr, address, addrlen);
   updateUnixAddressLength(addrlen);
 
index 788490d0e05b34694205584aacc6b0d0e2a074c5..e4edfbb68d6826712d4b2ac3c0ab800ac318ee42 100644 (file)
@@ -151,7 +151,11 @@ class SocketAddress {
   bool isLoopbackAddress() const;
 
   void reset() {
-    prepFamilyChange(AF_UNSPEC);
+    if (external_) {
+      storage_.un.free();
+    }
+    storage_.addr = folly::IPAddress();
+    external_ = false;
   }
 
   /**
@@ -579,21 +583,6 @@ class SocketAddress {
 
   void updateUnixAddressLength(socklen_t addrlen);
 
-  void prepFamilyChange(sa_family_t newFamily) {
-    if (newFamily != AF_UNIX) {
-      if (external_) {
-        storage_.un.free();
-        storage_.addr = folly::IPAddress();
-      }
-      external_ = false;
-    } else {
-      if (!external_) {
-        storage_.un.init();
-      }
-      external_ = true;
-    }
-  }
-
   /*
    * storage_ contains room for a full IPv4 or IPv6 address, so they can be
    * stored inline without a separate allocation on the heap.
index 15a23cb02180547c2963e8822835c7bc28fc4316..c04d9bfcc5ae8b0cdc088b2fcdf386a14f24dfcc 100644 (file)
@@ -897,3 +897,18 @@ TEST(SocketAddress, ResetUnixAddress) {
   addy.reset();
   EXPECT_EQ(addy.getFamily(), AF_UNSPEC);
 }
+
+TEST(SocketAddress, ResetIPAddress) {
+  SocketAddress addr;
+  addr.setFromIpPort("127.0.0.1", 80);
+  addr.reset();
+  EXPECT_EQ(addr.getFamily(), AF_UNSPEC);
+  EXPECT_FALSE(addr.isInitialized());
+  EXPECT_TRUE(addr.empty());
+
+  addr.setFromIpPort("2620:0:1cfe:face:b00c::3:65535");
+  addr.reset();
+  EXPECT_EQ(addr.getFamily(), AF_UNSPEC);
+  EXPECT_FALSE(addr.isInitialized());
+  EXPECT_TRUE(addr.empty());
+}