diff --git a/csdb/src/pool.cpp b/csdb/src/pool.cpp index 58116d6bd..34132734f 100644 --- a/csdb/src/pool.cpp +++ b/csdb/src/pool.cpp @@ -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)) { @@ -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(cnt), is.size())); for (uint32_t i = 0; i < cnt; ++i) { NewWalletInfo wall; if (!is.get(wall)) { diff --git a/tests/csnode/pool_tests.cpp b/tests/csnode/pool_tests.cpp index f3127ce8d..7f1d451a7 100644 --- a/tests/csnode/pool_tests.cpp +++ b/tests/csnode/pool_tests.cpp @@ -2,7 +2,9 @@ #include "gtest/gtest.h" +#include #include +#include #include @@ -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::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::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::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()); +}