From e01d0c36af115ed46d54b5dbbacfe3ad92226bd3 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Fri, 30 Jun 2023 13:21:32 -0700 Subject: 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 --- .../include/utils/query_param_test.cpp | 56 +++++++++++++++++----- 1 file changed, 45 insertions(+), 11 deletions(-) (limited to 'test/redfish-core') diff --git a/test/redfish-core/include/utils/query_param_test.cpp b/test/redfish-core/include/utils/query_param_test.cpp index 7aa4ad519b..c5ae21f1d2 100644 --- a/test/redfish-core/include/utils/query_param_test.cpp +++ b/test/redfish-core/include/utils/query_param_test.cpp @@ -358,9 +358,12 @@ TEST(RecursiveSelect, ReservedPropertiesAreSelected) ASSERT_TRUE(ret); crow::Response res; std::optional query = parseParameters(ret->params(), res); - - ASSERT_NE(query, std::nullopt); - recursiveSelect(root, query->selectTrie.root); + ASSERT_TRUE(query); + if (!query) + { + return; + } + recursiveSelect(root, query.value().selectTrie.root); EXPECT_EQ(root, expected); } @@ -508,10 +511,18 @@ TEST(QueryParams, ParseParametersOnly) { auto ret = boost::urls::parse_relative_ref("/redfish/v1?only"); ASSERT_TRUE(ret); + if (!ret) + { + return; + } crow::Response res; std::optional query = parseParameters(ret->params(), res); - ASSERT_TRUE(query != std::nullopt); + ASSERT_TRUE(query); + if (!query) + { + return; + } EXPECT_TRUE(query->isOnly); } @@ -519,14 +530,22 @@ TEST(QueryParams, ParseParametersExpand) { auto ret = boost::urls::parse_relative_ref("/redfish/v1?$expand=*"); ASSERT_TRUE(ret); + if (!ret) + { + return; + } crow::Response res; std::optional query = parseParameters(ret->params(), res); if constexpr (bmcwebInsecureEnableQueryParams) { - ASSERT_NE(query, std::nullopt); - EXPECT_TRUE(query->expandType == + ASSERT_TRUE(query); + if (!query) + { + return; + } + EXPECT_TRUE(query.value().expandType == redfish::query_param::ExpandType::Both); } else @@ -539,12 +558,20 @@ TEST(QueryParams, ParseParametersTop) { auto ret = boost::urls::parse_relative_ref("/redfish/v1?$top=1"); ASSERT_TRUE(ret); + if (!ret) + { + return; + } crow::Response res; std::optional query = parseParameters(ret->params(), res); - ASSERT_TRUE(query != std::nullopt); - EXPECT_EQ(query->top, 1); + ASSERT_TRUE(query); + if (!query) + { + return; + } + EXPECT_EQ(query.value().top, 1); } TEST(QueryParams, ParseParametersTopOutOfRangeNegative) @@ -562,7 +589,10 @@ TEST(QueryParams, ParseParametersTopOutOfRangePositive) { auto ret = boost::urls::parse_relative_ref("/redfish/v1?$top=1001"); ASSERT_TRUE(ret); - + if (!ret) + { + return; + } crow::Response res; std::optional query = parseParameters(ret->params(), res); @@ -577,8 +607,12 @@ TEST(QueryParams, ParseParametersSkip) crow::Response res; std::optional query = parseParameters(ret->params(), res); - ASSERT_TRUE(query != std::nullopt); - EXPECT_EQ(query->skip, 1); + ASSERT_TRUE(query); + if (!query) + { + return; + } + EXPECT_EQ(query.value().skip, 1); } TEST(QueryParams, ParseParametersSkipOutOfRange) { -- cgit v1.2.3