Skip to content

Commit

Permalink
Address cert-err33-c warning
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed Jan 15, 2024
1 parent d3c23b5 commit 91a0d7e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Checks: >
-bugprone-unchecked-optional-access,
-bugprone-unused-raii,
cert-*,
-cert-err33-c,
-cert-err58-cpp,
-cert-oop54-cpp,
-clang-analyzer-*,
Expand Down
4 changes: 2 additions & 2 deletions silkworm/capi/silkworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ static std::filesystem::path make_path(const char data_dir_path[SILKWORM_PATH_SI
static log::Args log_args_for_exec_progress(ExecutionProgress& progress, uint64_t current_block) {
static auto float_to_string = [](float f) -> std::string {
const auto size = std::snprintf(nullptr, 0, "%.1f", static_cast<double>(f));
std::string s(static_cast<size_t>(size + 1), '\0'); // +1 for null terminator
std::snprintf(s.data(), s.size(), "%.1f", static_cast<double>(f)); // certain to fit
std::string s(static_cast<size_t>(size + 1), '\0'); // +1 for null terminator
(void)std::snprintf(s.data(), s.size(), "%.1f", static_cast<double>(f)); // certain to fit
return s;
};

Expand Down
4 changes: 3 additions & 1 deletion silkworm/core/common/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <cstdio>
#include <regex>

#include <silkworm/core/common/assert.hpp>

namespace silkworm {

// ASCII -> hex value (0xff means bad [hex] char)
Expand Down Expand Up @@ -211,7 +213,7 @@ std::string human_size(uint64_t bytes) {
}
static constexpr size_t kBufferSize{64};
SILKWORM_THREAD_LOCAL char output[kBufferSize];
std::snprintf(output, kBufferSize, "%.02lf %s", value, suffix[index]);
SILKWORM_ASSERT(std::snprintf(output, kBufferSize, "%.02lf %s", value, suffix[index]) > 0);
return output;
}

Expand Down
2 changes: 1 addition & 1 deletion silkworm/infra/common/mem_usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ size_t get_mem_usage(bool resident) {
vm_size = vm * static_cast<size_t>(getpagesize());
rm_size = rm * static_cast<size_t>(getpagesize());
}
fclose(file);
(void)fclose(file);
}
return (resident ? rm_size : vm_size);

Expand Down
18 changes: 11 additions & 7 deletions silkworm/infra/concurrency/signal_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,29 @@ void SignalHandler::handle(int sig_code) {
if (signalled_.compare_exchange_strong(expected, true)) {
sig_code_ = sig_code;
if (!silent_) {
std::fputs("\nGot ", stderr);
std::fputs(sig_name(sig_code), stderr);
std::fputs(". Shutting down ...\n", stderr);
(void)std::fputs("\nGot ", stderr);
(void)std::fputs(sig_name(sig_code), stderr);
(void)std::fputs(". Shutting down ...\n", stderr);
}
}
uint32_t sig_count = ++sig_count_;
if (sig_count >= 10) {
std::abort();
}
if (sig_count > 1 && !silent_) {
std::fputs("Already shutting down. Interrupt more to panic. ", stderr);
(void)std::fputs("Already shutting down. Interrupt more to panic. ", stderr);
char digit_with_endl[3];
digit_with_endl[0] = static_cast<char>('0' + (10 - sig_count));
digit_with_endl[1] = '\n';
digit_with_endl[2] = '\0';
std::fputs(digit_with_endl, stderr);
(void)std::fputs(digit_with_endl, stderr);
}
if (custom_handler_) {
custom_handler_(sig_code);
}
signal(sig_code, &SignalHandler::handle); // Re-enable the hook
if (signal(sig_code, &SignalHandler::handle) == SIG_ERR) { // Re-enable the hook
(void)std::fputs("Failed to re-enable signal hook :(", stderr);
}
}

void SignalHandler::reset() {
Expand All @@ -154,7 +156,9 @@ void SignalHandler::reset() {
// Restore any previous signal handlers
for (const int sig_code : kHandleableCodes) {
if (previous_signal_handlers.contains(sig_code)) {
signal(sig_code, previous_signal_handlers[sig_code]);
if (signal(sig_code, previous_signal_handlers[sig_code]) == SIG_ERR) {
(void)std::fputs("Failed to restore previous signal handlers :(", stderr);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion silkworm/infra/concurrency/signal_handler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace silkworm {
#ifndef __APPLE__
TEST_CASE("Signal Handler") {
SignalHandler::init();
std::raise(SIGINT);
CHECK(std::raise(SIGINT) == 0);
CHECK(SignalHandler::signalled());
SignalHandler::reset();
CHECK(SignalHandler::signalled() == false);
Expand Down
5 changes: 1 addition & 4 deletions silkworm/node/recsplit/rec_split.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,7 @@ class RecSplit {
friend std::istream& operator>>(std::istream& is, RecSplit<LEAF_SIZE>& rs) {
size_t leaf_size{0};
is.read(reinterpret_cast<char*>(&leaf_size), sizeof(leaf_size));
if (leaf_size != LEAF_SIZE) {
fprintf(stderr, "Serialized leaf size %d, code leaf size %d\n", int(leaf_size), int(LEAF_SIZE));
abort();
}
SILKWORM_ASSERT(leaf_size == LEAF_SIZE);
is.read(reinterpret_cast<char*>(&rs.bucket_size_), sizeof(rs.bucket_size_));
is.read(reinterpret_cast<char*>(&rs.key_count_), sizeof(rs.key_count_));
rs.bucket_count_ = std::max(std::size_t{1}, (rs.key_count_ + rs.bucket_size_ - 1) / rs.bucket_size_);
Expand Down

0 comments on commit 91a0d7e

Please sign in to comment.