Fix potential map::at error
There are some potential errors, when using the map::at method,
the process may hang due to out of bounds. so the exception should
be caught.
Also, use the map::contains method of C++20 instead of map::find.
Tested: built pldm successfully and worked.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Ia5d7dea906e5c598072c08b27387195ef1201839
diff --git a/host-bmc/dbus_to_event_handler.cpp b/host-bmc/dbus_to_event_handler.cpp
index 7b2d207..ac8778f 100644
--- a/host-bmc/dbus_to_event_handler.cpp
+++ b/host-bmc/dbus_to_event_handler.cpp
@@ -80,6 +80,12 @@
{
// Encode PLDM platform event msg to indicate a state sensor change.
// DSP0248_1.2.0 Table 19
+ if (!dbusMaps.contains(sensorId))
+ {
+ std::cerr << "Invalid sensor ID : " << sensorId << std::endl;
+ return;
+ }
+
size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
@@ -151,7 +157,11 @@
{
pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
SensorId sensorId = LE16TOH(pdr->sensor_id);
- sensorHandlers.at(pdrType)(sensorId, dbusMaps);
+ if (sensorHandlers.contains(pdrType))
+ {
+ sensorHandlers.at(pdrType)(sensorId, dbusMaps);
+ }
+
pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
}
}