From 823bf3d08de6aff1b0fd10f384bf72e727412d07 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Tue, 6 Sep 2016 20:11:02 -0700 Subject: [PATCH] Fix invalid narrowing conversion to size_t --- contrib/pzstd/Pzstd.cpp | 19 +++++++++++-------- contrib/pzstd/Pzstd.h | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/contrib/pzstd/Pzstd.cpp b/contrib/pzstd/Pzstd.cpp index 09eab90f1..0caf0f933 100644 --- a/contrib/pzstd/Pzstd.cpp +++ b/contrib/pzstd/Pzstd.cpp @@ -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(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 ¶ms) { 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::max()}) { + step = std::min(step, size_t{newStep}); } } return step; @@ -268,7 +271,7 @@ void asyncCompressChunks( WorkQueue>& chunks, ThreadPool& executor, FILE* fd, - size_t size, + std::uintmax_t size, size_t numThreads, ZSTD_parameters params) { auto chunksGuard = makeScopeGuard([&] { chunks.finish(); }); diff --git a/contrib/pzstd/Pzstd.h b/contrib/pzstd/Pzstd.h index 617aecb3f..51d15846c 100644 --- a/contrib/pzstd/Pzstd.h +++ b/contrib/pzstd/Pzstd.h @@ -19,6 +19,7 @@ #undef ZSTD_STATIC_LINKING_ONLY #include +#include #include namespace pzstd { @@ -52,7 +53,7 @@ void asyncCompressChunks( WorkQueue>& chunks, ThreadPool& executor, FILE* fd, - std::size_t size, + std::uintmax_t size, std::size_t numThreads, ZSTD_parameters parameters);