Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions csnode/include/csnode/compressor.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef COMPRESSOR_HPP
#define COMPRESSOR_HPP

#include <limits>
#include <mutex>

#include <lz4.h>
Expand Down Expand Up @@ -65,13 +66,27 @@ class Compressor {
// try to decompress data, returns object if serializable
template<typename T>
T decompress(const CompressedRegion& region) {
// region and its binary size may come from the network: reject what no compress() could produce
if (region.size() < static_cast<size_t>(byteSizeof_)) {
cserror() << "Decompress error of " << cstype(T) << ": empty region";
return T{};
}

const auto compression = checkCompression(region.data(), region.size());

cs::Bytes bytes;
cs::Byte* data = nullptr;
size_t size = 0;

if (compression == Compression::Compressed) {
const size_t compressedSize = region.size() - static_cast<size_t>(byteSizeof_);

if (region.binarySize() > static_cast<size_t>(std::numeric_limits<int>::max()) ||
region.binarySize() > compressedSize * kLz4MaxExpansion) {
cserror() << "Decompress error of " << cstype(T) << ": invalid binary size " << region.binarySize();
return T{};
}

bytes.resize(region.binarySize());

const int uncompressedSize = LZ4_decompress_safe(reinterpret_cast<char*>(region.data()) + byteSizeof_, reinterpret_cast<char*>(bytes.data()),
Expand Down Expand Up @@ -99,6 +114,9 @@ class Compressor {

private:
static inline int byteSizeof_ = sizeof(cs::Byte);

// LZ4 can not expand compressed data more than 255 times
static constexpr size_t kLz4MaxExpansion = 256;
};

// multi-threaded compressor
Expand Down
21 changes: 21 additions & 0 deletions tests/csnode/compressor_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ TEST(Compressor, TestCompressByte) {
ASSERT_EQ(byte, entity);
}

// a region and its binary size are read from the network (Node::getBlockReply)
TEST(Compressor, RejectsBinarySizeLz4CannotProduce) {
cs::Compressor compressor;
std::vector<size_t> data(100, 7);

auto region = compressor.compress(data);
ASSERT_EQ(cs::Compressor::checkCompression(region), cs::Compressor::Compression::Compressed);

cs::CompressedRegion forged{ cs::Bytes(region.bytes()), std::numeric_limits<size_t>::max() / 2 };
auto entity = compressor.decompress<std::vector<size_t>>(forged);

ASSERT_TRUE(entity.empty());
}

TEST(Compressor, RejectsEmptyRegion) {
cs::Compressor compressor;
auto entity = compressor.decompress<std::vector<size_t>>(cs::CompressedRegion{});

ASSERT_TRUE(entity.empty());
}

TEST(SynchronizedCompressor, BaseSynchronizedCompressorUsage) {
cs::SynchronizedCompressor compressor;

Expand Down
Loading