Fix core dump when invalid MAC is configured
When configuring an invalid MAC, a runtime error is thrown
with the existing implementation, which causes a core dump
and phosphor-networkd does not respond when further requests
are made.
This commit avoids the core dump that's caused.
Signed-off-by: Asmitha Karunanithi <asmitk01@in.ibm.com>
Change-Id: I5cb5a25e91a685e1bb2f4bb80d3d8f8c483f56a1
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index 0c83fba..522be57 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -998,7 +998,18 @@
std::string EthernetInterface::mACAddress(std::string value)
{
- ether_addr newMAC = mac_address::fromString(value);
+ ether_addr newMAC;
+ try
+ {
+ newMAC = mac_address::fromString(value);
+ }
+ catch (std::invalid_argument&)
+ {
+ log<level::ERR>("MACAddress is not valid.",
+ entry("MAC=%s", value.c_str()));
+ elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
+ Argument::ARGUMENT_VALUE(value.c_str()));
+ }
if (!mac_address::isUnicast(newMAC))
{
log<level::ERR>("MACAddress is not valid.",
diff --git a/test/test_util.cpp b/test/test_util.cpp
index a6605dd..e3a1fba 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -219,9 +219,9 @@
TEST(MacFromString, Bad)
{
- EXPECT_THROW(fromString("0x:00:00:00:00:00"), std::runtime_error);
- EXPECT_THROW(fromString("00:00:00:00:00"), std::runtime_error);
- EXPECT_THROW(fromString(""), std::runtime_error);
+ EXPECT_THROW(fromString("0x:00:00:00:00:00"), std::invalid_argument);
+ EXPECT_THROW(fromString("00:00:00:00:00"), std::invalid_argument);
+ EXPECT_THROW(fromString(""), std::invalid_argument);
}
TEST(MacFromString, Valid)
diff --git a/util.cpp b/util.cpp
index cb9e720..0c5dbff 100644
--- a/util.cpp
+++ b/util.cpp
@@ -631,7 +631,7 @@
struct ether_addr* mac = ether_aton(str);
if (mac == nullptr)
{
- throw std::runtime_error("Invalid mac address string");
+ throw std::invalid_argument("Invalid MAC Address");
}
return *mac;
}