summaryrefslogtreecommitdiff
path: root/redfish-core/lib/virtual_media.hpp
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2023-06-30 23:21:32 +0300
committerEd Tanous <ed@tanous.net>2023-08-07 20:51:50 +0300
commite01d0c36af115ed46d54b5dbbacfe3ad92226bd3 (patch)
tree3355d0c47cc9af8068a0a3f7329a8109573f9eb1 /redfish-core/lib/virtual_media.hpp
parent1ccf70f116b28a0f78404b914a789ce05f411ddf (diff)
downloadbmcweb-e01d0c36af115ed46d54b5dbbacfe3ad92226bd3.tar.xz
Fix bugprone-unchecked-optional-access findings
Clang-tidy has the aforementioned check, which shows a few places in the core where we ignored the required optional checks. Fix all uses. Note, we cannot enable the check that this time because of some weird code in health.hpp that crashes tidy[1]. That will need to be a future improvement. There are tests that call something like ASSERT(optional) EXPECT(optional->foo()) While this isn't an actual violation, clang-tidy doesn't seem to be smart enough to deal with it, so add some explicit checks. [1] https://github.com/llvm/llvm-project/issues/55530 Tested: Redfish service validator passes. Change-Id: Ied579cd0b957efc81aff5d5d1091a740a7a2d7e3 Signed-off-by: Ed Tanous <edtanous@google.com>
Diffstat (limited to 'redfish-core/lib/virtual_media.hpp')
-rw-r--r--redfish-core/lib/virtual_media.hpp35
1 files changed, 20 insertions, 15 deletions
diff --git a/redfish-core/lib/virtual_media.hpp b/redfish-core/lib/virtual_media.hpp
index e425cc188d..9ef45e719a 100644
--- a/redfish-core/lib/virtual_media.hpp
+++ b/redfish-core/lib/virtual_media.hpp
@@ -391,7 +391,7 @@ inline std::optional<TransferProtocol>
inline std::optional<TransferProtocol> getTransferProtocolFromParam(
const std::optional<std::string>& transferProtocolType)
{
- if (transferProtocolType == std::nullopt)
+ if (!transferProtocolType)
{
return {};
}
@@ -670,7 +670,7 @@ inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
}
// optional param inserted must be true
- if ((actionParams.inserted != std::nullopt) && !*actionParams.inserted)
+ if (actionParams.inserted && !*actionParams.inserted)
{
BMCWEB_LOG_ERROR(
"Request action optional parameter Inserted must be true.");
@@ -682,7 +682,7 @@ inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
}
// optional param transferMethod must be stream
- if ((actionParams.transferMethod != std::nullopt) &&
+ if (actionParams.transferMethod &&
(*actionParams.transferMethod != "Stream"))
{
BMCWEB_LOG_ERROR("Request action optional parameter "
@@ -708,7 +708,8 @@ inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
getTransferProtocolFromParam(actionParams.transferProtocolType);
// ImageUrl does not contain valid protocol type
- if (*uriTransferProtocolType == TransferProtocol::invalid)
+ if (uriTransferProtocolType &&
+ *uriTransferProtocolType == TransferProtocol::invalid)
{
BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
"contain specified protocol type from list: "
@@ -720,21 +721,21 @@ inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
}
// transferProtocolType should contain value from list
- if (*paramTransferProtocolType == TransferProtocol::invalid)
+ if (paramTransferProtocolType &&
+ *paramTransferProtocolType == TransferProtocol::invalid)
{
BMCWEB_LOG_ERROR("Request action parameter TransferProtocolType "
"must be provided with value from list: "
"(CIFS, HTTPS).");
- messages::propertyValueNotInList(asyncResp->res,
- *actionParams.transferProtocolType,
- "TransferProtocolType");
+ messages::propertyValueNotInList(
+ asyncResp->res, actionParams.transferProtocolType.value_or(""),
+ "TransferProtocolType");
return;
}
// valid transfer protocol not provided either with URI nor param
- if ((uriTransferProtocolType == std::nullopt) &&
- (paramTransferProtocolType == std::nullopt))
+ if (!uriTransferProtocolType && !paramTransferProtocolType)
{
BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
"contain specified protocol type or param "
@@ -746,8 +747,7 @@ inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
}
// valid transfer protocol provided both with URI and param
- if ((paramTransferProtocolType != std::nullopt) &&
- (uriTransferProtocolType != std::nullopt))
+ if (paramTransferProtocolType && uriTransferProtocolType)
{
// check if protocol is the same for URI and param
if (*paramTransferProtocolType != *uriTransferProtocolType)
@@ -758,15 +758,20 @@ inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
"provided with param imageUrl.");
messages::actionParameterValueTypeError(
- asyncResp->res, *actionParams.transferProtocolType,
+ asyncResp->res, actionParams.transferProtocolType.value_or(""),
"TransferProtocolType", "InsertMedia");
return;
}
}
+ if (!paramTransferProtocolType)
+ {
+ messages::internalError(asyncResp->res);
+ return;
+ }
// validation passed, add protocol to URI if needed
- if (uriTransferProtocolType == std::nullopt)
+ if (!uriTransferProtocolType)
{
actionParams.imageUrl = getUriWithTransferProtocol(
*actionParams.imageUrl, *paramTransferProtocolType);
@@ -783,7 +788,7 @@ inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
}
doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
- !(*actionParams.writeProtected),
+ !(actionParams.writeProtected.value_or(false)),
std::move(*actionParams.userName),
std::move(*actionParams.password));
}