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>
diff --git a/redfish-core/include/query.hpp b/redfish-core/include/query.hpp
index 78de6ca..5962093 100644
--- a/redfish-core/include/query.hpp
+++ b/redfish-core/include/query.hpp
@@ -135,7 +135,7 @@
 
     std::optional<query_param::Query> queryOpt =
         query_param::parseParameters(req.url().params(), asyncResp->res);
-    if (queryOpt == std::nullopt)
+    if (!queryOpt)
     {
         return false;
     }
diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp
index 3d3ae8d..5b4ad9a 100644
--- a/redfish-core/include/utils/json_utils.hpp
+++ b/redfish-core/include/utils/json_utils.hpp
@@ -552,7 +552,7 @@
                    std::string_view key, UnpackTypes&&... in)
 {
     std::optional<nlohmann::json> jsonRequest = readJsonPatchHelper(req, res);
-    if (jsonRequest == std::nullopt)
+    if (!jsonRequest)
     {
         return false;
     }
diff --git a/redfish-core/lib/account_service.hpp b/redfish-core/lib/account_service.hpp
index 173de73..9f2748d 100644
--- a/redfish-core/lib/account_service.hpp
+++ b/redfish-core/lib/account_service.hpp
@@ -1806,7 +1806,7 @@
 inline void processAfterGetAllGroups(
     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
     const std::string& username, const std::string& password,
-    const std::optional<std::string>& roleId, std::optional<bool> enabled,
+    const std::string& roleId, bool enabled,
     std::optional<std::vector<std::string>> accountTypes,
     const std::vector<std::string>& allGroupsList)
 {
@@ -1871,7 +1871,6 @@
         messages::internalError(asyncResp->res);
         return;
     }
-
     crow::connections::systemBus->async_method_call(
         [asyncResp, username, password](const boost::system::error_code& ec2,
                                         sdbusplus::message_t& m) {
@@ -1879,7 +1878,7 @@
         },
         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
-        *roleId, *enabled);
+        roleId, enabled);
 }
 
 inline void handleAccountCollectionPost(
@@ -1892,24 +1891,28 @@
     }
     std::string username;
     std::string password;
-    std::optional<std::string> roleId("User");
-    std::optional<bool> enabled = true;
+    std::optional<std::string> roleIdJson;
+    std::optional<bool> enabledJson;
     std::optional<std::vector<std::string>> accountTypes;
-    if (!json_util::readJsonPatch(
-            req, asyncResp->res, "UserName", username, "Password", password,
-            "RoleId", roleId, "Enabled", enabled, "AccountTypes", accountTypes))
+    if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
+                                  "Password", password, "RoleId", roleIdJson,
+                                  "Enabled", enabledJson, "AccountTypes",
+                                  accountTypes))
     {
         return;
     }
 
-    std::string priv = getPrivilegeFromRoleId(*roleId);
+    std::string roleId = roleIdJson.value_or("User");
+    std::string priv = getPrivilegeFromRoleId(roleId);
     if (priv.empty())
     {
-        messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
+        messages::propertyValueNotInList(asyncResp->res, roleId, "RoleId");
         return;
     }
     roleId = priv;
 
+    bool enabled = enabledJson.value_or(true);
+
     // Reading AllGroups property
     sdbusplus::asio::getProperty<std::vector<std::string>>(
         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
diff --git a/redfish-core/lib/ethernet.hpp b/redfish-core/lib/ethernet.hpp
index 7783e7c..57fe24c 100644
--- a/redfish-core/lib/ethernet.hpp
+++ b/redfish-core/lib/ethernet.hpp
@@ -1295,34 +1295,27 @@
             // not explicitly provided are assumed to be unmodified from the
             // current state of the interface. Merge existing state into the
             // current request.
-            const std::string* addr = nullptr;
-            const std::string* gw = nullptr;
-            uint8_t prefixLength = 0;
-            bool errorInEntry = false;
             if (address)
             {
-                if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
-                {
-                    addr = &(*address);
-                }
-                else
+                if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
                 {
                     messages::propertyValueFormatError(asyncResp->res, *address,
                                                        pathString + "/Address");
-                    errorInEntry = true;
+                    return;
                 }
             }
             else if (nicIpEntry != ipv4Data.cend())
             {
-                addr = &(nicIpEntry->address);
+                address = (nicIpEntry->address);
             }
             else
             {
                 messages::propertyMissing(asyncResp->res,
                                           pathString + "/Address");
-                errorInEntry = true;
+                return;
             }
 
+            uint8_t prefixLength = 0;
             if (subnetMask)
             {
                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
@@ -1331,7 +1324,7 @@
                     messages::propertyValueFormatError(
                         asyncResp->res, *subnetMask,
                         pathString + "/SubnetMask");
-                    errorInEntry = true;
+                    return;
                 }
             }
             else if (nicIpEntry != ipv4Data.cend())
@@ -1342,50 +1335,41 @@
                     messages::propertyValueFormatError(
                         asyncResp->res, nicIpEntry->netmask,
                         pathString + "/SubnetMask");
-                    errorInEntry = true;
+                    return;
                 }
             }
             else
             {
                 messages::propertyMissing(asyncResp->res,
                                           pathString + "/SubnetMask");
-                errorInEntry = true;
+                return;
             }
 
             if (gateway)
             {
-                if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
-                {
-                    gw = &(*gateway);
-                }
-                else
+                if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
                 {
                     messages::propertyValueFormatError(asyncResp->res, *gateway,
                                                        pathString + "/Gateway");
-                    errorInEntry = true;
+                    return;
                 }
             }
             else if (nicIpEntry != ipv4Data.cend())
             {
-                gw = &nicIpEntry->gateway;
+                gateway = nicIpEntry->gateway;
             }
             else
             {
                 messages::propertyMissing(asyncResp->res,
                                           pathString + "/Gateway");
-                errorInEntry = true;
-            }
-
-            if (errorInEntry)
-            {
                 return;
             }
 
             if (nicIpEntry != ipv4Data.cend())
             {
                 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
-                                         nicIpEntry->id, prefixLength, *gw,
-                                         *addr, asyncResp);
+                                         nicIpEntry->id, prefixLength, *gateway,
+                                         *address, asyncResp);
                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
                                                   ipv4Data.cend());
             }
diff --git a/redfish-core/lib/virtual_media.hpp b/redfish-core/lib/virtual_media.hpp
index e425cc1..9ef45e7 100644
--- a/redfish-core/lib/virtual_media.hpp
+++ b/redfish-core/lib/virtual_media.hpp
@@ -391,7 +391,7 @@
 inline std::optional<TransferProtocol> getTransferProtocolFromParam(
     const std::optional<std::string>& transferProtocolType)
 {
-    if (transferProtocolType == std::nullopt)
+    if (!transferProtocolType)
     {
         return {};
     }
@@ -670,7 +670,7 @@
     }
 
     // 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 @@
     }
 
     // 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 @@
         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 @@
     }
 
     // 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 @@
     }
 
     // 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 @@
                              "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 @@
     }
 
     doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
-                    !(*actionParams.writeProtected),
+                    !(actionParams.writeProtected.value_or(false)),
                     std::move(*actionParams.userName),
                     std::move(*actionParams.password));
 }