Commit 7ccfefc7 authored by Pavlo Kushnir's avatar Pavlo Kushnir Committed by Tudor Bosman

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
parent c765dfdf
...@@ -330,13 +330,13 @@ bool operator==(const IPAddress& addr1, const IPAddress& addr2) { ...@@ -330,13 +330,13 @@ bool operator==(const IPAddress& addr1, const IPAddress& addr2) {
} }
} }
// addr1 is v4 mapped v6 address, addr2 is v4 // addr1 is v4 mapped v6 address, addr2 is v4
if (addr1.isIPv4Mapped()) { if (addr1.isIPv4Mapped() && addr2.isV4()) {
if (IPAddress::createIPv4(addr1) == addr2.asV4()) { if (IPAddress::createIPv4(addr1) == addr2.asV4()) {
return true; return true;
} }
} }
// addr2 is v4 mapped v6 address, addr1 is v4 // addr2 is v4 mapped v6 address, addr1 is v4
if (addr2.isIPv4Mapped()) { if (addr2.isIPv4Mapped() && addr1.isV4()) {
if (IPAddress::createIPv4(addr2) == addr1.asV4()) { if (IPAddress::createIPv4(addr2) == addr1.asV4()) {
return true; return true;
} }
......
...@@ -435,6 +435,26 @@ TEST_P(IPAddressMaskBoundaryTest, NonMaskedSubnet) { ...@@ -435,6 +435,26 @@ TEST_P(IPAddressMaskBoundaryTest, NonMaskedSubnet) {
EXPECT_EQ(param.inSubnet, ip.inSubnet(subnet, param.mask)); 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 subnet calcs with 6to4 addresses
TEST(IPAddress, InSubnetWith6to4) { TEST(IPAddress, InSubnetWith6to4) {
auto ip = IPAddress("2002:c000:022a::"); // 192.0.2.42 auto ip = IPAddress("2002:c000:022a::"); // 192.0.2.42
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment