ZSTDCodec should properly handle non-shortcut levels
authorPhilip Pronin <philipp@fb.com>
Wed, 12 Oct 2016 17:57:13 +0000 (10:57 -0700)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Wed, 12 Oct 2016 18:08:47 +0000 (11:08 -0700)
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

index e1c65ef0ff2cd73776dd46faa3aeb61433ecf339..9e86c4c06b616840ed2947c0655d6badd4065e4d 100644 (file)
@@ -960,7 +960,7 @@ class ZSTDCodec final : public Codec {
       const IOBuf* data,
       uint64_t uncompressedLength) override;
 
-  int level_{1};
+  int level_;
 };
 
 std::unique_ptr<Codec> 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<std::string>("ZSTD: invalid level: ", level));
+  }
+  level_ = level;
 }
 
 bool ZSTDCodec::doNeedsUncompressedLength() const {