diff options
author | Ed Tanous <edtanous@google.com> | 2023-02-16 21:28:44 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2023-02-24 20:21:14 +0300 |
commit | 26ccae32112679c4653c1e3f8a1203c828bea05c (patch) | |
tree | a87d5056294e163b9ddeed9e867bcfb0750a8bb5 | |
parent | 7da1c58890e82194ff83ca6c8d55b5c327f2444a (diff) | |
download | bmcweb-26ccae32112679c4653c1e3f8a1203c828bea05c.tar.xz |
Pass string views by value
string_view should always be passed by value; This commit is a sed
replace of the code to make all string_views pass by value, per general
coding guidelines[1].
[1] https://quuxplusone.github.io/blog/2021/11/09/pass-string-view-by-value/
Tested: Code compiles.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I55b342a29a0fbfce0a4ed9ea63db6014d03b134c
-rw-r--r-- | COMMON_ERRORS.md | 2 | ||||
-rw-r--r-- | http/http_request.hpp | 2 | ||||
-rw-r--r-- | http/http_response.hpp | 2 | ||||
-rw-r--r-- | http/routing.hpp | 6 | ||||
-rw-r--r-- | http/utility.hpp | 13 | ||||
-rw-r--r-- | http/websocket.hpp | 6 | ||||
-rw-r--r-- | include/http_utility.hpp | 2 | ||||
-rw-r--r-- | include/human_sort.hpp | 3 | ||||
-rw-r--r-- | include/ibm/utils.hpp | 2 | ||||
-rw-r--r-- | include/nbd_proxy.hpp | 2 | ||||
-rw-r--r-- | include/openbmc_dbus_rest.hpp | 3 | ||||
-rw-r--r-- | include/pam_authenticate.hpp | 4 | ||||
-rw-r--r-- | include/security_headers.hpp | 2 | ||||
-rw-r--r-- | include/sessions.hpp | 8 | ||||
-rw-r--r-- | redfish-core/include/event_service_manager.hpp | 2 | ||||
-rw-r--r-- | redfish-core/include/privileges.hpp | 2 | ||||
-rw-r--r-- | redfish-core/include/redfish_aggregator.hpp | 2 | ||||
-rw-r--r-- | redfish-core/include/registries.hpp | 4 | ||||
-rw-r--r-- | redfish-core/include/resource_messages.hpp | 2 | ||||
-rw-r--r-- | redfish-core/lib/certificate_service.hpp | 8 | ||||
-rw-r--r-- | redfish-core/lib/log_services.hpp | 11 | ||||
-rw-r--r-- | redfish-core/lib/redfish_v1.hpp | 2 | ||||
-rw-r--r-- | redfish-core/lib/task.hpp | 2 | ||||
-rw-r--r-- | test/http/router_test.cpp | 6 |
24 files changed, 45 insertions, 53 deletions
diff --git a/COMMON_ERRORS.md b/COMMON_ERRORS.md index 6cb7340bfd..aa4b042654 100644 --- a/COMMON_ERRORS.md +++ b/COMMON_ERRORS.md @@ -23,7 +23,7 @@ This pointer is not guaranteed to be filled, and could be a null dereference. ## 2. String views aren't null terminated ```cpp -int getIntFromString(const std::string_view s){ +int getIntFromString(std::string_view s){ return std::atoi(s.data()); } ``` diff --git a/http/http_request.hpp b/http/http_request.hpp index d203077e10..405db870fa 100644 --- a/http/http_request.hpp +++ b/http/http_request.hpp @@ -92,7 +92,7 @@ struct Request return req.target(); } - bool target(const std::string_view target) + bool target(std::string_view target) { req.target(target); return setUrlInfo(); diff --git a/http/http_response.hpp b/http/http_response.hpp index f9204e4a4e..5cbdb28eb3 100644 --- a/http/http_response.hpp +++ b/http/http_response.hpp @@ -27,7 +27,7 @@ struct Response nlohmann::json jsonValue; - void addHeader(const std::string_view key, const std::string_view value) + void addHeader(std::string_view key, std::string_view value) { stringResponse->set(key, value); } diff --git a/http/routing.hpp b/http/routing.hpp index 5b3ddfe429..ae63f0504c 100644 --- a/http/routing.hpp +++ b/http/routing.hpp @@ -422,7 +422,7 @@ struct RuleParameterTraits return *p; } - self_t& name(const std::string_view name) noexcept + self_t& name(std::string_view name) noexcept { self_t* self = static_cast<self_t*>(this); self->nameStr = name; @@ -663,7 +663,7 @@ class TaggedRule : } template <typename Func> - void operator()(const std::string_view name, Func&& f) + void operator()(std::string_view name, Func&& f) { nameStr = name; (*this).template operator()<Func>(std::forward(f)); @@ -807,7 +807,7 @@ class Trie } std::pair<unsigned, RoutingParams> - find(const std::string_view reqUrl, const Node* node = nullptr, + find(std::string_view reqUrl, const Node* node = nullptr, size_t pos = 0, RoutingParams* params = nullptr) const { RoutingParams empty; diff --git a/http/utility.hpp b/http/utility.hpp index 4ea44d5863..71f39c8f76 100644 --- a/http/utility.hpp +++ b/http/utility.hpp @@ -378,7 +378,7 @@ struct FunctionTraits using arg = std::tuple_element_t<i, boost::callable_traits::args_t<T>>; }; -inline std::string base64encode(const std::string_view data) +inline std::string base64encode(std::string_view data) { const std::array<char, 64> key = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', @@ -438,7 +438,7 @@ inline std::string base64encode(const std::string_view data) // TODO this is temporary and should be deleted once base64 is refactored out of // crow -inline bool base64Decode(const std::string_view input, std::string& output) +inline bool base64Decode(std::string_view input, std::string& output) { static const char nop = static_cast<char>(-1); // See note on encoding_data[] in above function @@ -541,8 +541,7 @@ inline bool base64Decode(const std::string_view input, std::string& output) return true; } -inline bool constantTimeStringCompare(const std::string_view a, - const std::string_view b) +inline bool constantTimeStringCompare(std::string_view a, std::string_view b) { // Important note, this function is ONLY constant time if the two input // sizes are the same @@ -555,7 +554,7 @@ inline bool constantTimeStringCompare(const std::string_view a, struct ConstantTimeCompare { - bool operator()(const std::string_view a, const std::string_view b) const + bool operator()(std::string_view a, std::string_view b) const { return constantTimeStringCompare(a, b); } @@ -567,7 +566,7 @@ inline boost::urls::url appendUrlPieces(boost::urls::url& url, const std::initializer_list<std::string_view> args) { - for (const std::string_view& arg : args) + for (std::string_view arg : args) { url.segments().push_back(arg); } @@ -696,7 +695,7 @@ inline bool readUrlSegments(const boost::urls::url_view& urlView, inline boost::urls::url replaceUrlSegment(const boost::urls::url_view& urlView, const uint replaceLoc, - const std::string_view newSegment) + std::string_view newSegment) { const boost::urls::segments_view& urlSegments = urlView.segments(); boost::urls::url url("/"); diff --git a/http/websocket.hpp b/http/websocket.hpp index 0ab13f2cdd..c36e579ea8 100644 --- a/http/websocket.hpp +++ b/http/websocket.hpp @@ -148,7 +148,7 @@ class ConnectionImpl : public Connection }); } - void sendBinary(const std::string_view msg) override + void sendBinary(std::string_view msg) override { ws.binary(true); outBuffer.emplace_back(msg); @@ -162,7 +162,7 @@ class ConnectionImpl : public Connection doWrite(); } - void sendText(const std::string_view msg) override + void sendText(std::string_view msg) override { ws.text(true); outBuffer.emplace_back(msg); @@ -176,7 +176,7 @@ class ConnectionImpl : public Connection doWrite(); } - void close(const std::string_view msg) override + void close(std::string_view msg) override { ws.async_close( {boost::beast::websocket::close_code::normal, msg}, diff --git a/include/http_utility.hpp b/include/http_utility.hpp index cf3ddc2121..8f2478fcd2 100644 --- a/include/http_utility.hpp +++ b/include/http_utility.hpp @@ -109,7 +109,7 @@ inline bool isContentTypeAllowed(std::string_view header, ContentType type, return type == allowed; } -inline std::string urlEncode(const std::string_view value) +inline std::string urlEncode(std::string_view value) { std::ostringstream escaped; escaped.fill('0'); diff --git a/include/human_sort.hpp b/include/human_sort.hpp index f4dfbe03c7..7ab326d1a1 100644 --- a/include/human_sort.hpp +++ b/include/human_sort.hpp @@ -22,8 +22,7 @@ enum class ModeType } // namespace details -inline int alphanumComp(const std::string_view left, - const std::string_view right) +inline int alphanumComp(std::string_view left, std::string_view right) { std::string_view::const_iterator l = left.begin(); diff --git a/include/ibm/utils.hpp b/include/ibm/utils.hpp index 256cbc52b5..bb439ee423 100644 --- a/include/ibm/utils.hpp +++ b/include/ibm/utils.hpp @@ -10,7 +10,7 @@ namespace crow namespace ibm_utils { -inline bool createDirectory(const std::string_view path) +inline bool createDirectory(std::string_view path) { // Create persistent directory std::error_code ec; diff --git a/include/nbd_proxy.hpp b/include/nbd_proxy.hpp index 3472c3a514..500ea76555 100644 --- a/include/nbd_proxy.hpp +++ b/include/nbd_proxy.hpp @@ -109,7 +109,7 @@ struct NbdProxyServer : std::enable_shared_from_this<NbdProxyServer> "xyz.openbmc_project.VirtualMedia.Proxy", "Mount"); } - void send(const std::string_view data) + void send(std::string_view data) { boost::asio::buffer_copy(ws2uxBuf.prepare(data.size()), boost::asio::buffer(data)); diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp index 9bf968a40d..30351051dd 100644 --- a/include/openbmc_dbus_rest.hpp +++ b/include/openbmc_dbus_rest.hpp @@ -108,8 +108,7 @@ inline bool validateFilename(const std::string& filename) inline void setErrorResponse(crow::Response& res, boost::beast::http::status result, - const std::string& desc, - const std::string_view msg) + const std::string& desc, std::string_view msg) { res.result(result); res.jsonValue["data"]["description"] = desc; diff --git a/include/pam_authenticate.hpp b/include/pam_authenticate.hpp index ca8c8d3250..5802078e4a 100644 --- a/include/pam_authenticate.hpp +++ b/include/pam_authenticate.hpp @@ -83,8 +83,8 @@ inline int pamFunctionConversation(int numMsg, const struct pam_message** msg, * @param username The provided username aka account name. * @param password The provided password. * @returns PAM error code or PAM_SUCCESS for success. */ -inline int pamAuthenticateUser(const std::string_view username, - const std::string_view password) +inline int pamAuthenticateUser(std::string_view username, + std::string_view password) { std::string userStr(username); std::string passStr(password); diff --git a/include/security_headers.hpp b/include/security_headers.hpp index d724de4e8a..9877bb0b1a 100644 --- a/include/security_headers.hpp +++ b/include/security_headers.hpp @@ -58,7 +58,7 @@ inline void addSecurityHeaders(const crow::Request& req [[maybe_unused]], "object-src *; " "base-uri *"); - const std::string_view origin = req.getHeaderValue("Origin"); + std::string_view origin = req.getHeaderValue("Origin"); res.addHeader(bf::access_control_allow_origin, origin); res.addHeader(bf::access_control_allow_methods, "GET, " "POST, " diff --git a/include/sessions.hpp b/include/sessions.hpp index 19a1793ff9..9795719b31 100644 --- a/include/sessions.hpp +++ b/include/sessions.hpp @@ -201,8 +201,7 @@ class SessionStore { public: std::shared_ptr<UserSession> generateUserSession( - const std::string_view username, - const boost::asio::ip::address& clientIp, + std::string_view username, const boost::asio::ip::address& clientIp, const std::optional<std::string>& clientId, PersistenceType persistence = PersistenceType::TIMEOUT, bool isConfigureSelfOnly = false) @@ -264,8 +263,7 @@ class SessionStore return it.first->second; } - std::shared_ptr<UserSession> - loginSessionByToken(const std::string_view token) + std::shared_ptr<UserSession> loginSessionByToken(std::string_view token) { applySessionTimeouts(); if (token.size() != sessionTokenSize) @@ -282,7 +280,7 @@ class SessionStore return userSession; } - std::shared_ptr<UserSession> getSessionByUid(const std::string_view uid) + std::shared_ptr<UserSession> getSessionByUid(std::string_view uid) { applySessionTimeouts(); // TODO(Ed) this is inefficient diff --git a/redfish-core/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp index 976d043e6e..9668e5d691 100644 --- a/redfish-core/include/event_service_manager.hpp +++ b/redfish-core/include/event_service_manager.hpp @@ -114,7 +114,7 @@ static const Message* return nullptr; } -static const Message* formatMessage(const std::string_view& messageID) +static const Message* formatMessage(std::string_view messageID) { // Redfish MessageIds are in the form // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find diff --git a/redfish-core/include/privileges.hpp b/redfish-core/include/privileges.hpp index 62276815c3..08e554a86c 100644 --- a/redfish-core/include/privileges.hpp +++ b/redfish-core/include/privileges.hpp @@ -110,7 +110,7 @@ class Privileges * @return None * */ - bool setSinglePrivilege(const std::string_view privilege) + bool setSinglePrivilege(std::string_view privilege) { for (size_t searchIndex = 0; searchIndex < privilegeNames.size(); searchIndex++) diff --git a/redfish-core/include/redfish_aggregator.hpp b/redfish-core/include/redfish_aggregator.hpp index c2c2e1db66..080e67e681 100644 --- a/redfish-core/include/redfish_aggregator.hpp +++ b/redfish-core/include/redfish_aggregator.hpp @@ -44,7 +44,7 @@ constexpr std::array nonUriProperties{ // Determines if the passed property contains a URI. Those property names // either end with a case-insensitive version of "uri" or are specifically // defined in the above array. -inline bool isPropertyUri(const std::string_view propertyName) +inline bool isPropertyUri(std::string_view propertyName) { return boost::iends_with(propertyName, "uri") || std::binary_search(nonUriProperties.begin(), nonUriProperties.end(), diff --git a/redfish-core/include/registries.hpp b/redfish-core/include/registries.hpp index da8fa85ff6..03e8bc55a1 100644 --- a/redfish-core/include/registries.hpp +++ b/redfish-core/include/registries.hpp @@ -61,7 +61,7 @@ inline std::string { std::string ret; size_t reserve = msg.size(); - for (const std::string_view& arg : messageArgs) + for (std::string_view arg : messageArgs) { reserve += arg.size(); } @@ -102,7 +102,7 @@ inline nlohmann::json::object_t std::string msg = redfish::registries::fillMessageArgs(args, entry.second.message); nlohmann::json jArgs = nlohmann::json::array(); - for (const std::string_view arg : args) + for (std::string_view arg : args) { jArgs.push_back(arg); } diff --git a/redfish-core/include/resource_messages.hpp b/redfish-core/include/resource_messages.hpp index 315393548b..309b293a65 100644 --- a/redfish-core/include/resource_messages.hpp +++ b/redfish-core/include/resource_messages.hpp @@ -11,7 +11,7 @@ namespace messages inline nlohmann::json getLogResourceEvent(redfish::registries::resource_event::Index name, - std::span<const std::string_view> args) + std::span<std::string_view> args) { size_t index = static_cast<size_t>(name); if (index >= redfish::registries::resource_event::registry.size()) diff --git a/redfish-core/lib/certificate_service.hpp b/redfish-core/lib/certificate_service.hpp index 16f26e37b2..93cb366d99 100644 --- a/redfish-core/lib/certificate_service.hpp +++ b/redfish-core/lib/certificate_service.hpp @@ -148,7 +148,7 @@ class CertificateFile * @return None */ static void updateCertIssuerOrSubject(nlohmann::json& out, - const std::string_view value) + std::string_view value) { // example: O=openbmc-project.xyz,CN=localhost std::string_view::iterator i = value.begin(); @@ -163,16 +163,14 @@ static void updateCertIssuerOrSubject(nlohmann::json& out, { break; } - const std::string_view key(tokenBegin, - static_cast<size_t>(i - tokenBegin)); + std::string_view key(tokenBegin, static_cast<size_t>(i - tokenBegin)); ++i; tokenBegin = i; while (i != value.end() && *i != ',') { ++i; } - const std::string_view val(tokenBegin, - static_cast<size_t>(i - tokenBegin)); + std::string_view val(tokenBegin, static_cast<size_t>(i - tokenBegin)); if (key == "L") { out["City"] = val; diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp index bb9552a53d..ee57557064 100644 --- a/redfish-core/lib/log_services.hpp +++ b/redfish-core/lib/log_services.hpp @@ -91,7 +91,7 @@ static const Message* return nullptr; } -static const Message* getMessage(const std::string_view& messageID) +static const Message* getMessage(std::string_view messageID) { // Redfish MessageIds are in the form // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find @@ -157,7 +157,7 @@ inline std::optional<bool> getProviderNotifyAction(const std::string& notify) } inline static int getJournalMetadata(sd_journal* journal, - const std::string_view& field, + std::string_view field, std::string_view& contents) { const char* data = nullptr; @@ -179,8 +179,8 @@ inline static int getJournalMetadata(sd_journal* journal, } inline static int getJournalMetadata(sd_journal* journal, - const std::string_view& field, - const int& base, long int& contents) + std::string_view field, const int& base, + long int& contents) { int ret = 0; std::string_view metadata; @@ -3321,8 +3321,7 @@ enum class OEMDiagnosticType invalid, }; -inline OEMDiagnosticType - getOEMDiagnosticType(const std::string_view& oemDiagStr) +inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr) { if (oemDiagStr == "OnDemand") { diff --git a/redfish-core/lib/redfish_v1.hpp b/redfish-core/lib/redfish_v1.hpp index b4349abb5b..4cae985c21 100644 --- a/redfish-core/lib/redfish_v1.hpp +++ b/redfish-core/lib/redfish_v1.hpp @@ -83,7 +83,7 @@ inline void json["Name"] = "JsonSchemaFile Collection"; json["Description"] = "Collection of JsonSchemaFiles"; nlohmann::json::array_t members; - for (const std::string_view schema : schemas) + for (std::string_view schema : schemas) { nlohmann::json::object_t member; member["@odata.id"] = crow::utility::urlFromPieces( diff --git a/redfish-core/lib/task.hpp b/redfish-core/lib/task.hpp index 563f06f592..95fa0c0073 100644 --- a/redfish-core/lib/task.hpp +++ b/redfish-core/lib/task.hpp @@ -196,7 +196,7 @@ struct TaskData : std::enable_shared_from_this<TaskData> }); } - static void sendTaskEvent(const std::string_view state, size_t index) + static void sendTaskEvent(std::string_view state, size_t index) { std::string origin = "/redfish/v1/TaskService/Tasks/" + std::to_string(index); diff --git a/test/http/router_test.cpp b/test/http/router_test.cpp index 9b5d9bec98..79320f7e04 100644 --- a/test/http/router_test.cpp +++ b/test/http/router_test.cpp @@ -36,7 +36,7 @@ TEST(Router, AllowHeader) Router router; std::error_code ec; - constexpr const std::string_view url = "/foo"; + constexpr std::string_view url = "/foo"; Request req{{boost::beast::http::verb::get, url, 11}, ec}; @@ -75,7 +75,7 @@ TEST(Router, 404) Router router; std::error_code ec; - constexpr const std::string_view url = "/foo/bar"; + constexpr std::string_view url = "/foo/bar"; Request req{{boost::beast::http::verb::get, url, 11}, ec}; @@ -105,7 +105,7 @@ TEST(Router, 405) Router router; std::error_code ec; - constexpr const std::string_view url = "/foo/bar"; + constexpr std::string_view url = "/foo/bar"; Request req{{boost::beast::http::verb::patch, url, 11}, ec}; |