wallet, bdbro: Reject overflow pages without data

bitcoin/bitcoin: PR #36886

1 minor finding · 2 suggestions

Findings

Link

[minor] · The new overflow rejection is missing from the fuzz allowlist

Source inspection shows that an input reaching the new empty-overflow rejection would crash wallet_bdb_parser instead of being treated as an expected malformed-input rejection.

Location: src/wallet/migrate.cpp:705 (head).

Details and suggested fix

MakeBerkeleyRODatabase catches the new exception and returns its text in error.original, but wallet_bdb_parser rethrows every error absent from its allowlist. Overflow page has no data is absent, so this expected rejection escapes the fuzz entry point. An otherwise accepted file with an empty terminal overflow page exercises the changed path without relying on the old infinite loop. Add the new message to the fuzz allowlist in this change. The two older overflow-length messages are also missing, but those omissions predate this PR.


Optional improvements

Link

[suggestion] · The blanket empty-page rejection lacks a documented compatibility basis

The check also rejects finite complete chains accepted at base; whether supported BDB writers can produce such chains remains unverified.

Location: src/wallet/migrate.cpp:702 (head).

Details and suggested fix

An empty interior page followed by pages supplying all item_len bytes was readable at base, but now causes the whole file to fail loading. Both bitcoin-wallet dump and legacy migration select BERKELEY_RO and stop on that failure. Please verify the commit message's no-empty-page claim against the supported BDB overflow writer and cite that invariant alongside the check; the current comment explains termination, not compatibility. If supported writers can emit empty pages, use a traversal bound that preserves finite complete chains, such as an overflow-safe budget based on the file's page count.


Link

[suggestion] · The infinite-loop regression test has no bounded failure

If the empty-page guard regresses, the new functional test waits indefinitely for bitcoin-wallet rather than reporting a local timeout failure.

Location: test/functional/tool_wallet.py:494 (head).

Details and suggested fix

The self-linked empty page keeps the parser reading page 4 without increasing data.size(). assert_raises_tool_error waits in p.communicate() without a timeout, so its return-code and error-text assertions never run in that case. The test runner also waits without a process deadline; its half-second polling timeout does not bound execution. Give this invocation a timeout scaled by self.options.timeout_factor, then kill and reap the child on expiry and fail with an overflow-traversal diagnostic. Keep the existing assertions for normal completion.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

The independent BDB reader preserves access to legacy wallet data without retaining BDB as a dependency or maintaining the full legacy wallet implementation.

At the pinned head, both bitcoin-wallet dump and legacy migration explicitly select BERKELEY_RO. The overflow-page behavior therefore belongs to a long-term data-access and migration boundary, not Bitcoin's network protocol or consensus rules. Rejecting a file here affects whether its legacy records can be extracted or migrated.

Source 1 · Source 2

Read-only extraction was intended to avoid modifying the source wallet, but historical discussion also emphasized that a non-writing parser can still produce an unreliable dump. That concern initially led to making the independent reader optional.

The pinned parser opens the source file with rb and reconstructs records in memory. The historical tradeoff is preservation of the original file versus faithful recovery of its contents; read-only operation alone does not settle which unusual files should be accepted. The earlier optional rollout is historical context, not a description of the pinned head's selection behavior.

Source 1 · Source 2 · Source 3

Wallet files were treated as fallible external input rather than trusted internal state: historical fuzzing prompted replacing assertions with exceptions, and compatibility testing compared the independent reader against BDB.

The pinned Open path reports malformed structures through runtime errors, which MakeBerkeleyRODatabase converts into a failed load. The added empty-page rejection uses this existing error boundary. Historically, the author also reported cases where the independent reader was stricter than BDB, demonstrating why validation and compatibility were considered together.

Source 1 · Source 2 · Source 3

The earlier overflow-length checks were explicitly introduced to prevent circular page references from causing indefinite traversal. The historical alternatives were tracking visited pages or validating existing format metadata: total record length for overflow chains and descending levels for the B-tree.

The pinned base implements the metadata-based approach: item_len is bounded by a file-derived maximum, accumulated overflow data may not exceed item_len, and B-tree traversal checks expected levels. The current change concerns the progress assumption underlying that existing overflow bound; it does not introduce a new application-level record-size policy.

Source 1 · Source 2

Compatibility with actual old wallets required recognizing overflow pages as a distinct structure, rather than applying every ordinary B-tree invariant to them.

During the original reader's development, testing old wallets revealed that overflow pages use level zero. The resulting distinction remains in the pinned PageHeader validation, alongside the overflow-page-type check. Likewise, OverflowPage interprets hf_offset as payload length. This history explains why the affected behavior must be understood in terms of overflow-page semantics, not generic B-tree pages.

Source 1 · Source 2

Ralph's inferences

A cumulative-byte limit also serves as a traversal bound only when each traversed page increases that cumulative count. Requiring nonempty payloads makes that progress condition explicit.

In both pinned versions, overflow traversal follows next_page until zero and compares accumulated bytes against item_len. At the head, the new rejection occurs before appending an empty payload. This connects the behavior change to the historically documented termination objective without establishing that all BDB-compatible files satisfy the new condition.

Source 1