network_manager: Cleanup VLAN creation
This adds proper error handling to the path so that we don't segfault.
Change-Id: I65f39844bd36d9f5f97df33a64f5d07e767dd48c
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index 86d892f..b3b28b4 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -12,10 +12,9 @@
#include <net/if.h>
#include <algorithm>
-#include <bitset>
+#include <charconv>
#include <filesystem>
#include <fstream>
-#include <map>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/log.hpp>
#include <string>
@@ -121,11 +120,26 @@
if (index != std::string::npos)
{
// it is vlan interface
- auto interfaceName = interface.substr(0, index);
- auto vlanid = interface.substr(index + 1);
- uint32_t vlanInt = std::stoul(vlanid);
-
- interfaces[interfaceName]->loadVLAN(vlanInt);
+ auto sv = std::string_view(interface);
+ auto interfaceName = sv.substr(0, index);
+ auto vlanStr = sv.substr(index + 1);
+ uint16_t vlanId;
+ auto res = std::from_chars(vlanStr.begin(), vlanStr.end(), vlanId);
+ if (res.ec != std::errc() || res.ptr != vlanStr.end())
+ {
+ auto msg = fmt::format("Invalid VLAN: {}", vlanStr);
+ log<level::ERR>(msg.c_str());
+ continue;
+ }
+ auto it = interfaces.find(std::string(interfaceName));
+ if (it == interfaces.end())
+ {
+ auto msg = fmt::format("Missing interface({}) for VLAN({}): {}",
+ interfaceName, vlanId, interface);
+ log<level::ERR>(msg.c_str());
+ continue;
+ }
+ it->second->loadVLAN(vlanId);
continue;
}
// normal ethernet interface
@@ -165,7 +179,7 @@
bus, objPath.string(), *this);
}
-ObjectPath Manager::vlan(IntfName interfaceName, uint32_t id)
+ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
{
if (!hasInterface(interfaceName))
{