pldmd: Account for pldm_pdr_init() returning NULL
Historically, libpldm's pldm_pdr_init() has internall used assert() to
enforce that the returned value is not NULL. Efforts are underway to
remove this reliance on assert() in libpldm to ensure consumers aren't
ungracefully terminated.
This change chooses to return an appropriate error to the
caller of pldmd's internal APIs where possible. However, in a number of
circumstances there was either not a sensible value that could be
returned, or it was not possible to return a value without further
refactoring. In these circumstances we choose to throw
`std::runtime_error`, which will at least do what it can to unwind.
While it may feel like it falls short of proper error handling, the
reality is the calling code is already structured such that it pretends
allocation failures never occur. The switch to throwing a runtime error
makes a strict improvement over the immediate termination implemented by
assert().
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Ibb6bc3abf62e32f05ac079453853873298db6c4f
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 2d32468..ad8948c 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -739,6 +739,11 @@
std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
pldm_pdr_init(), pldm_pdr_destroy);
+ if (!stateSensorPdrRepo)
+ {
+ error("Failed to instantiate state sensor PDR repository");
+ return false;
+ }
Repo stateSensorPDRs(stateSensorPdrRepo.get());
getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
if (stateSensorPDRs.empty())
@@ -803,6 +808,11 @@
std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
pldm_pdr_init(), pldm_pdr_destroy);
+ if (!stateEffecterPdrRepo)
+ {
+ error("Failed to instantiate state effecter PDR repository");
+ return false;
+ }
Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
getRepoByType(handler.getRepo(), stateEffecterPDRs,
PLDM_STATE_EFFECTER_PDR);