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 920c8e2 commit 5830d8d
Show file tree
Hide file tree
Showing 31 changed files with 612 additions and 637 deletions.
21 changes: 7 additions & 14 deletions examples/logip/logip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ BOOST_FUSION_ADAPT_STRUCT(

string now() {
char date[32] = {};
auto now = time(NULL);
auto now = time(nullptr);
strftime(date, sizeof(date), "%Y-%m-%d %H:%M", localtime(&now));
return date;
}

int main(int argc, char *argv[]) {
int main(int /*argc*/, char * /*argv*/[])
{
#ifdef RESTC_CPP_LOG_WITH_BOOST_LOG
namespace logging = boost::log;
logging::core::get()->set_filter
Expand All @@ -71,22 +72,14 @@ int main(int argc, char *argv[]) {
.Execute());
valid = true;
} catch (const boost::exception& ex) {
clog << now()
<< "Caught boost exception: "
<< boost::diagnostic_information(ex)
<< endl;
clog << now() << "Caught boost exception: " << boost::diagnostic_information(ex)
<< '\n';
} catch (const exception& ex) {
clog << now()
<< "Caught exception: "
<< ex.what()
<< endl;
clog << now() << "Caught exception: " << ex.what() << '\n';
}

if (valid && (current_ip != data.ip)) {
clog << now()
<< ' '
<< data.ip
<< endl;
clog << now() << ' ' << data.ip << '\n';
current_ip = data.ip;
}

Expand Down
2 changes: 1 addition & 1 deletion include/restc-cpp/restc-cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Request {
Type type = Type::NONE;
std::string address;

const std::string& GetName();
const std::string &GetName() const;
};

using args_t = std::deque<Arg>;
Expand Down
25 changes: 13 additions & 12 deletions src/ChunkedReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class ChunkedReaderImpl : public DataReader {
{
}

bool IsEof() const override {
return stream_->IsEof();
}
[[nodiscard]] bool IsEof() const override { return stream_->IsEof(); }

void Finish() override {
ReadSome();
Expand All @@ -36,15 +34,16 @@ class ChunkedReaderImpl : public DataReader {
}
}

string ToPrintable(boost::string_ref buf) const {
[[nodiscard]] static string ToPrintable(boost::string_ref buf)
{
ostringstream out;
locale loc;
locale const loc;
auto pos = 0;
out << endl;
out << '\n';

for(const auto ch : buf) {
if (!(++pos % line_length)) {
out << endl;
if ((++pos % line_length) == 0u) {
out << '\n';
}
if (std::isprint(ch, loc)) {
out << ch;
Expand All @@ -56,7 +55,8 @@ class ChunkedReaderImpl : public DataReader {
return out.str();
}

void Log(const boost::asio::const_buffers_1 buffers, const char *tag) {
static void Log(const boost::asio::const_buffers_1 buffers, const char * /*tag*/)
{
const auto buf_len = boost::asio::buffer_size(*buffers.begin());

// At the time of the implementation, there are never multiple buffers.
Expand Down Expand Up @@ -132,11 +132,11 @@ class ChunkedReaderImpl : public DataReader {
size_t chunk_len = 0;
char ch = stream_->Getc();

if (!isxdigit(ch)) {
if (isxdigit(ch) == 0) {
throw ParseException("Missing chunk-length in new chunk.");
}

for(; isxdigit(ch); ch = stream_->Getc()) {
for (; isxdigit(ch) != 0; ch = stream_->Getc()) {
chunk_len *= magic_16;
if (ch >= 'a') {
chunk_len += magic_10 + (ch - 'a');
Expand All @@ -147,8 +147,9 @@ class ChunkedReaderImpl : public DataReader {
}
}

for(; ch != '\r'; ch = stream_->Getc())
for (; ch != '\r'; ch = stream_->Getc()) {
;
}

if (ch != '\r') {
throw ParseException("Missing CR in first chunk line");
Expand Down
7 changes: 3 additions & 4 deletions src/ChunkedWriterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace restc_cpp {
class ChunkedWriterImpl : public DataWriter {
public:
ChunkedWriterImpl(add_header_fn_t fn, ptr_t&& source)
: next_{move(source)}, add_header_fn_{move(fn)}
: next_{std::move(source)}, add_header_fn_{std::move(fn)}
{
}

Expand All @@ -34,8 +34,7 @@ class ChunkedWriterImpl : public DataWriter {
void Write(const write_buffers_t& buffers) override {
const auto len = boost::asio::buffer_size(buffers);
buffers_.resize(1);

std::copy(buffers.begin(), buffers.end(), std::back_insert_iterator{buffers_});
std::copy(buffers.begin(), buffers.end(), std::back_inserter(buffers_));
DoWrite(len);
}

Expand Down Expand Up @@ -100,7 +99,7 @@ class ChunkedWriterImpl : public DataWriter {

DataWriter::ptr_t
DataWriter::CreateChunkedWriter(add_header_fn_t fn, ptr_t&& source) {
return make_unique<ChunkedWriterImpl>(move(fn), move(source));
return make_unique<ChunkedWriterImpl>(std::move(fn), std::move(source));
}

} // namespace
Expand Down
19 changes: 12 additions & 7 deletions src/ConnectionPoolImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,13 @@ class ConnectionPoolImpl
return entry_->GetConnection()->GetSocket();
}

const Socket& GetSocket() const override {
return entry_->GetConnection()->GetSocket();
[[nodiscard]] const Socket &GetSocket() const override
{
return entry_->GetConnection()->GetSocket();
}

boost::uuids::uuid GetId() const override {
[[nodiscard]] boost::uuids::uuid GetId() const override
{
return entry_->GetConnection()->GetId();
}

Expand Down Expand Up @@ -207,11 +209,13 @@ class ConnectionPoolImpl
LOCK_ALWAYS_;
cache_cleanup_timer_.expires_from_now(
boost::posix_time::seconds(properties_->cacheCleanupIntervalSeconds));
cache_cleanup_timer_.async_wait(std::bind(&ConnectionPoolImpl::OnCacheCleanup,
shared_from_this(), std::placeholders::_1));
cache_cleanup_timer_.async_wait([capture0 = shared_from_this()](auto &&PH1) {
capture0->OnCacheCleanup(std::forward<decltype(PH1)>(PH1));
});
}

void OnCacheCleanup(const boost::system::error_code& error) {
void OnCacheCleanup(const boost::system::error_code &error)
{
RESTC_CPP_LOG_TRACE_("OnCacheCleanup: enter");
if (closed_) {
RESTC_CPP_LOG_TRACE_("OnCacheCleanup: closed");
Expand Down Expand Up @@ -252,7 +256,8 @@ class ConnectionPoolImpl
RESTC_CPP_LOG_TRACE_("OnCacheCleanup: leave");
}

void OnRelease(const Entry::ptr_t entry) {
void OnRelease(const Entry::ptr_t &entry)
{
{
LOCK_ALWAYS_;
in_use_.erase(entry->GetKey());
Expand Down
7 changes: 4 additions & 3 deletions src/DataReaderStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void DataReaderStream::ReadHeaderLines(const add_header_fn_t& addHeader) {
constexpr size_t max_headers = 256;

while(true) {
char ch;
char ch = 0;
string name;
string value;
for(ch = Getc(); ch != '\r'; ch = Getc()) {
Expand Down Expand Up @@ -199,11 +199,12 @@ void DataReaderStream::ReadHeaderLines(const add_header_fn_t& addHeader) {
std::string DataReaderStream::GetHeaderValue() {
constexpr size_t max_header_value_len = 1024 * 4;
std::string value;
char ch;
char ch = 0;

while(true) {
for (ch = Getc(); ch == ' ' || ch == '\t'; ch = Getc())
for (ch = Getc(); ch == ' ' || ch == '\t'; ch = Getc()) {
; // skip space
}

for (; ch != '\r'; ch = Getc()) {
value += ch;
Expand Down
5 changes: 3 additions & 2 deletions src/IoReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IoReaderImpl : public DataReader {
for(size_t retries = 0;; ++retries) {
size_t bytes = 0;
try {
if (retries) {
if (retries != 0u) {
RESTC_CPP_LOG_DEBUG_("IoReaderImpl::ReadSome: taking a nap");
ctx_.Sleep(retries * 20ms);
RESTC_CPP_LOG_DEBUG_("IoReaderImpl::ReadSome: Waking up. Will try to read from the socket now.");
Expand Down Expand Up @@ -68,7 +68,8 @@ class IoReaderImpl : public DataReader {
throw ObjectExpiredException("Connection expired");
}

bool IsEof() const override {
[[nodiscard]] bool IsEof() const override
{
if (auto conn = connection_.lock()) {
return !conn->GetSocket().IsOpen();
}
Expand Down
4 changes: 1 addition & 3 deletions src/IoWriterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class IoWriterImpl : public DataWriter {
;
}

void SetHeaders(Request::headers_t& ) override {
;
}
void SetHeaders(Request::headers_t & /*headers*/) override { ; }

private:
Context& ctx_;
Expand Down
4 changes: 1 addition & 3 deletions src/NoBodyReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ class NoBodyReaderImpl : public DataReader {
public:
NoBodyReaderImpl() = default;

bool IsEof() const override {
return true;
}
[[nodiscard]] bool IsEof() const override { return true; }

void Finish() override {
}
Expand Down
7 changes: 3 additions & 4 deletions src/PlainReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ class PlainReaderImpl : public DataReader {
: remaining_{contentLength},
source_{move(source)} {}

Check warning on line 16 in src/PlainReaderImpl.cpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, clang)

unqualified call to 'std::move' [-Wunqualified-std-cast-call]

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

void Finish() override {
if (source_)
if (source_) {
source_->Finish();
}
}

boost::asio::const_buffers_1 ReadSome() override {
Expand Down
2 changes: 1 addition & 1 deletion src/ReplyImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void ReplyImpl::HandleDecompression() {
return;
}

boost::tokenizer<> tok(*te_hdr);
boost::tokenizer<> const tok(*te_hdr);
for(auto it = tok.begin(); it != tok.end(); ++it) {
#ifdef RESTC_CPP_WITH_ZLIB
if (ciEqLibC()(gzip, *it)) {
Expand Down
10 changes: 3 additions & 7 deletions src/RequestBodyFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ class RequestBodyFileImpl : public RequestBody
file_ = make_unique<ifstream>(path_.string(), ios::binary);
}

Type GetType() const noexcept override {
return Type::FIXED_SIZE;
}
[[nodiscard]] Type GetType() const noexcept override { return Type::FIXED_SIZE; }

uint64_t GetFixedSize() const override {
return size_;
}
[[nodiscard]] uint64_t GetFixedSize() const override { return size_; }

bool GetData(write_buffers_t & buffers) override {
const auto bytes_left = size_ - bytes_read_;
Expand All @@ -54,7 +50,7 @@ class RequestBodyFileImpl : public RequestBody
}

bytes_read_ += read_this_time;
buffers.push_back({buffer_.data(), read_this_time});
buffers.emplace_back(buffer_.data(), read_this_time);
return true;
}

Expand Down
15 changes: 4 additions & 11 deletions src/RequestBodyStringImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ class RequestBodyStringImpl : public RequestBody
{
}

Type GetType() const noexcept override {
return Type::FIXED_SIZE;
}
[[nodiscard]] Type GetType() const noexcept override { return Type::FIXED_SIZE; }

std::uint64_t GetFixedSize() const override {
return body_.size();
}
[[nodiscard]] std::uint64_t GetFixedSize() const override { return body_.size(); }

bool GetData(write_buffers_t & buffers) override {
if (eof_) {
return false;
}

buffers.push_back({body_.c_str(), body_.size()});
buffers.emplace_back(body_.c_str(), body_.size());
eof_ = true;
return true;
}
Expand All @@ -42,10 +38,7 @@ class RequestBodyStringImpl : public RequestBody
eof_ = false;
}

std::string GetCopyOfData() const override {
return body_;
}

[[nodiscard]] std::string GetCopyOfData() const override { return body_; }

private:
string body_;
Expand Down
Loading

0 comments on commit 5830d8d

Please sign in to comment.