Skip to content

Commit

Permalink
Fixing various issues reported by static code analysis and compiler w…
Browse files Browse the repository at this point in the history
…arnings
  • Loading branch information
jgaa committed Aug 17, 2024
1 parent 5830d8d commit 1cacbfe
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 34 deletions.
16 changes: 14 additions & 2 deletions include/restc-cpp/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <thread>
#include <iomanip>
#include <array>
#include <ctime>

namespace restc_cpp {

Expand Down Expand Up @@ -108,6 +109,17 @@ class Logger {

}


inline std::tm *restc_cpp_localtime(const time_t now, std::tm& timeInfo) {
#ifdef _WIN32
localtime_s(&timeInfo, &now); // Windows-specific
#else
localtime_r(&now, &timeInfo); // POSIX-specific
#endif

return &timeInfo;
}

//#define RESTC_CPP_TEST_LOGGING_SETUP(level) RestcCppTestStartLogger(level)
#define RESTC_CPP_TEST_LOGGING_SETUP(level) RestcCppTestStartLogger("trace")

Expand All @@ -130,9 +142,9 @@ inline void RestcCppTestStartLogger(const std::string& level = "info") {
const std::string& msg) {
static const std::array<std::string, 6> levels = {"NONE", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"};

const auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm timeInfo = {};

std::clog << std::put_time(std::localtime(&now), "%c") << ' '
std::clog << std::put_time(restc_cpp_localtime(time({}), timeInfo), "%c") << ' '
<< levels.at(static_cast<size_t>(level))
<< ' ' << std::this_thread::get_id() << ' '
<< msg << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions src/ChunkedReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ChunkedReaderImpl : public DataReader {
public:

ChunkedReaderImpl(add_header_fn_t&& fn, unique_ptr<DataReaderStream>&& source)
: stream_{move(source)}, add_header_(move(fn))
: stream_{std::move(source)}, add_header_(std::move(fn))
{
}

Expand Down Expand Up @@ -171,7 +171,7 @@ class ChunkedReaderImpl : public DataReader {

DataReader::ptr_t
DataReader::CreateChunkedReader(add_header_fn_t fn, unique_ptr<DataReaderStream>&& source) {
return make_unique<ChunkedReaderImpl>(move(fn), move(source));
return make_unique<ChunkedReaderImpl>(std::move(fn), std::move(source));
}


Expand Down
8 changes: 4 additions & 4 deletions src/ConnectionPoolImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ConnectionPoolImpl
struct Key {
Key(boost::asio::ip::tcp::endpoint ep,
const Connection::Type connectionType)
: endpoint{move(ep)}, type{connectionType} {}
: endpoint{std::move(ep)}, type{connectionType} {}

Key(const Key&) = default;
Key(Key&&) = default;
Expand Down Expand Up @@ -70,7 +70,7 @@ class ConnectionPoolImpl
const Connection::Type connectionType,
Connection::ptr_t conn,
const Request::Properties& prop)
: key{move(ep), connectionType}, connection{move(conn)}, ttl{prop.cacheTtlSeconds}
: key{std::move(ep), connectionType}, connection{std::move(conn)}, ttl{prop.cacheTtlSeconds}
, created{time(nullptr)} {}

friend ostream& operator << (ostream& o, const Entry& e) {
Expand Down Expand Up @@ -112,7 +112,7 @@ class ConnectionPoolImpl
using release_callback_t = std::function<void (const Entry::ptr_t&)>;
ConnectionWrapper(Entry::ptr_t entry,
release_callback_t on_release)
: on_release_{move(on_release)}, entry_{move(entry)}
: on_release_{std::move(on_release)}, entry_{std::move(entry)}
{
}

Expand Down Expand Up @@ -378,7 +378,7 @@ class ConnectionPoolImpl
}

auto entry = make_shared<Entry>(ep, connectionType,
make_shared<ConnectionImpl>(move(socket)),
make_shared<ConnectionImpl>(std::move(socket)),
*properties_);

RESTC_CPP_LOG_TRACE_("Created new connection " << *entry);
Expand Down
6 changes: 3 additions & 3 deletions src/DataReaderStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;
namespace restc_cpp {

DataReaderStream::DataReaderStream(std::unique_ptr<DataReader>&& source)
: source_{move(source)} {
: source_{std::move(source)} {
RESTC_CPP_LOG_TRACE_("DataReaderStream: Chained to "
<< RESTC_CPP_TYPENAME(decltype(*source_)));
}
Expand Down Expand Up @@ -135,7 +135,7 @@ void DataReaderStream::ReadServerResponse(Reply::HttpResponse& response)
throw ProtocolException("ReadHeaders(): No CR/LF after HTTP response phrase!");
}

response.reason_phrase = move(value);
response.reason_phrase = std::move(value);
RESTC_CPP_LOG_TRACE_("ReadServerResponse: getc_bytes is " << getc_bytes_);

RESTC_CPP_LOG_TRACE_("HTTP Response: "
Expand Down Expand Up @@ -190,7 +190,7 @@ void DataReaderStream::ReadHeaderLines(const add_header_fn_t& addHeader) {
}

RESTC_CPP_LOG_TRACE_(name << ": " << value);
addHeader(move(name), move(value));
addHeader(std::move(name), std::move(value));
name.clear();
value.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions src/PlainReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PlainReaderImpl : public DataReader {

PlainReaderImpl(size_t contentLength, ptr_t&& source)
: remaining_{contentLength},
source_{move(source)} {}
source_{std::move(source)} {}

[[nodiscard]] bool IsEof() const override { return remaining_ == 0; }

Expand Down Expand Up @@ -47,7 +47,7 @@ class PlainReaderImpl : public DataReader {

DataReader::ptr_t
DataReader::CreatePlainReader(size_t contentLength, ptr_t&& source) {
return make_unique<PlainReaderImpl>(contentLength, move(source));
return make_unique<PlainReaderImpl>(contentLength, std::move(source));
}


Expand Down
4 changes: 2 additions & 2 deletions src/PlainWriterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace restc_cpp {
class PlainWriterImpl : public DataWriter {
public:
PlainWriterImpl(size_t contentLength, ptr_t&& source)
: next_{move(source)}, content_length_{contentLength}
: next_{std::move(source)}, content_length_{contentLength}
{
}

Expand Down Expand Up @@ -47,7 +47,7 @@ class PlainWriterImpl : public DataWriter {

DataWriter::ptr_t
DataWriter::CreatePlainWriter(size_t contentLength, ptr_t&& source) {
return make_unique<PlainWriterImpl>(contentLength, move(source));
return make_unique<PlainWriterImpl>(contentLength, std::move(source));
}

} // namespace
Expand Down
22 changes: 11 additions & 11 deletions src/ReplyImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ReplyImpl::ReplyImpl(Connection::ptr_t connection,
RestClient& owner,
Request::Properties::ptr_t& properties,
Request::Type type)
: connection_{move(connection)}, ctx_{ctx}
: connection_{std::move(connection)}, ctx_{ctx}
, properties_{properties}
, owner_{owner}
, connection_id_(connection_ ? connection_->GetId()
Expand All @@ -57,7 +57,7 @@ ReplyImpl::ReplyImpl(Connection::ptr_t connection,
Context& ctx,
RestClient& owner,
Request::Type type)
: connection_{move(connection)}, ctx_{ctx}
: connection_{std::move(connection)}, ctx_{ctx}
, properties_{owner.GetConnectionProperties()}
, owner_{owner}
, connection_id_(connection_ ? connection_->GetId()
Expand Down Expand Up @@ -93,14 +93,14 @@ void ReplyImpl::StartReceiveFromServer(DataReader::ptr_t&& reader) {
connection_);

assert(reader);
auto stream = make_unique<DataReaderStream>(move(reader));
auto stream = make_unique<DataReaderStream>(std::move(reader));
stream->ReadServerResponse(response_);
stream->ReadHeaderLines(
[this](std::string&& name, std::string&& value) {
headers_.insert({move(name), move(value)});
headers_.insert({std::move(name), std::move(value)});
});

HandleContentType(move(stream));
HandleContentType(std::move(stream));
HandleConnectionLifetime();
HandleDecompression();
CheckIfWeAreDone();
Expand All @@ -115,13 +115,13 @@ void ReplyImpl::HandleContentType(unique_ptr<DataReaderStream>&& stream) {
reader_ = DataReader::CreateNoBodyReader();
} else if (const auto cl = GetHeader(content_len_name)) {
content_length_ = stoi(*cl);
reader_ = DataReader::CreatePlainReader(*content_length_, move(stream));
reader_ = DataReader::CreatePlainReader(*content_length_, std::move(stream));
} else {
auto te = GetHeader(transfer_encoding_name);
if (te && ciEqLibC()(*te, chunked_name)) {
reader_ = DataReader::CreateChunkedReader([this](string&& name, string&& value) {
headers_[name] = move(value);
}, move(stream));
headers_[name] = std::move(value);
}, std::move(stream));
} else {
reader_ = DataReader::CreateNoBodyReader();
}
Expand Down Expand Up @@ -159,10 +159,10 @@ void ReplyImpl::HandleDecompression() {
#ifdef RESTC_CPP_WITH_ZLIB
if (ciEqLibC()(gzip, *it)) {
RESTC_CPP_LOG_TRACE_("Adding gzip reader to " << *connection_);
reader_ = DataReader::CreateGzipReader(move(reader_));
reader_ = DataReader::CreateGzipReader(std::move(reader_));
} else if (ciEqLibC()(deflate, *it)) {
RESTC_CPP_LOG_TRACE_("Adding deflate reader to " << *connection_);
reader_ = DataReader::CreateZipReader(move(reader_));
reader_ = DataReader::CreateZipReader(std::move(reader_));
} else
#endif // RESTC_CPP_WITH_ZLIB
{
Expand Down Expand Up @@ -244,7 +244,7 @@ ReplyImpl::Create(Connection::ptr_t connection,
Request::Properties::ptr_t& properties,
Request::Type type) {

return make_unique<ReplyImpl>(move(connection), ctx, owner, properties, type);
return make_unique<ReplyImpl>(std::move(connection), ctx, owner, properties, type);
}

} // restc_cpp
4 changes: 2 additions & 2 deletions src/RequestBodyFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RequestBodyFileImpl : public RequestBody
{
public:
RequestBodyFileImpl(boost::filesystem::path path)
: path_{move(path)}
: path_{std::move(path)}
, size_{boost::filesystem::file_size(path_)}
{
file_ = make_unique<ifstream>(path_.string(), ios::binary);
Expand Down Expand Up @@ -77,7 +77,7 @@ class RequestBodyFileImpl : public RequestBody
unique_ptr<RequestBody> RequestBody::CreateFileBody(
boost::filesystem::path path) {

return make_unique<impl::RequestBodyFileImpl>(move(path));
return make_unique<impl::RequestBodyFileImpl>(std::move(path));
}

} // restc_cpp
Expand Down
2 changes: 1 addition & 1 deletion src/RequestBodyStringImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class RequestBodyStringImpl : public RequestBody
std::unique_ptr<RequestBody> RequestBody::CreateStringBody(
std::string body) {

return make_unique<impl::RequestBodyStringImpl>(move(body));
return make_unique<impl::RequestBodyStringImpl>(std::move(body));
}

} // restc_cpp
Expand Down
6 changes: 6 additions & 0 deletions src/RequestImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,13 @@ class RequestImpl : public Request {
std::string pre_base = auth.name + ':' + auth.passwd;
properties_->headers[authorization]
= basic_sp + Base64Encode(pre_base);
#if __cplusplus >= 201703L // C++17 or later
std::memset(pre_base.data(), 0, pre_base.capacity());
#else // C++14 or earlier
if (!pre_base.empty()) {
std::memset(&pre_base[0], 0, pre_base.capacity());
}
#endif
pre_base.clear();
}

Expand Down
6 changes: 3 additions & 3 deletions src/ZipReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ZipReaderImpl : public DataReader {

ZipReaderImpl(std::unique_ptr<DataReader>&& source,
const Format format)
: source_{move(source)}
: source_{std::move(source)}
{
const auto wsize = (format == Format::GZIP) ? (MAX_WBITS | 16) : MAX_WBITS;

Expand Down Expand Up @@ -145,13 +145,13 @@ class ZipReaderImpl : public DataReader {

std::unique_ptr<DataReader>
DataReader::CreateZipReader(std::unique_ptr<DataReader>&& source) {
return make_unique<ZipReaderImpl>(move(source),
return make_unique<ZipReaderImpl>(std::move(source),
ZipReaderImpl::Format::DEFLATE);
}

std::unique_ptr<DataReader>
DataReader::CreateGzipReader(std::unique_ptr<DataReader>&& source) {
return make_unique<ZipReaderImpl>(move(source),
return make_unique<ZipReaderImpl>(std::move(source),
ZipReaderImpl::Format::GZIP);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/JsonSerializeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ struct Group {
std::list<Person> more_members_ = {},
std::deque<Person> even_more_members_ = {})
: name{std::move(name_)}, gid{gid_}, leader{std::move(leader_)}
, members{move(members_)}, more_members{move(more_members_)}
, even_more_members{move(even_more_members_)}
, members{std::move(members_)}, more_members{std::move(more_members_)}
, even_more_members{std::move(even_more_members_)}
{}

Group() = default;
Expand Down

0 comments on commit 1cacbfe

Please sign in to comment.