Remove nlohmann::json::items()
nlohmann::json::items() throws an exception if the object in question is
not a json object. This has the potential to cause problems, and isn't
in line with the standard that we code against.
Replace all uses of items with iterating the nlohmann::json::object_t.
This adds a new error check for pulling the object_t out of the
nlohmann::json object before each iteration, to ensure that we're
handling errors.
Tested: Redfish service validator passes.
Change-Id: I2934c9450ec296c76544c2a7c5855c9b519eae7f
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/include/dbus_monitor.hpp b/include/dbus_monitor.hpp
index db75fce..6ec43d0 100644
--- a/include/dbus_monitor.hpp
+++ b/include/dbus_monitor.hpp
@@ -80,20 +80,32 @@
BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
return 0;
}
-
- if (!data.is_array())
+ nlohmann::json::array_t* arr = data.get_ptr<nlohmann::json::array_t*>();
+ if (arr == nullptr)
+ {
+ BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
+ return 0;
+ }
+ if (arr->size() < 2)
{
BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
return 0;
}
- // data is type oa{sa{sv}} which is an array[2] of string, object
- for (const auto& entry : data[1].items())
+ nlohmann::json::object_t* obj =
+ (*arr)[1].get_ptr<nlohmann::json::object_t*>();
+ if (obj == nullptr)
{
- auto it = thisSession->second.interfaces.find(entry.key());
+ BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
+ return 0;
+ }
+ // data is type oa{sa{sv}} which is an array[2] of string, object
+ for (const auto& entry : *obj)
+ {
+ auto it = thisSession->second.interfaces.find(entry.first);
if (it != thisSession->second.interfaces.end())
{
- json["interfaces"][entry.key()] = entry.value();
+ json["interfaces"][entry.first] = entry.second;
}
}
}