From: Joel Rosdahl Date: Sun, 26 May 2024 14:46:12 +0000 (+0200) Subject: chore: Fix some Cppcheck warnings X-Git-Tag: v4.10~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5717e48b649f2d0ba36753efcb6f3d5dc9d6e2e4;p=thirdparty%2Fccache.git chore: Fix some Cppcheck warnings --- diff --git a/misc/cppcheck-suppressions.txt b/misc/cppcheck-suppressions.txt index f5e98833..452a1046 100644 --- a/misc/cppcheck-suppressions.txt +++ b/misc/cppcheck-suppressions.txt @@ -1,5 +1,5 @@ -ConfigurationNotChecked -missingIncludeSystem - // Ignore third party code. -*:src/third_party +*:*/third_party/* + +// Not interesting enough to enforce. +noExplicitConstructor diff --git a/src/ccache/argprocessing.hpp b/src/ccache/argprocessing.hpp index 3c2a07a4..94ee9b12 100644 --- a/src/ccache/argprocessing.hpp +++ b/src/ccache/argprocessing.hpp @@ -50,7 +50,7 @@ struct ProcessArgsResult Args compiler_args; // Whether to include the actual CWD in the hash. - bool hash_actual_cwd; + bool hash_actual_cwd = false; }; inline ProcessArgsResult::ProcessArgsResult(core::Statistic error_) diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index a53d55a0..c87e2f58 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -2314,7 +2314,7 @@ static tl::expected do_cache_compilation(Context& ctx); static void -log_result_to_debug_log(Context& ctx) +log_result_to_debug_log(const Context& ctx) { if (ctx.config.log_file().empty() && !ctx.config.debug()) { return; diff --git a/src/ccache/hashutil.cpp b/src/ccache/hashutil.cpp index c5a4d57b..7c0d6096 100644 --- a/src/ccache/hashutil.cpp +++ b/src/ccache/hashutil.cpp @@ -304,9 +304,10 @@ hash_source_code_file(const Context& ctx, hash.hash_delimiter("timestamp"); #ifdef HAVE_ASCTIME_R char buffer[26]; - auto timestamp = asctime_r(&*modified_time, buffer); + const char* timestamp = asctime_r(&*modified_time, buffer); #else - auto timestamp = asctime(&*modified_time); + // cppcheck-suppress asctimeCalled; thread-safety not needed here + const char* timestamp = asctime(&*modified_time); #endif if (!timestamp) { result.insert(HashSourceCode::error); diff --git a/src/ccache/storage/local/LocalStorage.cpp b/src/ccache/storage/local/LocalStorage.cpp index fc331042..a51c9001 100644 --- a/src/ccache/storage/local/LocalStorage.cpp +++ b/src/ccache/storage/local/LocalStorage.cpp @@ -640,7 +640,7 @@ LocalStorage::get_raw_file_path(const Hash::Digest& result_key, void LocalStorage::put_raw_files( const Hash::Digest& key, - const std::vector raw_files) + const std::vector& raw_files) { const auto cache_file = look_up_cache_file(key, core::CacheEntryType::result); core::ensure_dir_exists(fs::path(cache_file.path).parent_path()); diff --git a/src/ccache/storage/local/LocalStorage.hpp b/src/ccache/storage/local/LocalStorage.hpp index 313f8acd..cda466e5 100644 --- a/src/ccache/storage/local/LocalStorage.hpp +++ b/src/ccache/storage/local/LocalStorage.hpp @@ -85,9 +85,9 @@ public: std::string get_raw_file_path(const Hash::Digest& result_key, uint8_t file_number) const; - void - put_raw_files(const Hash::Digest& key, - const std::vector raw_files); + void put_raw_files( + const Hash::Digest& key, + const std::vector& raw_files); // Clone, hard link or copy a file from `source` to `dest` depending on // settings in `ctx`. If cloning or hard linking cannot and should not be done diff --git a/src/ccache/util/Bytes.cpp b/src/ccache/util/Bytes.cpp index edd0a72c..aedf9ce1 100644 --- a/src/ccache/util/Bytes.cpp +++ b/src/ccache/util/Bytes.cpp @@ -76,11 +76,11 @@ void Bytes::reserve(size_t size) noexcept { if (size > m_capacity) { - auto data = std::make_unique(size); + auto bytes = std::make_unique(size); if (m_size > 0) { - std::memcpy(data.get(), m_data.get(), m_size); + std::memcpy(bytes.get(), m_data.get(), m_size); } - m_data = std::move(data); + m_data = std::move(bytes); m_capacity = size; } } diff --git a/src/ccache/util/DirEntry.cpp b/src/ccache/util/DirEntry.cpp index 1877a069..967624b9 100644 --- a/src/ccache/util/DirEntry.cpp +++ b/src/ccache/util/DirEntry.cpp @@ -268,8 +268,8 @@ DirEntry::do_stat() const } else #endif { - auto path = pstr(m_path); - result = lstat_func(path, &m_stat); + auto mpath = pstr(m_path); + result = lstat_func(mpath, &m_stat); if (result == 0) { if (S_ISLNK(m_stat.st_mode) #ifdef _WIN32 @@ -278,7 +278,7 @@ DirEntry::do_stat() const ) { m_is_symlink = true; stat_t st; - if (stat_func(path, &st) == 0) { + if (stat_func(mpath, &st) == 0) { m_stat = st; m_exists = true; } diff --git a/src/ccache/util/MemoryMap.cpp b/src/ccache/util/MemoryMap.cpp index 35e29e10..439bb0e3 100644 --- a/src/ccache/util/MemoryMap.cpp +++ b/src/ccache/util/MemoryMap.cpp @@ -89,13 +89,13 @@ MemoryMap::map(int fd, size_t size) #ifndef _WIN32 const void* MMAP_FAILED = reinterpret_cast(-1); // NOLINT: Must cast here - void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (ptr == MMAP_FAILED) { + void* p = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (p == MMAP_FAILED) { return tl::unexpected(strerror(errno)); } MemoryMap map; - map.m_ptr = ptr; + map.m_ptr = p; map.m_size = size; return map; #else @@ -115,16 +115,15 @@ MemoryMap::map(int fd, size_t size) return tl::unexpected(FMT("Can't create file mapping: {}", GetLastError())); } - void* ptr = - MapViewOfFile(file_mapping_handle, FILE_MAP_ALL_ACCESS, 0, 0, size); - if (!ptr) { + void* p = MapViewOfFile(file_mapping_handle, FILE_MAP_ALL_ACCESS, 0, 0, size); + if (!p) { std::string error = FMT("Can't map file: {}", GetLastError()); CloseHandle(file_mapping_handle); return tl::unexpected(std::move(error)); } MemoryMap map; - map.m_ptr = ptr; + map.m_ptr = p; map.m_file_mapping_handle = file_mapping_handle; return map; #endif diff --git a/src/ccache/util/logging.cpp b/src/ccache/util/logging.cpp index 75f2d574..717c1913 100644 --- a/src/ccache/util/logging.cpp +++ b/src/ccache/util/logging.cpp @@ -89,7 +89,7 @@ do_log(std::string_view message, bool bulk) } snprintf(prefix, sizeof(prefix), - "[%s.%06d %-5d] ", + "[%s.%06u %-5d] ", timestamp, static_cast(now.nsec_decimal_part() / 1000), static_cast(getpid())); diff --git a/src/ccache/util/string.cpp b/src/ccache/util/string.cpp index e45b3289..111dac84 100644 --- a/src/ccache/util/string.cpp +++ b/src/ccache/util/string.cpp @@ -396,9 +396,8 @@ replace_all(const std::string_view string, std::string result; size_t left = 0; - size_t right = 0; while (left < string.size()) { - right = string.find(from, left); + size_t right = string.find(from, left); if (right == std::string_view::npos) { result.append(string.data() + left); break; diff --git a/unittest/test_util_Bytes.cpp b/unittest/test_util_Bytes.cpp index e1eefb77..5deb7ac0 100644 --- a/unittest/test_util_Bytes.cpp +++ b/unittest/test_util_Bytes.cpp @@ -85,7 +85,9 @@ TEST_CASE("Basics") SUBCASE("Move construction") { - const auto bytes1_orig_data = bytes1.data(); + // cppcheck-suppress constVariablePointer; we're intentionally keeping a + // copy of the pointer + const uint8_t* bytes1_orig_data = bytes1.data(); Bytes bytes2(std::move(bytes1)); CHECK(bytes1.data() == nullptr); @@ -120,7 +122,9 @@ TEST_CASE("Basics") SUBCASE("Move assignment") { - const auto bytes1_orig_data = bytes1.data(); + // cppcheck-suppress constVariablePointer; we're intentionally keeping a + // copy of the pointer + const uint8_t* bytes1_orig_data = bytes1.data(); Bytes bytes2; bytes2 = std::move(bytes1); @@ -144,7 +148,7 @@ TEST_CASE("Basics") SUBCASE("Non-const operator[]") { bytes1[1] = 'x'; - CHECK(bytes1[1] == 'x'); + CHECK(bytes1[1] == 'x'); // cppcheck-suppress knownConditionTrueFalse } SUBCASE("Comparison") @@ -197,7 +201,9 @@ TEST_CASE("Basics") SUBCASE("Reserve and capacity") { - const auto bytes1_orig_data = bytes1.data(); + // cppcheck-suppress constVariablePointer; we're intentionally keeping a + // copy of the pointer + const uint8_t* bytes1_orig_data = bytes1.data(); CHECK(bytes1.size() == 3); CHECK(bytes1.capacity() == 3); @@ -214,7 +220,9 @@ TEST_CASE("Basics") SUBCASE("Increase size") { - const auto bytes1_orig_data = bytes1.data(); + // cppcheck-suppress constVariablePointer; we're intentionally keeping a + // copy of the pointer + const uint8_t* bytes1_orig_data = bytes1.data(); bytes1.resize(4); CHECK(bytes1.data() != bytes1_orig_data); CHECK(bytes1.size() == 4); @@ -226,7 +234,9 @@ TEST_CASE("Basics") SUBCASE("Decrease size") { - const auto bytes1_orig_data = bytes1.data(); + // cppcheck-suppress constVariablePointer; we're intentionally keeping a + // copy of the pointer + const uint8_t* bytes1_orig_data = bytes1.data(); bytes1.resize(2); CHECK(bytes1.data() == bytes1_orig_data); CHECK(bytes1.size() == 2); diff --git a/unittest/test_util_file.cpp b/unittest/test_util_file.cpp index d5150402..9f1c1a03 100644 --- a/unittest/test_util_file.cpp +++ b/unittest/test_util_file.cpp @@ -65,10 +65,15 @@ TEST_CASE("util::fallocate") TEST_CASE("util::likely_size_on_disk") { + // cppcheck-suppress knownConditionTrueFalse CHECK(util::likely_size_on_disk(0) == 0); + // cppcheck-suppress knownConditionTrueFalse CHECK(util::likely_size_on_disk(1) == 4096); + // cppcheck-suppress knownConditionTrueFalse CHECK(util::likely_size_on_disk(4095) == 4096); + // cppcheck-suppress knownConditionTrueFalse CHECK(util::likely_size_on_disk(4096) == 4096); + // cppcheck-suppress knownConditionTrueFalse CHECK(util::likely_size_on_disk(4097) == 8192); }