]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Fix invalid narrowing conversion to size_t
authorNick Terrell <terrelln@fb.com>
Wed, 7 Sep 2016 03:11:02 +0000 (20:11 -0700)
committerNick Terrell <terrelln@fb.com>
Wed, 7 Sep 2016 03:11:02 +0000 (20:11 -0700)
contrib/pzstd/Pzstd.cpp
contrib/pzstd/Pzstd.h

index 09eab90f161461cc531e9c95d447af9fd93107a2..0caf0f933ab530994c5a6768091182546b207b2f 100644 (file)
@@ -34,14 +34,14 @@ using std::size_t;
 size_t pzstdMain(const Options& options, ErrorHolder& errorHolder) {
   // Open the input file and attempt to determine its size
   FILE* inputFd = stdin;
-  size_t inputSize = 0;
+  std::uintmax_t inputSize = 0;
   if (options.inputFile != "-") {
     inputFd = std::fopen(options.inputFile.c_str(), "rb");
     if (!errorHolder.check(inputFd != nullptr, "Failed to open input file")) {
       return 0;
     }
     std::error_code ec;
-    inputSize = static_cast<size_t>(file_size(options.inputFile, ec));
+    inputSize = file_size(options.inputFile, ec);
     if (ec) {
       inputSize = 0;
     }
@@ -217,14 +217,17 @@ static void compress(
  * @param numThreads The number of threads available to run compression jobs on
  * @param params     The zstd parameters to be used for compression
  */
-static size_t
-calculateStep(size_t size, size_t numThreads, const ZSTD_parameters& params) {
+static size_t calculateStep(
+    std::uintmax_t size,
+    size_t numThreads,
+    const ZSTD_parameters &params) {
   size_t step = 1ul << (params.cParams.windowLog + 2);
   // If file size is known, see if a smaller step will spread work more evenly
   if (size != 0) {
-    size_t newStep = size / numThreads;
-    if (newStep != 0) {
-      step = std::min(step, newStep);
+    const std::uintmax_t newStep = size / std::uintmax_t{numThreads};
+    if (newStep != 0 &&
+        newStep <= std::uintmax_t{std::numeric_limits<size_t>::max()}) {
+      step = std::min(step, size_t{newStep});
     }
   }
   return step;
@@ -268,7 +271,7 @@ void asyncCompressChunks(
     WorkQueue<std::shared_ptr<BufferWorkQueue>>& chunks,
     ThreadPool& executor,
     FILE* fd,
-    size_t size,
+    std::uintmax_t size,
     size_t numThreads,
     ZSTD_parameters params) {
   auto chunksGuard = makeScopeGuard([&] { chunks.finish(); });
index 617aecb3f69167caa4b81003d2e1650d393024e9..51d15846c0e03c30d5c44f3190975d20b63268fb 100644 (file)
@@ -19,6 +19,7 @@
 #undef ZSTD_STATIC_LINKING_ONLY
 
 #include <cstddef>
+#include <cstdint>
 #include <memory>
 
 namespace pzstd {
@@ -52,7 +53,7 @@ void asyncCompressChunks(
     WorkQueue<std::shared_ptr<BufferWorkQueue>>& chunks,
     ThreadPool& executor,
     FILE* fd,
-    std::size_t size,
+    std::uintmax_t size,
     std::size_t numThreads,
     ZSTD_parameters parameters);