Skip to content

csdb: bound Pool reserve() by the bytes left to parse (huge allocation) - #7

Merged
EnzinoBB merged 1 commit into
masterfrom
fix/pool-bound-reserve
Oct 8, 2026
Merged

EnzinoBB merged 1 commit into
masterfrom
fix/pool-bound-reserve

Conversation

@EnzinoBB

@EnzinoBB EnzinoBB commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Problem: huge allocations decided by received data

Pool::priv::getTransactions() and getNewWallets() reserve() as many elements as the uint32 count read from the data. A peer-supplied block (e.g. a BlockReply while syncing) with a count near 2^32 asks for tens of GB. std::bad_alloc/length_error escapes the transport thread and the node terminates.

Fix (csdb/src/pool.cpp, +4/-2)

Reserve at most the number of bytes still unread, because every element takes at least one of them.

  • Valid data: reserves exactly what it did before.
  • Forged data: the parse loop fails at the first missing element, as it already did, and the Pool is invalid.

The other counts in Pool parsing are uint8 and were left as they are.

Tests (tests/csnode/pool_tests.cpp)

Test Before After
Pool.HugeTransactionsCountDoesNotAllocate from_binary throws ✅
Pool.HugeNewWalletsCountDoesNotAllocate from_binary throws ✅
full nodetests — 116/116

Upstream

A single commit that applies to upstream. It touches pool.cpp in different places from PR #5, so the two apply without conflicts.

🤖 Generated with Claude Code

Pool::priv::getTransactions() and getNewWallets() reserve() as many elements
as the uint32 count read from the data. A peer-supplied block (e.g. a
BlockReply while syncing) with a count near 2^32 asks for tens of GB:
std::bad_alloc/length_error escapes the transport thread and the node
terminates.

Reserve at most the number of bytes still unread, because every element
takes at least one of them. For valid data this reserves exactly what it did
before. For forged data the parse loop fails at the first missing element, as
it already did, and the pool is invalid.

The other counts in Pool parsing are uint8, so they are left as they are.

Tests (tests/csnode/pool_tests.cpp):
- Pool.HugeTransactionsCountDoesNotAllocate: before the fix from_binary throws.
- Pool.HugeNewWalletsCountDoesNotAllocate: before the fix from_binary throws.
nodetests: 116/116 pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@EnzinoBB
EnzinoBB merged commit ff2e59a into master Oct 8, 2026
1 check passed
EnzinoBB added a commit that referenced this pull request Nov 5, 2026
- setup-test: "tokens node" passes --node-key too. The wallet no longer takes
  the node's key from the current folder (wallet PR #7), and this wallet.toml
  says node_key = "off" (the answer was create), so "node" did not resolve and
  the {"error": ...} JSON ended in KeyError 'source'. The error is now asserted
  first.
- setup.ps1: the command printed for later quotes only the arguments that need
  it (".\credits-wallet.exe --config wallet.toml account new main"), as
  setup.sh does; the test looks for "account new main".
- both: the create option says the account is under the wallet password, one
  for every account, chosen with the first (wallet PRs #9/#10).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant