]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
chore: Fix some -Wconversion warnings
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 4 Dec 2023 19:15:15 +0000 (20:15 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 10 Dec 2023 20:01:46 +0000 (21:01 +0100)
21 files changed:
src/Config.cpp
src/ProgressBar.cpp
src/argprocessing.cpp
src/core/CacheEntry.cpp
src/core/Manifest.cpp
src/core/Manifest.hpp
src/core/Result.cpp
src/core/Statistics.cpp
src/core/mainoptions.cpp
src/storage/local/LocalStorage.cpp
src/storage/remote/RedisStorage.cpp
src/util/Duration.hpp
src/util/LockFile.cpp
src/util/TemporaryFile.cpp
src/util/TimePoint.hpp
src/util/conversion.hpp
src/util/string.cpp
src/util/zstd.cpp
unittest/test_util_Tokenizer.cpp
unittest/test_util_file.cpp
unittest/test_util_string.cpp

index 042e856fbe76fdde8bb4c74295ae39c83cf26721..71bdc354cb49b9f638cee31fce7087d7076e329a 100644 (file)
@@ -1013,8 +1013,8 @@ Config::set_item(const std::string& key,
     break;
 
   case ConfigItem::compression_level:
-    m_compression_level = util::value_or_throw<core::Error>(
-      util::parse_signed(value, INT8_MIN, INT8_MAX, "compression_level"));
+    m_compression_level = static_cast<int8_t>(util::value_or_throw<core::Error>(
+      util::parse_signed(value, INT8_MIN, INT8_MAX, "compression_level")));
     break;
 
   case ConfigItem::cpp_extension:
@@ -1030,8 +1030,8 @@ Config::set_item(const std::string& key,
     break;
 
   case ConfigItem::debug_level:
-    m_debug_level = util::value_or_throw<core::Error>(
-      util::parse_unsigned(value, std::nullopt, std::nullopt, "debug level"));
+    m_debug_level = static_cast<uint8_t>(util::value_or_throw<core::Error>(
+      util::parse_unsigned(value, 0, UINT8_MAX, "debug level")));
     break;
 
   case ConfigItem::depend_mode:
index c616d6d94f8d41792483ab5b91f6e58fa2b4c2e8..dc55dd4e0b1998188068b5431544cfb3449c62ea 100644 (file)
@@ -90,7 +90,8 @@ ProgressBar::update(double value)
     PRINT(stdout, "\r{} {:5.1f}%", m_header, new_value_percent);
   } else {
     size_t total_bar_width = m_width - first_part_width;
-    size_t filled_bar_width = (new_value_percent / 100) * total_bar_width;
+    size_t filled_bar_width = static_cast<size_t>(
+      (new_value_percent / 100) * static_cast<double>(total_bar_width));
     size_t unfilled_bar_width = total_bar_width - filled_bar_width;
     PRINT(stdout,
           "\r{} {:5.1f}% [{:=<{}}{: <{}}]",
index 874d41050d19c71236110cd19cb130d8f042a56a..216cdc3e55768d01b9a2f2e6cc8613f6312066a1 100644 (file)
@@ -333,12 +333,12 @@ process_option_arg(const Context& ctx,
     return std::nullopt;
   }
 
-  // Ignore clang -ivfsoverlay <arg> to not detect multiple input files.
   if (arg == "-ivfsoverlay"
       && !(config.sloppiness().contains(core::Sloppy::ivfsoverlay))) {
     LOG_RAW(
       "You have to specify \"ivfsoverlay\" sloppiness when using"
       " -ivfsoverlay to get hits");
+    ++i;
     return Statistic::unsupported_compiler_option;
   }
 
index d1ab2b0f0c03b302cfd9be4399655b32da98f13b..9ae0de3052876088706feb58d9120ab053085c5e 100644 (file)
@@ -172,9 +172,9 @@ CacheEntry::Header::serialize(util::Bytes& output) const
   writer.write_int(compression_level);
   writer.write_int<uint8_t>(self_contained);
   writer.write_int(creation_time);
-  writer.write_int<uint8_t>(ccache_version.length());
+  writer.write_int(static_cast<uint8_t>(ccache_version.length()));
   writer.write_str(ccache_version);
-  writer.write_int<uint8_t>(namespace_.length());
+  writer.write_int(static_cast<uint8_t>(namespace_.length()));
   writer.write_str(namespace_);
   writer.write_int(entry_size);
 }
@@ -182,7 +182,8 @@ CacheEntry::Header::serialize(util::Bytes& output) const
 uint32_t
 CacheEntry::Header::uncompressed_payload_size() const
 {
-  return entry_size - serialized_size() - k_epilogue_fields_size;
+  return static_cast<uint32_t>(entry_size - serialized_size()
+                               - k_epilogue_fields_size);
 }
 
 CacheEntry::CacheEntry(nonstd::span<const uint8_t> data) : m_header(data)
index 70c1f47462eb5a2e7ad10d134866c12c22867c7a..d542239306fc9b7eeecdbd7b0951d6179da32498 100644 (file)
@@ -212,8 +212,13 @@ Manifest::add_result(
   file_info_indexes.reserve(included_files.size());
 
   for (const auto& [path, digest] : included_files) {
-    file_info_indexes.push_back(get_file_info_index(
-      path, digest, mf_files, mf_file_infos, stat_file_function));
+    auto index = get_file_info_index(
+      path, digest, mf_files, mf_file_infos, stat_file_function);
+    if (!index) {
+      LOG_RAW("Index overflow in manifest");
+      return false;
+    }
+    file_info_indexes.push_back(*index);
   }
 
   ResultEntry entry{std::move(file_info_indexes), result_key};
@@ -252,7 +257,7 @@ Manifest::serialized_size() const
     throw core::Error(
       FMT("Serialized manifest too large ({} > {})", size, max));
   }
-  return size;
+  return static_cast<uint32_t>(size);
 }
 
 void
@@ -261,13 +266,13 @@ Manifest::serialize(util::Bytes& output)
   core::CacheEntryDataWriter writer(output);
 
   writer.write_int(k_format_version);
-  writer.write_int<uint32_t>(m_files.size());
+  writer.write_int(static_cast<uint32_t>(m_files.size()));
   for (const auto& file : m_files) {
-    writer.write_int<uint16_t>(file.length());
+    writer.write_int(static_cast<uint16_t>(file.length()));
     writer.write_str(file);
   }
 
-  writer.write_int<uint32_t>(m_file_infos.size());
+  writer.write_int(static_cast<uint32_t>(m_file_infos.size()));
   for (const auto& file_info : m_file_infos) {
     writer.write_int<uint32_t>(file_info.index);
     writer.write_bytes(file_info.digest);
@@ -276,9 +281,9 @@ Manifest::serialize(util::Bytes& output)
     writer.write_int(file_info.ctime.nsec());
   }
 
-  writer.write_int<uint32_t>(m_results.size());
+  writer.write_int(static_cast<uint32_t>(m_results.size()));
   for (const auto& result : m_results) {
-    writer.write_int<uint32_t>(result.file_info_indexes.size());
+    writer.write_int(static_cast<uint32_t>(result.file_info_indexes.size()));
     for (auto index : result.file_info_indexes) {
       writer.write_int(index);
     }
@@ -307,7 +312,7 @@ Manifest::clear()
   m_results.clear();
 }
 
-uint32_t
+std::optional<uint32_t>
 Manifest::get_file_info_index(
   const std::string& path,
   const Hash::Digest& digest,
@@ -320,9 +325,11 @@ Manifest::get_file_info_index(
   const auto f_it = mf_files.find(path);
   if (f_it != mf_files.end()) {
     fi.index = f_it->second;
+  } else if (m_files.size() > UINT32_MAX) {
+    return std::nullopt;
   } else {
     m_files.push_back(path);
-    fi.index = m_files.size() - 1;
+    fi.index = static_cast<uint32_t>(m_files.size() - 1);
   }
 
   fi.digest = digest;
@@ -335,6 +342,8 @@ Manifest::get_file_info_index(
   const auto fi_it = mf_file_infos.find(fi);
   if (fi_it != mf_file_infos.end()) {
     return fi_it->second;
+  } else if (m_file_infos.size() > UINT32_MAX) {
+    return std::nullopt;
   } else {
     m_file_infos.push_back(fi);
     return m_file_infos.size() - 1;
index c4b1d3b3b7cc0512d6fcd129a20428c650cc863d..9ffa65ea3c842da9a1c8606997625283a14b944b 100644 (file)
@@ -94,7 +94,7 @@ private:
 
   void clear();
 
-  uint32_t get_file_info_index(
+  std::optional<uint32_t> get_file_info_index(
     const std::string& path,
     const Hash::Digest& digest,
     const std::unordered_map<std::string, uint32_t>& mf_files,
index 3f008f8a2275b135921d83abed2fae1a360724c5..5694fdcef3d2b76631f92836222c828112ba02fe 100644 (file)
@@ -268,7 +268,7 @@ Serializer::serialized_size() const
     throw Error(
       FMT("Serialized result too large ({} > {})", m_serialized_size, max));
   }
-  return m_serialized_size;
+  return static_cast<uint32_t>(m_serialized_size);
 }
 
 void
@@ -277,7 +277,7 @@ Serializer::serialize(util::Bytes& output)
   CacheEntryDataWriter writer(output);
 
   writer.write_int(k_format_version);
-  writer.write_int<uint8_t>(m_file_entries.size());
+  writer.write_int(static_cast<uint8_t>(m_file_entries.size()));
 
   uint8_t file_number = 0;
   for (const auto& entry : m_file_entries) {
index cecb7ca6f8a47ba6f7b0a4d84375fe0b259c2621..aa01b18c4034378ce62cc8fae0f469c67f160a61 100644 (file)
@@ -280,11 +280,15 @@ percent(const uint64_t nominator, const uint64_t denominator)
     return "";
   }
 
-  std::string result = FMT("({:5.2f}%)", (100.0 * nominator) / denominator);
+  std::string result = FMT("({:5.2f}%)",
+                           (100.0 * static_cast<double>(nominator))
+                             / static_cast<double>(denominator));
   if (result.length() <= 8) {
     return result;
   } else {
-    return FMT("({:5.1f}%)", (100.0 * nominator) / denominator);
+    return FMT("({:5.1f}%)",
+               (100.0 * static_cast<double>(nominator))
+                 / static_cast<double>(denominator));
   }
 }
 
@@ -453,15 +457,17 @@ Statistics::format_human_readable(const Config& config,
     table.add_heading("Local storage:");
   }
   if (!from_log) {
-    std::vector<C> size_cells{
-      FMT("  Cache size ({}):", size_unit),
-      C(FMT("{:.1f}", static_cast<double>(local_size) / size_divider))
-        .right_align()};
+    std::vector<C> size_cells{FMT("  Cache size ({}):", size_unit),
+                              C(FMT("{:.1f}",
+                                    static_cast<double>(local_size)
+                                      / static_cast<double>(size_divider)))
+                                .right_align()};
     if (config.max_size() != 0) {
       size_cells.emplace_back("/");
-      size_cells.emplace_back(
-        C(FMT("{:.1f}", static_cast<double>(config.max_size()) / size_divider))
-          .right_align());
+      size_cells.emplace_back(C(FMT("{:.1f}",
+                                    static_cast<double>(config.max_size())
+                                      / static_cast<double>(size_divider)))
+                                .right_align());
       size_cells.emplace_back(percent(local_size, config.max_size()));
     }
     table.add_row(size_cells);
index f5ed6cebd5f6241ed7c43edd34c5b6616a77c57a..826ce5235e6f35599c9b1d857874107913b1ead8 100644 (file)
@@ -235,7 +235,8 @@ print_compression_statistics(const Config& config,
                              const storage::local::CompressionStatistics& cs)
 {
   const double ratio = cs.actual_size > 0
-                         ? static_cast<double>(cs.content_size) / cs.actual_size
+                         ? static_cast<double>(cs.content_size)
+                             / static_cast<double>(cs.actual_size)
                          : 0.0;
   const double savings = ratio > 0.0 ? 100.0 - (100.0 / ratio) : 0.0;
 
@@ -505,8 +506,9 @@ process_main_options(int argc, const char* const* argv)
       break;
 
     case RECOMPRESS_THREADS:
-      recompress_threads = util::value_or_throw<Error>(util::parse_unsigned(
-        arg, 1, std::numeric_limits<uint32_t>::max(), "threads"));
+      recompress_threads =
+        static_cast<uint32_t>(util::value_or_throw<Error>(util::parse_unsigned(
+          arg, 1, std::numeric_limits<uint32_t>::max(), "threads")));
       break;
 
     case TRIM_MAX_SIZE: {
@@ -527,8 +529,8 @@ process_main_options(int argc, const char* const* argv)
 
     case TRIM_RECOMPRESS_THREADS:
       trim_recompress_threads =
-        util::value_or_throw<Error>(util::parse_unsigned(
-          arg, 1, std::numeric_limits<uint32_t>::max(), "threads"));
+        static_cast<uint32_t>(util::value_or_throw<Error>(util::parse_unsigned(
+          arg, 1, std::numeric_limits<uint32_t>::max(), "threads")));
       break;
 
     case 'v': // --verbose
index 142820747c31e2a059295fda690144f4937025ac..e0f6947f1d4240b5436099941755fe59cf3addb4 100644 (file)
@@ -300,6 +300,17 @@ struct CleanDirResult
   Level2Counters after;
 };
 
+template<typename T>
+static double
+ratio(T numerator, T denominator)
+{
+  if (denominator == 0) {
+    return 0.0;
+  } else {
+    return static_cast<double>(numerator) / static_cast<double>(denominator);
+  }
+}
+
 static CleanDirResult
 clean_dir(
   const std::string& l2_dir,
@@ -322,7 +333,7 @@ clean_dir(
     raw_files_map;
 
   for (size_t i = 0; i < files.size();
-       ++i, progress_receiver(1.0 / 3 + 1.0 * i / files.size() / 3)) {
+       ++i, progress_receiver(1.0 / 3 + 1.0 * ratio(i, files.size()) / 3)) {
     const auto& file = files[i];
 
     if (!file.is_regular_file()) {
@@ -360,7 +371,7 @@ clean_dir(
 
   bool cleaned = false;
   for (size_t i = 0; i < files.size();
-       ++i, progress_receiver(2.0 / 3 + 1.0 * i / files.size() / 3)) {
+       ++i, progress_receiver(2.0 / 3 + 1.0 * ratio(i, files.size()) / 3)) {
     const auto& file = files[i];
 
     if (!file || file.is_directory()) {
@@ -440,8 +451,8 @@ LocalStorage::finalize()
     // Pseudo-randomly choose one of the stats files in the 256 level 2
     // directories.
     const auto bucket = getpid() % 256;
-    const uint8_t l1_index = bucket / 16;
-    const uint8_t l2_index = bucket % 16;
+    const uint8_t l1_index = static_cast<uint8_t>(bucket / 16);
+    const uint8_t l2_index = static_cast<uint8_t>(bucket % 16);
     const auto l2_stats_file = get_stats_file(l1_index, l2_index);
 
     uint64_t l2_files_in_cache = 0;
@@ -812,7 +823,7 @@ LocalStorage::wipe_all(const ProgressReceiver& progress_receiver)
 
           for (size_t i = 0; i < files.size(); ++i) {
             util::remove_nfs_safe(files[i].path().string());
-            l2_progress_receiver(0.5 + 0.5 * i / files.size());
+            l2_progress_receiver(0.5 + 0.5 * ratio(i, files.size()));
           }
 
           if (!files.empty()) {
@@ -849,7 +860,7 @@ LocalStorage::get_compression_statistics(
             } catch (core::Error&) {
               cs.incompressible_size += cache_file.size_on_disk();
             }
-            l2_progress_receiver(0.2 + 0.8 * i / files.size());
+            l2_progress_receiver(0.2 + 0.8 * ratio(i, files.size()));
           }
         });
     });
@@ -920,7 +931,7 @@ LocalStorage::recompress(const std::optional<int8_t> level,
               incompressible_size += file.size_on_disk();
             }
 
-            l2_progress_receiver(0.1 + 0.9 * i / files.size());
+            l2_progress_receiver(0.1 + 0.9 * ratio(i, files.size()));
           }
 
           if (util::ends_with(l2_dir, "f/f")) {
@@ -938,16 +949,12 @@ LocalStorage::recompress(const std::optional<int8_t> level,
     PRINT_RAW(stdout, "\n\n");
   }
 
-  const double old_ratio = recompressor.old_size() > 0
-                             ? static_cast<double>(recompressor.content_size())
-                                 / recompressor.old_size()
-                             : 0.0;
+  const double old_ratio =
+    ratio(recompressor.content_size(), recompressor.old_size());
   const double old_savings =
     old_ratio > 0.0 ? 100.0 - (100.0 / old_ratio) : 0.0;
-  const double new_ratio = recompressor.new_size() > 0
-                             ? static_cast<double>(recompressor.content_size())
-                                 / recompressor.new_size()
-                             : 0.0;
+  const double new_ratio =
+    ratio(recompressor.content_size(), recompressor.new_size());
   const double new_savings =
     new_ratio > 0.0 ? 100.0 - (100.0 / new_ratio) : 0.0;
   const int64_t size_diff = static_cast<int64_t>(recompressor.new_size())
@@ -1241,7 +1248,8 @@ LocalStorage::perform_automatic_cleanup()
   // practice removing much newer entries than the oldest in other
   // subdirectories. By doing cleanup based on the number of files, both example
   // scenarios are improved.
-  const uint64_t target_files = 0.9 * evaluation->total_files / 256;
+  const uint64_t target_files = static_cast<uint64_t>(
+    0.9 * static_cast<double>(evaluation->total_files) / 256);
 
   auto clean_dir_result = clean_dir(
     get_subdir(evaluation->l1_index, largest_level_2_index), 0, target_files);
index 3b74c22f336c38a084c51a5ae86f2bc99d84d9ef..2b3401e6941debc9af323d312921b52fa05f785a 100644 (file)
@@ -140,7 +140,9 @@ RedisStorageBackend::RedisStorageBackend(const Params& params)
     }
   }
 
-  connect(url, connect_timeout.count(), operation_timeout.count());
+  connect(url,
+          static_cast<uint32_t>(connect_timeout.count()),
+          static_cast<uint32_t>(operation_timeout.count()));
   authenticate(url);
   select_database(url);
 }
@@ -244,9 +246,10 @@ RedisStorageBackend::connect(const Url& url,
   } else {
     const std::string host = url.host().empty() ? "localhost" : url.host();
     const uint32_t port =
-      url.port().empty() ? DEFAULT_PORT
-                         : util::value_or_throw<core::Fatal>(
-                           util::parse_unsigned(url.port(), 1, 65535, "port"));
+      url.port().empty()
+        ? DEFAULT_PORT
+        : static_cast<uint32_t>(util::value_or_throw<core::Fatal>(
+          util::parse_unsigned(url.port(), 1, 65535, "port")));
     ASSERT(url.path().empty() || url.path()[0] == '/');
 
     LOG("Redis connecting to {}:{} (connect timeout {} ms)",
@@ -293,8 +296,9 @@ RedisStorageBackend::select_database(const Url& url)
   }
   const uint32_t db_number =
     !db ? 0
-        : util::value_or_throw<core::Fatal>(util::parse_unsigned(
-          *db, 0, std::numeric_limits<uint32_t>::max(), "db number"));
+        : static_cast<uint32_t>(
+          util::value_or_throw<core::Fatal>(util::parse_unsigned(
+            *db, 0, std::numeric_limits<uint32_t>::max(), "db number")));
 
   if (db_number != 0) {
     LOG("Redis SELECT {}", db_number);
index 5ff4bef6f7941035461e92cf7a45f520cb9cce9f..b7ab082254ac1d032548e299a4cfa43560cf9011 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Joel Rosdahl and other contributors
+// Copyright (C) 2022-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -105,13 +105,13 @@ Duration::operator-(const Duration& other) const
 inline Duration
 Duration::operator*(double factor) const
 {
-  return Duration(0, factor * m_ns);
+  return Duration(0, static_cast<int64_t>(factor * static_cast<double>(m_ns)));
 }
 
 inline Duration
 Duration::operator/(double factor) const
 {
-  return Duration(0, m_ns / factor);
+  return Duration(0, static_cast<int64_t>(static_cast<double>(m_ns) / factor));
 }
 
 inline int64_t
@@ -129,7 +129,7 @@ Duration::nsec() const
 inline int32_t
 Duration::nsec_decimal_part() const
 {
-  return m_ns % 1'000'000'000;
+  return static_cast<int32_t>(m_ns % 1'000'000'000);
 }
 
 } // namespace util
index c57f265befe32112fa3baf2aba1e145eead4c111..b6c0b9f42e973d3a9a3eeb9e75582b178b37d709 100644 (file)
@@ -38,9 +38,8 @@
 #include <random>
 #include <sstream>
 
-// Seconds.
-const double k_min_sleep_time = 0.010;
-const double k_max_sleep_time = 0.050;
+const uint32_t k_min_sleep_time_ms = 100;
+const uint32_t k_max_sleep_time_ms = 500;
 #ifndef _WIN32
 const util::Duration k_staleness_limit(2);
 #endif
@@ -228,8 +227,8 @@ LockFile::do_acquire(const bool blocking)
   }();
 
   std::string initial_content;
-  RandomNumberGenerator sleep_ms_generator(k_min_sleep_time * 1000,
-                                           k_max_sleep_time * 1000);
+  RandomNumberGenerator sleep_ms_generator(k_min_sleep_time_ms,
+                                           k_max_sleep_time_ms);
 
   while (true) {
     const auto now = TimePoint::now();
@@ -356,8 +355,8 @@ void*
 LockFile::do_acquire(const bool blocking)
 {
   void* handle;
-  RandomNumberGenerator sleep_ms_generator(k_min_sleep_time * 1000,
-                                           k_max_sleep_time * 1000);
+  RandomNumberGenerator sleep_ms_generator(k_min_sleep_time_ms,
+                                           k_max_sleep_time_ms);
 
   while (true) {
     DWORD flags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE;
index 2c946a8341712b36cb049971b395988927cd68ac..cc1785fa410cd110fe0fbd98365450ff0367593f 100644 (file)
@@ -60,9 +60,9 @@ TemporaryFile::create(const fs::path& path_prefix, std::string_view suffix)
 
   // [1]: <https://github.com/Alexpux/mingw-w64/blob/
   // d0d7f784833bbb0b2d279310ddc6afb52fe47a46/mingw-w64-crt/misc/mkstemp.c>
-  Fd fd(bsd_mkstemps(&path_template[0], suffix.length()));
+  Fd fd(bsd_mkstemps(&path_template[0], static_cast<int>(suffix.length())));
 #else
-  Fd fd(mkstemps(&path_template[0], suffix.length()));
+  Fd fd(mkstemps(&path_template[0], static_cast<int>(suffix.length())));
 #endif
   if (!fd) {
     return tl::unexpected(FMT("failed to create temporary file for {}: {}",
index c35908b41aa5b6ec57fda3769c9e5c7bf48f0a8d..98a3cc80099cd715807ddfe1fe1535836863c4aa 100644 (file)
@@ -103,7 +103,7 @@ TimePoint::nsec() const
 inline int32_t
 TimePoint::nsec_decimal_part() const
 {
-  return m_ns % 1'000'000'000;
+  return static_cast<int32_t>(m_ns % 1'000'000'000);
 }
 
 inline void
index 9eaed5ce6a33a735d8093f47f236438609fa3cc9..9f9eb255328dab88c27a41bca556e7b11c6c4f6d 100644 (file)
@@ -89,7 +89,7 @@ void
 int_to_big_endian(T value, uint8_t* buffer)
 {
   for (size_t i = 0; i < sizeof(T); ++i) {
-    buffer[sizeof(T) - i - 1] = value & 0xFF;
+    buffer[sizeof(T) - i - 1] = static_cast<uint8_t>(value & 0xFF);
     value >>= 8;
   }
 }
index 45f80dc0269dc22df6b31c630e0fb8c418748344..8f4c805d4fae9cd6361de85d8714f870e3374ce9 100644 (file)
@@ -169,14 +169,15 @@ std::string
 format_human_readable_size(uint64_t size, SizeUnitPrefixType prefix_type)
 {
   const double factor = prefix_type == SizeUnitPrefixType::binary ? 1024 : 1000;
+  const double dsize = static_cast<double>(size);
   const char* infix = prefix_type == SizeUnitPrefixType::binary ? "i" : "";
-  if (size >= factor * factor * factor) {
-    return FMT("{:.1f} G{}B", size / (factor * factor * factor), infix);
-  } else if (size >= factor * factor) {
-    return FMT("{:.1f} M{}B", size / (factor * factor), infix);
-  } else if (size >= factor) {
+  if (dsize >= factor * factor * factor) {
+    return FMT("{:.1f} G{}B", dsize / (factor * factor * factor), infix);
+  } else if (dsize >= factor * factor) {
+    return FMT("{:.1f} M{}B", dsize / (factor * factor), infix);
+  } else if (dsize >= factor) {
     const char* k = prefix_type == SizeUnitPrefixType::binary ? "K" : "k";
-    return FMT("{:.1f} {}{}B", size / factor, k, infix);
+    return FMT("{:.1f} {}{}B", dsize / factor, k, infix);
   } else if (size == 1) {
     return "1 byte";
   } else {
@@ -308,7 +309,12 @@ parse_size(const std::string& value)
 tl::expected<mode_t, std::string>
 parse_umask(std::string_view value)
 {
-  return parse_unsigned(value, 0, 0777, "umask", 8);
+  auto result = parse_unsigned(value, 0, 0777, "umask", 8);
+  if (result) {
+    return static_cast<mode_t>(*result);
+  } else {
+    return tl::unexpected(result.error());
+  }
 }
 
 tl::expected<uint64_t, std::string>
index c47139e6b22f5e3a1056a7d786b52f32bf1c2450..81fe6f1ee5ec34ccd58fa847088d753ec63256b0 100644 (file)
@@ -79,7 +79,8 @@ zstd_supported_compression_level(int8_t wanted_level)
     return {1, "minimum level supported by libzstd"};
   }
 
-  const int8_t level = std::min<int>(wanted_level, ZSTD_maxCLevel());
+  const int8_t level =
+    static_cast<int8_t>(std::min<int>(wanted_level, ZSTD_maxCLevel()));
   if (level != wanted_level) {
     return {level, "max libzstd level"};
   }
index f52fc4c6d7c7a939f86a77bf28291b3ae5f88e31..25621779704e73ee8343d971e3a8e5a859320967 100644 (file)
@@ -47,7 +47,7 @@ TEST_CASE("util::Tokenizer")
       const auto res =
         util::split_into_views(input, separators, m_mode, m_include_delimiter);
       REQUIRE(res.size() == expected.size());
-      for (int i = 0, total = expected.size(); i < total; ++i) {
+      for (size_t i = 0, total = expected.size(); i < total; ++i) {
         CHECK(res[i] == expected[i]);
       }
     }
index 67982d80955e456df36c9762a92ae75b555b096b..46f4ce8c3fd0a2b4cae2b1aa3d6cd3a284f3095f 100644 (file)
@@ -122,7 +122,7 @@ TEST_CASE("util::read_file and util::write_file, binary data")
 
   std::vector<uint8_t> expected;
   for (size_t i = 0; i < 512; ++i) {
-    expected.push_back((32 + i) % 256);
+    expected.push_back(static_cast<uint8_t>((32 + i) % 256));
   }
 
   CHECK(util::write_file("test", expected));
index 1c49d70d549f2378dc57aa6cc298b76bb5cba378..876295ba62109a5fe2d2097083df0fec5b122e4c 100644 (file)
@@ -159,38 +159,36 @@ TEST_CASE("util::format_human_readable_diff")
     CHECK(util::format_human_readable_diff(0, SUPT::binary) == "0 bytes");
     CHECK(util::format_human_readable_diff(1, SUPT::binary) == "+1 byte");
     CHECK(util::format_human_readable_diff(42, SUPT::binary) == "+42 bytes");
-    CHECK(util::format_human_readable_diff(1949, SUPT::binary) == "+1.9 KiB");
-    CHECK(util::format_human_readable_diff(1951, SUPT::binary) == "+1.9 KiB");
-    CHECK(util::format_human_readable_diff(499.7 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(1'949, SUPT::binary) == "+1.9 KiB");
+    CHECK(util::format_human_readable_diff(1'951, SUPT::binary) == "+1.9 KiB");
+    CHECK(util::format_human_readable_diff(499'700, SUPT::binary)
           == "+488.0 KiB");
-    CHECK(util::format_human_readable_diff(1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(1'000'000, SUPT::binary)
           == "+976.6 KiB");
-    CHECK(util::format_human_readable_diff(1234 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(1'234'000, SUPT::binary)
           == "+1.2 MiB");
-    CHECK(util::format_human_readable_diff(438.5 * 1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(438'500'000, SUPT::binary)
           == "+418.2 MiB");
-    CHECK(util::format_human_readable_diff(1000 * 1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(1'000'000'000, SUPT::binary)
           == "+953.7 MiB");
-    CHECK(
-      util::format_human_readable_diff(17.11 * 1000 * 1000 * 1000, SUPT::binary)
-      == "+15.9 GiB");
+    CHECK(util::format_human_readable_diff(17'110'000'000, SUPT::binary)
+          == "+15.9 GiB");
 
     CHECK(util::format_human_readable_diff(-1, SUPT::binary) == "-1 byte");
     CHECK(util::format_human_readable_diff(-42, SUPT::binary) == "-42 bytes");
-    CHECK(util::format_human_readable_diff(-1949, SUPT::binary) == "-1.9 KiB");
-    CHECK(util::format_human_readable_diff(-1951, SUPT::binary) == "-1.9 KiB");
-    CHECK(util::format_human_readable_diff(-499.7 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(-1'949, SUPT::binary) == "-1.9 KiB");
+    CHECK(util::format_human_readable_diff(-1'951, SUPT::binary) == "-1.9 KiB");
+    CHECK(util::format_human_readable_diff(-499'700, SUPT::binary)
           == "-488.0 KiB");
-    CHECK(util::format_human_readable_diff(-1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(-1'000'000, SUPT::binary)
           == "-976.6 KiB");
-    CHECK(util::format_human_readable_diff(-1234 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(-1'234'000, SUPT::binary)
           == "-1.2 MiB");
-    CHECK(util::format_human_readable_diff(-438.5 * 1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(-438'500'000, SUPT::binary)
           == "-418.2 MiB");
-    CHECK(util::format_human_readable_diff(-1000 * 1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_diff(-1'000'000'000, SUPT::binary)
           == "-953.7 MiB");
-    CHECK(util::format_human_readable_diff(-17.11 * 1000 * 1000 * 1000,
-                                           SUPT::binary)
+    CHECK(util::format_human_readable_diff(-17'110'000'000, SUPT::binary)
           == "-15.9 GiB");
   }
 
@@ -199,38 +197,36 @@ TEST_CASE("util::format_human_readable_diff")
     CHECK(util::format_human_readable_diff(0, SUPT::decimal) == "0 bytes");
     CHECK(util::format_human_readable_diff(1, SUPT::decimal) == "+1 byte");
     CHECK(util::format_human_readable_diff(42, SUPT::decimal) == "+42 bytes");
-    CHECK(util::format_human_readable_diff(1949, SUPT::decimal) == "+1.9 kB");
-    CHECK(util::format_human_readable_diff(1951, SUPT::decimal) == "+2.0 kB");
-    CHECK(util::format_human_readable_diff(499.7 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(1'949, SUPT::decimal) == "+1.9 kB");
+    CHECK(util::format_human_readable_diff(1'951, SUPT::decimal) == "+2.0 kB");
+    CHECK(util::format_human_readable_diff(499'700, SUPT::decimal)
           == "+499.7 kB");
-    CHECK(util::format_human_readable_diff(1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(1'000'000, SUPT::decimal)
           == "+1.0 MB");
-    CHECK(util::format_human_readable_diff(1234 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(1'234'000, SUPT::decimal)
           == "+1.2 MB");
-    CHECK(util::format_human_readable_diff(438.5 * 1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(438'500'000, SUPT::decimal)
           == "+438.5 MB");
-    CHECK(util::format_human_readable_diff(1000 * 1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(1'000'000'000, SUPT::decimal)
           == "+1.0 GB");
-    CHECK(util::format_human_readable_diff(17.11 * 1000 * 1000 * 1000,
-                                           SUPT::decimal)
+    CHECK(util::format_human_readable_diff(17'110'000'000, SUPT::decimal)
           == "+17.1 GB");
 
     CHECK(util::format_human_readable_diff(-1, SUPT::decimal) == "-1 byte");
     CHECK(util::format_human_readable_diff(-42, SUPT::decimal) == "-42 bytes");
-    CHECK(util::format_human_readable_diff(-1949, SUPT::decimal) == "-1.9 kB");
-    CHECK(util::format_human_readable_diff(-1951, SUPT::decimal) == "-2.0 kB");
-    CHECK(util::format_human_readable_diff(-499.7 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(-1'949, SUPT::decimal) == "-1.9 kB");
+    CHECK(util::format_human_readable_diff(-1'951, SUPT::decimal) == "-2.0 kB");
+    CHECK(util::format_human_readable_diff(-499'700, SUPT::decimal)
           == "-499.7 kB");
-    CHECK(util::format_human_readable_diff(-1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(-1'000'000, SUPT::decimal)
           == "-1.0 MB");
-    CHECK(util::format_human_readable_diff(-1234 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(-1'234'000, SUPT::decimal)
           == "-1.2 MB");
-    CHECK(util::format_human_readable_diff(-438.5 * 1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(-438'500'000, SUPT::decimal)
           == "-438.5 MB");
-    CHECK(util::format_human_readable_diff(-1000 * 1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_diff(-1'000'000'000, SUPT::decimal)
           == "-1.0 GB");
-    CHECK(util::format_human_readable_diff(-17.11 * 1000 * 1000 * 1000,
-                                           SUPT::decimal)
+    CHECK(util::format_human_readable_diff(-17'110'000'000, SUPT::decimal)
           == "-17.1 GB");
   }
 }
@@ -244,21 +240,20 @@ TEST_CASE("util::format_human_readable_size")
     CHECK(util::format_human_readable_size(0, SUPT::binary) == "0 bytes");
     CHECK(util::format_human_readable_size(1, SUPT::binary) == "1 byte");
     CHECK(util::format_human_readable_size(42, SUPT::binary) == "42 bytes");
-    CHECK(util::format_human_readable_size(1949, SUPT::binary) == "1.9 KiB");
-    CHECK(util::format_human_readable_size(1951, SUPT::binary) == "1.9 KiB");
-    CHECK(util::format_human_readable_size(499.7 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_size(1'949, SUPT::binary) == "1.9 KiB");
+    CHECK(util::format_human_readable_size(1'951, SUPT::binary) == "1.9 KiB");
+    CHECK(util::format_human_readable_size(499'700, SUPT::binary)
           == "488.0 KiB");
-    CHECK(util::format_human_readable_size(1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_size(1'000'000, SUPT::binary)
           == "976.6 KiB");
-    CHECK(util::format_human_readable_size(1234 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_size(1'234'000, SUPT::binary)
           == "1.2 MiB");
-    CHECK(util::format_human_readable_size(438.5 * 1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_size(438'500'000, SUPT::binary)
           == "418.2 MiB");
-    CHECK(util::format_human_readable_size(1000 * 1000 * 1000, SUPT::binary)
+    CHECK(util::format_human_readable_size(1'000'000'000, SUPT::binary)
           == "953.7 MiB");
-    CHECK(
-      util::format_human_readable_size(17.11 * 1000 * 1000 * 1000, SUPT::binary)
-      == "15.9 GiB");
+    CHECK(util::format_human_readable_size(17'110'000'000, SUPT::binary)
+          == "15.9 GiB");
   }
 
   SUBCASE("decimal")
@@ -266,20 +261,19 @@ TEST_CASE("util::format_human_readable_size")
     CHECK(util::format_human_readable_size(0, SUPT::decimal) == "0 bytes");
     CHECK(util::format_human_readable_size(1, SUPT::decimal) == "1 byte");
     CHECK(util::format_human_readable_size(42, SUPT::decimal) == "42 bytes");
-    CHECK(util::format_human_readable_size(1949, SUPT::decimal) == "1.9 kB");
-    CHECK(util::format_human_readable_size(1951, SUPT::decimal) == "2.0 kB");
-    CHECK(util::format_human_readable_size(499.7 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_size(1'949, SUPT::decimal) == "1.9 kB");
+    CHECK(util::format_human_readable_size(1'951, SUPT::decimal) == "2.0 kB");
+    CHECK(util::format_human_readable_size(499'700, SUPT::decimal)
           == "499.7 kB");
-    CHECK(util::format_human_readable_size(1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_size(1'000'000, SUPT::decimal)
           == "1.0 MB");
-    CHECK(util::format_human_readable_size(1234 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_size(1'234'000, SUPT::decimal)
           == "1.2 MB");
-    CHECK(util::format_human_readable_size(438.5 * 1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_size(438'500'000, SUPT::decimal)
           == "438.5 MB");
-    CHECK(util::format_human_readable_size(1000 * 1000 * 1000, SUPT::decimal)
+    CHECK(util::format_human_readable_size(1'000'000'000, SUPT::decimal)
           == "1.0 GB");
-    CHECK(util::format_human_readable_size(17.11 * 1000 * 1000 * 1000,
-                                           SUPT::decimal)
+    CHECK(util::format_human_readable_size(17'110'000'000, SUPT::decimal)
           == "17.1 GB");
   }
 }
@@ -387,14 +381,14 @@ TEST_CASE("util::parse_size")
         == h(u64(42) * 1024 * 1024 * 1024, SUPT::binary));
 
   // Decimal suffixes
-  CHECK(*util::parse_size("78k") == h(78 * 1000, SUPT::decimal));
-  CHECK(*util::parse_size("78K") == h(78 * 1000, SUPT::decimal));
-  CHECK(*util::parse_size("1.1 M") == h(u64(1.1 * 1000 * 1000), SUPT::decimal));
+  CHECK(*util::parse_size("78k") == h(78'000, SUPT::decimal));
+  CHECK(*util::parse_size("78K") == h(78'000, SUPT::decimal));
+  CHECK(*util::parse_size("1.1 M") == h(u64(1.1 * 1'000'000), SUPT::decimal));
   CHECK(*util::parse_size("438.55M")
-        == h(u64(438.55 * 1000 * 1000), SUPT::decimal));
-  CHECK(*util::parse_size("1 G") == h(1 * 1000 * 1000 * 1000, SUPT::decimal));
+        == h(u64(438.55 * 1'000'000), SUPT::decimal));
+  CHECK(*util::parse_size("1 G") == h(1 * 1'000'000'000, SUPT::decimal));
   CHECK(*util::parse_size("2T")
-        == h(u64(2) * 1000 * 1000 * 1000 * 1000, SUPT::decimal));
+        == h(u64(2) * 1'000'000 * 1'000'000, SUPT::decimal));
 
   // Binary suffixes
   CHECK(*util::parse_size("78 Ki") == h(78 * 1024, SUPT::binary));
@@ -406,7 +400,7 @@ TEST_CASE("util::parse_size")
         == h(u64(2) * 1024 * 1024 * 1024 * 1024, SUPT::binary));
 
   // With B suffix
-  CHECK(*util::parse_size("9MB") == h(9 * 1000 * 1000, SUPT::decimal));
+  CHECK(*util::parse_size("9MB") == h(9 * 1'000'000, SUPT::decimal));
   CHECK(*util::parse_size("9MiB") == h(9 * 1024 * 1024, SUPT::binary));
 
   // Errors