Fix invalid narrowing conversion to size_t

dev
Nick Terrell 2016-09-06 20:11:02 -07:00
parent b3ed23e18e
commit 823bf3d08d
2 changed files with 13 additions and 9 deletions

View 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(); });

View 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);