NVMeBasicContext: Reject bad I2C bus IDs
We can't possibly query such sensors, so ensure they can't exist.
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I899979db57a84272be9b537d87e54756296695cf
diff --git a/src/NVMeSensorMain.cpp b/src/NVMeSensorMain.cpp
index f15f867..7e32815 100644
--- a/src/NVMeSensorMain.cpp
+++ b/src/NVMeSensorMain.cpp
@@ -152,13 +152,27 @@
<< "\n";
}
- std::shared_ptr<NVMeSensor> sensorPtr =
- std::make_shared<NVMeSensor>(
- objectServer, io, dbusConnection, *sensorName,
- std::move(sensorThresholds), interfacePath, *busNumber);
+ try
+ {
+ // May throw for an invalid rootBus
+ std::shared_ptr<NVMeContext> context =
+ provideRootBusContext(io, nvmeDeviceMap, *rootBus);
- provideRootBusContext(io, nvmeDeviceMap, *rootBus)
- ->addSensor(sensorPtr);
+ // Construct the sensor after grabbing the context so we don't
+ // glitch D-Bus May throw for an invalid busNumber
+ std::shared_ptr<NVMeSensor> sensorPtr =
+ std::make_shared<NVMeSensor>(
+ objectServer, io, dbusConnection, *sensorName,
+ std::move(sensorThresholds), interfacePath, *busNumber);
+
+ context->addSensor(sensorPtr);
+ }
+ catch (const std::invalid_argument& ex)
+ {
+ std::cerr << "Failed to add sensor for "
+ << std::string(interfacePath) << ": " << ex.what()
+ << "\n";
+ }
}
}
for (const auto& [_, context] : nvmeDeviceMap)