ethernet_interface: Empty address also deletes the gateway
Since the address is invalid anyway, this is a straightforward way to
clear the entry.
Change-Id: I7c130d0e3c3c0d182e8a7c54d53be44b4ffe47a7
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index c6cc1b9..e7a2c29 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -820,7 +820,13 @@
}
try
{
- gw = stdplus::toStr(stdplus::fromStr<Addr>(gw));
+ auto ip = stdplus::fromStr<Addr>(gw);
+ if (ip == Addr{})
+ {
+ gw.clear();
+ return;
+ }
+ gw = stdplus::toStr(ip);
}
catch (const std::exception& e)
{
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index c92b8eb..4156506 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -176,6 +176,8 @@
EXPECT_EQ(interface.defaultGateway(), gateway);
interface.defaultGateway("");
EXPECT_EQ(interface.defaultGateway(), "");
+ interface.defaultGateway("0.0.0.0");
+ EXPECT_EQ(interface.defaultGateway(), "");
}
TEST_F(TestEthernetInterface, addGateway6)
@@ -185,6 +187,8 @@
EXPECT_EQ(interface.defaultGateway6(), gateway6);
interface.defaultGateway6("");
EXPECT_EQ(interface.defaultGateway6(), "");
+ interface.defaultGateway6("::");
+ EXPECT_EQ(interface.defaultGateway6(), "");
}
TEST_F(TestEthernetInterface, DHCPEnabled)