From 920c8e2c85d785a3c80e698dec5b922c969901c0 Mon Sep 17 00:00:00 2001 From: Jarle Aase Date: Sat, 17 Aug 2024 15:13:06 +0300 Subject: [PATCH] Fixing various issues reported by static code analysis --- src/ChunkedReaderImpl.cpp | 5 ++--- src/ChunkedWriterImpl.cpp | 5 ++--- src/ConnectionPoolImpl.cpp | 2 +- src/ReplyImpl.cpp | 2 +- src/RequestBodyStringImpl.cpp | 4 ++-- src/RequestImpl.cpp | 19 +++++++++---------- src/RestClientImpl.cpp | 3 +-- 7 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/ChunkedReaderImpl.cpp b/src/ChunkedReaderImpl.cpp index 259e5ef..1bdf03d 100644 --- a/src/ChunkedReaderImpl.cpp +++ b/src/ChunkedReaderImpl.cpp @@ -103,12 +103,11 @@ class ChunkedReaderImpl : public DataReader { if (eat_chunk_padding_) { eat_chunk_padding_ = false; - char ch = {}; - if ((ch = stream_->Getc()) != '\r') { + if (stream_->Getc() != '\r') { throw ParseException("Chunk: Missing padding CR!"); } - if ((ch = stream_->Getc()) != '\n') { + if (stream_->Getc() != '\n') { throw ParseException("Chunk: Missing padding LF!"); } } diff --git a/src/ChunkedWriterImpl.cpp b/src/ChunkedWriterImpl.cpp index dcdef3d..79fca4b 100644 --- a/src/ChunkedWriterImpl.cpp +++ b/src/ChunkedWriterImpl.cpp @@ -34,9 +34,8 @@ class ChunkedWriterImpl : public DataWriter { void Write(const write_buffers_t& buffers) override { const auto len = boost::asio::buffer_size(buffers); buffers_.resize(1); - for(auto &b : buffers) { - buffers_.push_back(b); - } + + std::copy(buffers.begin(), buffers.end(), std::back_insert_iterator{buffers_}); DoWrite(len); } diff --git a/src/ConnectionPoolImpl.cpp b/src/ConnectionPoolImpl.cpp index c2627be..9216ab0 100644 --- a/src/ConnectionPoolImpl.cpp +++ b/src/ConnectionPoolImpl.cpp @@ -146,7 +146,7 @@ class ConnectionPoolImpl }; - ConnectionPoolImpl(RestClient& owner) + explicit ConnectionPoolImpl(RestClient& owner) : owner_{owner}, properties_{owner.GetConnectionProperties()} , cache_cleanup_timer_{owner.GetIoService()} { diff --git a/src/ReplyImpl.cpp b/src/ReplyImpl.cpp index dc6b093..b33774d 100644 --- a/src/ReplyImpl.cpp +++ b/src/ReplyImpl.cpp @@ -75,7 +75,7 @@ ReplyImpl::~ReplyImpl() { << "received data."); connection_->GetSocket().Close(); connection_.reset(); - } catch(std::exception& ex) { + } catch(const std::exception& ex) { RESTC_CPP_LOG_WARN_("~ReplyImpl(): Caught exception:" << ex.what()); } } diff --git a/src/RequestBodyStringImpl.cpp b/src/RequestBodyStringImpl.cpp index 0f26078..fc55fc7 100644 --- a/src/RequestBodyStringImpl.cpp +++ b/src/RequestBodyStringImpl.cpp @@ -15,8 +15,8 @@ namespace impl { class RequestBodyStringImpl : public RequestBody { public: - RequestBodyStringImpl(string body) - : body_{move(body)} + explicit RequestBodyStringImpl(string body) + : body_{std::move(body)} { } diff --git a/src/RequestImpl.cpp b/src/RequestImpl.cpp index 1139209..383c82d 100644 --- a/src/RequestImpl.cpp +++ b/src/RequestImpl.cpp @@ -86,18 +86,18 @@ constexpr char SOCKS5_HOSTNAME_ADDR = 0x03; * ipv4: 1.2.3.4:123 -> "1.2.3.4", 123 * ipv6: [fe80::4479:f6ff:fea3:aa23]:123 -> "fe80::4479:f6ff:fea3:aa23", 123 */ -pair ParseAddress(const std::string addr) { +pair ParseAddress(const std::string& addr) { auto pos = addr.find('['); // IPV6 string host; string port; if (pos != string::npos) { - auto host = addr.substr(1); // strip '[' + host = addr.substr(1); // strip '[' pos = host.find(']'); if (pos == string::npos) { throw ParseException{"IPv6 address must have a ']'"}; } port = host.substr(pos); - host = host.substr(0, pos); + host.resize(pos); if (port.size() < 3 || (host.at(1) != ':')) { throw ParseException{"Need `]:` in "s + addr}; @@ -179,7 +179,7 @@ void ParseAddressIntoSocke5ConnectRequest(const std::string& addr, } // Return 0 whene there is no more bytes to read -size_t ValidateCompleteSocks5ConnectReply(uint8_t *buf, size_t len) { +size_t ValidateCompleteSocks5ConnectReply(const uint8_t *buf, size_t len) { if (len < 5) { throw RestcCppException{"SOCKS5 server connect reply must start at minimum 5 bytes"s}; } @@ -223,7 +223,7 @@ size_t ValidateCompleteSocks5ConnectReply(uint8_t *buf, size_t len) { void DoSocks5Handshake(Connection& connection, const Url& url, - const Request::Properties properties, + const Request::Properties& properties, Context& ctx) { assert(properties.proxy.type == Request::Proxy::Type::SOCKS5); @@ -482,8 +482,9 @@ class RequestImpl : public Request { } // Add arguments to the path as ?name=value&name=value... - bool first_arg = true; + if (add_url_args_) { + bool first_arg = true; // Normal processing. request_buffer << url_encode(parsed_url_.GetPath()); for(const auto& arg : properties_->args) { @@ -609,9 +610,7 @@ class RequestImpl : public Request { auto ep = resolver.async_resolve(q, ctx.GetYield()); const decltype(ep) addr_end; - for(; ep != addr_end; ++ep) - if (ep != addr_end) { - + for(; ep != addr_end; ++ep) { RESTC_CPP_LOG_TRACE_("ep=" << ep->endpoint() << ", protocol=" << ep->endpoint().protocol().protocol()); if (protocol == ep->endpoint().protocol()) { @@ -955,7 +954,7 @@ class RequestImpl : public Request { * received. */ - return move(reply); + return reply; } diff --git a/src/RestClientImpl.cpp b/src/RestClientImpl.cpp index 8e4c17c..2fdadfb 100644 --- a/src/RestClientImpl.cpp +++ b/src/RestClientImpl.cpp @@ -22,8 +22,7 @@ using namespace std; namespace restc_cpp { - -class RestClientImpl : public RestClient { +class RestClientImpl final : public RestClient { public: /*! Proper shutdown handling