Fix IPAddress operator==
authorPavlo Kushnir <pavlo@fb.com>
Mon, 7 Jul 2014 04:03:36 +0000 (21:03 -0700)
committerTudor Bosman <tudorb@fb.com>
Tue, 15 Jul 2014 00:42:03 +0000 (17:42 -0700)
Summary: IPAddress operator== throws an exception when comparing ip v6 mapped address with unitialized (default constructed) address. With this diff it returns false.

Test Plan: folly unit tests

Reviewed By: bmatheny@fb.com

FB internal diff: D1421280

folly/IPAddress.cpp
folly/test/IPAddressTest.cpp

index 016421043b338cda7de166263cbd7b761e098b1b..b49b0dc86f370eb08c0845d3366b602202181b10 100644 (file)
@@ -330,13 +330,13 @@ bool operator==(const IPAddress& addr1, const IPAddress& addr2) {
     }
   }
   // addr1 is v4 mapped v6 address, addr2 is v4
-  if (addr1.isIPv4Mapped()) {
+  if (addr1.isIPv4Mapped() && addr2.isV4()) {
     if (IPAddress::createIPv4(addr1) == addr2.asV4()) {
       return true;
     }
   }
   // addr2 is v4 mapped v6 address, addr1 is v4
-  if (addr2.isIPv4Mapped()) {
+  if (addr2.isIPv4Mapped() && addr1.isV4()) {
     if (IPAddress::createIPv4(addr2) == addr1.asV4()) {
       return true;
     }
index 039999e6fcc65a803ad388637eb06d00ab1edb44..d72a2b90e020ca3f117b3ac4ce0bb32e594c9f9b 100644 (file)
@@ -435,6 +435,26 @@ TEST_P(IPAddressMaskBoundaryTest, NonMaskedSubnet) {
   EXPECT_EQ(param.inSubnet, ip.inSubnet(subnet, param.mask));
 }
 
+TEST(IPAddress, UnitializedEqual) {
+  IPAddress addrEmpty;
+  IPAddress ip4("127.0.0.1");
+  EXPECT_FALSE(addrEmpty == ip4);
+  EXPECT_FALSE(ip4 == addrEmpty);
+  IPAddress ip6("::1");
+  EXPECT_FALSE(addrEmpty == ip6);
+  EXPECT_FALSE(ip6 == addrEmpty);
+  IPAddress ip6Map("::ffff:192.0.2.129");
+  EXPECT_FALSE(addrEmpty == ip6Map);
+  EXPECT_FALSE(ip6Map == addrEmpty);
+  IPAddress ip4Zero("0.0.0.0");
+  EXPECT_FALSE(addrEmpty == ip4Zero);
+  EXPECT_FALSE(ip4Zero == addrEmpty);
+  IPAddress ip6Zero("::");
+  EXPECT_FALSE(addrEmpty == ip6Zero);
+  EXPECT_FALSE(ip6Zero == addrEmpty);
+  EXPECT_EQ(addrEmpty, addrEmpty);
+}
+
 // Test subnet calcs with 6to4 addresses
 TEST(IPAddress, InSubnetWith6to4) {
   auto ip = IPAddress("2002:c000:022a::"); // 192.0.2.42