Add fuzz testing
authorNick Terrell <terrelln@fb.com>
Tue, 13 Jun 2017 21:04:08 +0000 (14:04 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 13 Jun 2017 21:06:58 +0000 (14:06 -0700)
Summary: `ZSTD_decompress()` doesn't verify the uncompressed length in the frame as I thought, so throw an exception instead of `DCHECK()`.

Reviewed By: meyering

Differential Revision: D5234576

fbshipit-source-id: f72cf085a7267de32ce13553ce7ebbfe3b8a3f05

folly/io/Compression.cpp

index b1671a5645d29cce6c530a99d30cfe3aa4d68a13..3e027d19fea27127c5725e035c30154d7915d935 100644 (file)
@@ -64,6 +64,9 @@ Codec::Codec(CodecType type) : type_(type) { }
 
 // Ensure consistent behavior in the nullptr case
 std::unique_ptr<IOBuf> Codec::compress(const IOBuf* data) {
+  if (data == nullptr) {
+    throw std::invalid_argument("Codec: data must not be nullptr");
+  }
   uint64_t len = data->computeChainDataLength();
   if (len == 0) {
     return IOBuf::create(0);
@@ -90,6 +93,9 @@ std::string Codec::compress(const StringPiece data) {
 std::unique_ptr<IOBuf> Codec::uncompress(
     const IOBuf* data,
     Optional<uint64_t> uncompressedLength) {
+  if (data == nullptr) {
+    throw std::invalid_argument("Codec: data must not be nullptr");
+  }
   if (!uncompressedLength) {
     if (needsUncompressedLength()) {
       throw std::invalid_argument("Codec: uncompressed length required");
@@ -1787,7 +1793,9 @@ bool ZSTDStreamCodec::tryBlockUncompress(
   size_t const length = ZSTD_decompress(
       output.data(), *uncompressedLength(), input.data(), compressedLength);
   zstdThrowIfError(length);
-  DCHECK_EQ(length, *uncompressedLength());
+  if (length != *uncompressedLength()) {
+    throw std::runtime_error("ZSTDStreamCodec: Incorrect uncompressed length");
+  }
   input.uncheckedAdvance(compressedLength);
   output.uncheckedAdvance(length);
   return true;