From: Naizhi Li Date: Wed, 31 Aug 2016 05:56:57 +0000 (-0700) Subject: Fix folly::SocketAddress::reset X-Git-Tag: v2016.09.05.00~9 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=8c3d0bc981bc08e159f6599bfbac5e3daec03dba;p=folly.git Fix folly::SocketAddress::reset 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 --- diff --git a/folly/SocketAddress.cpp b/folly/SocketAddress.cpp index 034b1b6f..bdafdb02 100644 --- a/folly/SocketAddress.cpp +++ b/folly/SocketAddress.cpp @@ -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); diff --git a/folly/SocketAddress.h b/folly/SocketAddress.h index 788490d0..e4edfbb6 100644 --- a/folly/SocketAddress.h +++ b/folly/SocketAddress.h @@ -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. diff --git a/folly/test/SocketAddressTest.cpp b/folly/test/SocketAddressTest.cpp index 15a23cb0..c04d9bfc 100644 --- a/folly/test/SocketAddressTest.cpp +++ b/folly/test/SocketAddressTest.cpp @@ -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()); +}