Apply clang-format to folly/io/ (namespace)
[folly.git] / folly / io / Compression.cpp
index b1671a5645d29cce6c530a99d30cfe3aa4d68a13..98f332b85eb55d34f047d47ef51e7cd368369e16 100644 (file)
 #include <algorithm>
 #include <unordered_set>
 
-namespace folly { namespace io {
+namespace folly {
+namespace io {
 
 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 +94,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");
@@ -506,7 +513,7 @@ inline uint64_t decodeVarintFromCursor(folly::io::Cursor& cursor) {
   return val;
 }
 
-}  // namespace
+} // namespace
 
 #endif  // FOLLY_HAVE_LIBLZ4 || FOLLY_HAVE_LIBLZMA
 
@@ -1787,7 +1794,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;
@@ -2315,4 +2324,5 @@ std::unique_ptr<Codec> getAutoUncompressionCodec(
     std::vector<std::unique_ptr<Codec>> customCodecs) {
   return AutomaticCodec::create(std::move(customCodecs));
 }
-}}  // namespaces
+} // namespace io
+} // namespace folly