common: fix Software::getPurpose error path
'version' cannot be dereferenced if it is not initialized.
Change the API to return an std::optional and handle in the caller.
Change-Id: Iac7badf145dad64e21ede5e81f0b4b761ff6c89d
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/common/include/software.hpp b/common/include/software.hpp
index a8477bf..2993707 100644
--- a/common/include/software.hpp
+++ b/common/include/software.hpp
@@ -58,9 +58,6 @@
SoftwareVersion::VersionPurpose versionPurpose =
SoftwareVersion::VersionPurpose::Unknown);
- // Return the version purpose
- SoftwareVersion::VersionPurpose getPurpose();
-
// This should populate 'softwareAssociationDefinitions'
// @param isRunning if the software version is currently running
// on the device. Otherwise the software is assumed to be activating (not
@@ -88,6 +85,10 @@
static long int getRandomId();
protected:
+ // @returns the version purpose
+ // @returns std::nullopt in case the version has not been set
+ std::optional<SoftwareVersion::VersionPurpose> getPurpose();
+
// @returns a random software id (swid) for that device
static std::string getRandomSoftwareId(device::Device& parent);
@@ -118,6 +119,7 @@
sdbusplus::async::context& ctx;
friend update::SoftwareUpdate;
+ friend device::Device;
};
}; // namespace phosphor::software
diff --git a/common/src/device.cpp b/common/src/device.cpp
index bba2d2b..7e39e0a 100644
--- a/common/src/device.cpp
+++ b/common/src/device.cpp
@@ -165,7 +165,8 @@
softwarePending->setActivation(ActivationInterface::Activations::Ready);
softwarePending->setVersion(componentVersion,
- softwareCurrent->getPurpose());
+ softwareCurrent->getPurpose().value_or(
+ SoftwareVersion::VersionPurpose::Unknown));
std::string objPath = softwarePending->objectPath;
diff --git a/common/src/software.cpp b/common/src/software.cpp
index 819f7be..d20451d 100644
--- a/common/src/software.cpp
+++ b/common/src/software.cpp
@@ -128,8 +128,12 @@
version->purpose(versionPurpose);
}
-SoftwareVersion::VersionPurpose Software::getPurpose()
+std::optional<SoftwareVersion::VersionPurpose> Software::getPurpose()
{
+ if (!version)
+ {
+ return std::nullopt;
+ }
return version->purpose();
}