]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
chore: Fix some Cppcheck warnings
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 26 May 2024 14:46:12 +0000 (16:46 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 27 May 2024 19:51:57 +0000 (21:51 +0200)
13 files changed:
misc/cppcheck-suppressions.txt
src/ccache/argprocessing.hpp
src/ccache/ccache.cpp
src/ccache/hashutil.cpp
src/ccache/storage/local/LocalStorage.cpp
src/ccache/storage/local/LocalStorage.hpp
src/ccache/util/Bytes.cpp
src/ccache/util/DirEntry.cpp
src/ccache/util/MemoryMap.cpp
src/ccache/util/logging.cpp
src/ccache/util/string.cpp
unittest/test_util_Bytes.cpp
unittest/test_util_file.cpp

index f5e988333e7f51fd90d0ec70d7d8e87f9ef39ac6..452a10469767a6a11d5b3d48b35ba27eb727f5cb 100644 (file)
@@ -1,5 +1,5 @@
-ConfigurationNotChecked
-missingIncludeSystem
-
 // Ignore third party code.
-*:src/third_party
+*:*/third_party/*
+
+// Not interesting enough to enforce.
+noExplicitConstructor
index 3c2a07a46bda286219836519233fbacdbc69fe69..94ee9b120548df57bee1a44e363e7f9143b948e5 100644 (file)
@@ -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_)
index a53d55a00f8c9d7939240dc202c08f93782f2bea..c87e2f58a18b0754a098b3c99e2c6832e1bc72d8 100644 (file)
@@ -2314,7 +2314,7 @@ static tl::expected<core::StatisticsCounters, Failure>
 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;
index c5a4d57b9aa848263570c5f4c1fa92f73be3d8c7..7c0d60966a044ff6c0f0245d1d550ce5a7cbfa4c 100644 (file)
@@ -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);
index fc33104229cafce3377e53e277e27dcea7d6c185..a51c9001013a0f565a39d2c3cb281a0e6abf0789 100644 (file)
@@ -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<core::Result::Serializer::RawFile> raw_files)
+  const std::vector<core::Result::Serializer::RawFile>& 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());
index 313f8acd069527e7eaa997fb9c4eaf3ab7f8f67c..cda466e5fe7d412d1b0350234a143209795f5bf5 100644 (file)
@@ -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<core::Result::Serializer::RawFile> raw_files);
+  void put_raw_files(
+    const Hash::Digest& key,
+    const std::vector<core::Result::Serializer::RawFile>& 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
index edd0a72c552f6e19831160f5ad3dd69f0b867eee..aedf9ce1b574e838b45f6af9fec26b4cce1856ed 100644 (file)
@@ -76,11 +76,11 @@ void
 Bytes::reserve(size_t size) noexcept
 {
   if (size > m_capacity) {
-    auto data = std::make_unique<uint8_t[]>(size);
+    auto bytes = std::make_unique<uint8_t[]>(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;
   }
 }
index 1877a069d4a6935ecf259be60c29b7a7edfecff0..967624b9cabffb02f1b1ea27982a29b27c4ce775 100644 (file)
@@ -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;
         }
index 35e29e1032c874b5e8395b117961a6208d2f2f36..439bb0e38cb3e42e59fde303e4a50c04a65ea77c 100644 (file)
@@ -89,13 +89,13 @@ MemoryMap::map(int fd, size_t size)
 #ifndef _WIN32
   const void* MMAP_FAILED =
     reinterpret_cast<void*>(-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
index 75f2d57477dfe8f3095290fd3a20b45cf24a4a73..717c19137ffffd5d5aaf5acd80c447e8a0ef394b 100644 (file)
@@ -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<unsigned int>(now.nsec_decimal_part() / 1000),
              static_cast<int>(getpid()));
index e45b32890822dfaedf7806218e0cb19bd674afb1..111dac840cf0c135e3b98bbff83bf9305729c56b 100644 (file)
@@ -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;
index e1eefb77c31fe13b441cf4d557771ea996efe5ed..5deb7ac06e2d9a67aaa277ae9ec0642cf3e9b91b 100644 (file)
@@ -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);
index d5150402d8decf885484b99ece2fd12ea059e11b..9f1c1a0313121effffc93d8abe26d67f95c313c4 100644 (file)
@@ -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);
 }