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
6 changes: 4 additions & 2 deletions csdb/src/pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ class Pool::priv : public ::csdb::internal::shared_data {

bool getTransactions(::csdb::priv::ibstream& is, size_t cnt) {
transactions_.clear();
transactions_.reserve(cnt);
// cnt comes from the data: every transaction takes at least one more byte of them
transactions_.reserve(std::min(cnt, is.size()));
for (size_t i = 0; i < cnt; ++i) {
Transaction tran;
if (!is.get(tran)) {
Expand Down Expand Up @@ -382,7 +383,8 @@ class Pool::priv : public ::csdb::internal::shared_data {
}

newWallets_.clear();
newWallets_.reserve(cnt);
// cnt comes from the data: every wallet takes at least one more byte of them
newWallets_.reserve(std::min(static_cast<size_t>(cnt), is.size()));
for (uint32_t i = 0; i < cnt; ++i) {
NewWalletInfo wall;
if (!is.get(wall)) {
Expand Down
51 changes: 51 additions & 0 deletions tests/csnode/pool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include "gtest/gtest.h"

#include <cstdint>
#include <cstring>
#include <limits>

#include <csdb/pool.hpp>

Expand Down Expand Up @@ -38,3 +40,52 @@ TEST(Pool, RejectsHashingLengthPastData) {
ASSERT_FALSE(csdb::Pool::from_binary(cs::Bytes(bytes)).is_valid());
ASSERT_TRUE(csdb::Pool::hash_from_binary(cs::Bytes(bytes)).is_empty());
}

// offset of the transactions count: the only uint32 that makes meta_from_binary() report it
static size_t transactionsCountOffset(const cs::Bytes& bytes) {
const uint32_t marker = std::numeric_limits<uint32_t>::max();

for (size_t offset = 0; offset + sizeof(marker) <= bytes.size(); ++offset) {
cs::Bytes copy = bytes;
std::memcpy(copy.data() + offset, &marker, sizeof(marker));

size_t count = 0;
if (csdb::Pool::meta_from_binary(std::move(copy), count).is_valid() && count == marker) {
return offset;
}
}

return bytes.size();
}

// counts come from the data: a forged one must not turn into a huge allocation
TEST(Pool, HugeTransactionsCountDoesNotAllocate) {
size_t hashingLength = 0;
cs::Bytes bytes = poolBinary(hashingLength);

const size_t offset = transactionsCountOffset(bytes);
ASSERT_LT(offset, bytes.size());

const uint32_t forged = std::numeric_limits<uint32_t>::max();
std::memcpy(bytes.data() + offset, &forged, sizeof(forged));

csdb::Pool pool;
ASSERT_NO_THROW(pool = csdb::Pool::from_binary(std::move(bytes)));
ASSERT_FALSE(pool.is_valid());
}

// with no transactions, the new wallets count follows the transactions count
TEST(Pool, HugeNewWalletsCountDoesNotAllocate) {
size_t hashingLength = 0;
cs::Bytes bytes = poolBinary(hashingLength);

const size_t offset = transactionsCountOffset(bytes) + sizeof(uint32_t);
ASSERT_LE(offset + sizeof(uint32_t), bytes.size());

const uint32_t forged = std::numeric_limits<uint32_t>::max();
std::memcpy(bytes.data() + offset, &forged, sizeof(forged));

csdb::Pool pool;
ASSERT_NO_THROW(pool = csdb::Pool::from_binary(std::move(bytes)));
ASSERT_FALSE(pool.is_valid());
}
Loading