From 339ce0bfc0610f5c91e13cb1cae28bfda6e147bc Mon Sep 17 00:00:00 2001 From: Philip Pronin Date: Wed, 12 Oct 2016 10:57:13 -0700 Subject: [PATCH] ZSTDCodec should properly handle non-shortcut levels Summary: Currently only shortcut levels are properly handled, others result in using level 1. Reviewed By: yfeldblum Differential Revision: D4008123 fbshipit-source-id: 37845eeec139007738f99e72ecfb969c6a2e5652 --- folly/io/Compression.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/folly/io/Compression.cpp b/folly/io/Compression.cpp index e1c65ef0..9e86c4c0 100644 --- a/folly/io/Compression.cpp +++ b/folly/io/Compression.cpp @@ -960,7 +960,7 @@ class ZSTDCodec final : public Codec { const IOBuf* data, uint64_t uncompressedLength) override; - int level_{1}; + int level_; }; std::unique_ptr ZSTDCodec::create(int level, CodecType type) { @@ -971,15 +971,20 @@ ZSTDCodec::ZSTDCodec(int level, CodecType type) : Codec(type) { DCHECK(type == CodecType::ZSTD); switch (level) { case COMPRESSION_LEVEL_FASTEST: - level_ = 1; + level = 1; break; case COMPRESSION_LEVEL_DEFAULT: - level_ = 1; + level = 1; break; case COMPRESSION_LEVEL_BEST: - level_ = 19; + level = 19; break; } + if (level < 1 || level > ZSTD_maxCLevel()) { + throw std::invalid_argument( + to("ZSTD: invalid level: ", level)); + } + level_ = level; } bool ZSTDCodec::doNeedsUncompressedLength() const { -- 2.34.1