From 626f5abbe49a6f0607ff7ab519e7b836e29c4de6 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Tue, 13 Jun 2017 14:04:08 -0700 Subject: [PATCH] Add fuzz testing 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 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/folly/io/Compression.cpp b/folly/io/Compression.cpp index b1671a56..3e027d19 100644 --- a/folly/io/Compression.cpp +++ b/folly/io/Compression.cpp @@ -64,6 +64,9 @@ Codec::Codec(CodecType type) : type_(type) { } // Ensure consistent behavior in the nullptr case std::unique_ptr 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 Codec::uncompress( const IOBuf* data, Optional 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; -- 2.34.1