From 7ccfefc766cc09064823ae19e886e17188d44a46 Mon Sep 17 00:00:00 2001 From: Pavlo Kushnir Date: Sun, 6 Jul 2014 21:03:36 -0700 Subject: [PATCH] Fix IPAddress operator== 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 | 4 ++-- folly/test/IPAddressTest.cpp | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/folly/IPAddress.cpp b/folly/IPAddress.cpp index 01642104..b49b0dc8 100644 --- a/folly/IPAddress.cpp +++ b/folly/IPAddress.cpp @@ -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; } diff --git a/folly/test/IPAddressTest.cpp b/folly/test/IPAddressTest.cpp index 039999e6..d72a2b90 100644 --- a/folly/test/IPAddressTest.cpp +++ b/folly/test/IPAddressTest.cpp @@ -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 -- 2.34.1