wallet: Clear streams before advancing BDB cursor

bitcoin/bitcoin: PR #36871

1 suggestion

Optional improvements

Link

[suggestion] · Cursor regression omits legacy metadata suffix acceptance

The cursor test catches appending but does not protect the legacy loader's acceptance of suffixed metadata; which released software wrote the reported version-2 suffix remains unverified.

Location: src/wallet/test/db_tests.cpp:64 (head).

Details and suggested fix

Both ignore() calls leave one byte unread, so this test covers the cursor bug. However, it never reaches CKeyMetadata or WalletBatch::LoadWallet. Version-2 metadata reads only nVersion and nCreateTime, leaving the suffix unread, and LoadRecords accepts that without requiring exhaustion. Adding a strict exhaustion check there would reject this metadata while all new cursor assertions still passed.

Add a load-level regression using BerkeleyRODatabase(path, /*open=*/false) with two serialized ("keymeta", CPubKey) records in m_records. Give the first record in map order version 2, a creation time, and an extra byte. Pass the database to a CWallet and call PopulateWalletFromDB, following walletload_tests.cpp. Assert LOAD_OK and both creation times in GetLegacyDataSPKM()->mapKeyMetadata under cs_KeyStore. With no flags record, loading takes the legacy path, and the backend's successful no-op writes accommodate automatic upgrades. This tests suffix compatibility as well as record isolation; checking the times also detects shifted decoding that does not throw.


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

Leaving an added metadata suffix unread was explicitly treated as a backward-compatibility mechanism. In the 2015 key-generation metadata proposal, maintainers questioned whether an extra byte required a wallet upgrade; the author reported testing that older software ignored the appended byte while retaining the stored metadata.

At both pinned revisions, CKeyMetadata reads fields according to version thresholds, and LoadRecords does not require each record stream to be exhausted. This historical discussion explains why an unread suffix can represent compatible wallet evolution rather than corruption.

Source 1 · Source 2 · Source 3

The proposed extra metadata byte served a security-relevant distinction: an encrypted wallet can still contain keys originally created while unencrypted. The proposal sought to remember whether keys were generated under encryption or imported, rather than infer their history from the wallet's current encryption state.

This supplies a concrete historical reason for extending individual key-metadata records without changing their basic key/time fields. The current patch leaves those metadata semantics untouched and changes only how the database cursor presents successive records.

Source 1 · Source 2

The independent Berkeley DB reader was introduced to preserve access to legacy wallet data without retaining Berkeley DB as a dependency. The removal plan kept a minimal legacy-data loader specifically so migration could continue after ordinary legacy-wallet support was removed.

BerkeleyROCursor is therefore part of a compatibility bridge, not a new general-purpose wallet storage format. Its outputs feed the retained legacy loaders in walletdb.cpp, which must interpret historical record layouts during migration.

Source 1 · Source 2 · Source 3

Read-only access was intended to avoid modifying the source wallet, but the original discussion distinguished non-mutation from faithful recovery. The author argued that the independent reader would not write as Berkeley DB could; an objection emphasized that an inaccurate dump could still harm someone relying on it as a backup. The author subsequently made its initial wallet-tool use optional.

This frames the historical tradeoff surrounding the backend affected here: preserving the original file and faithfully exposing its records are separate requirements. Replacing cursor output streams concerns record presentation, while the patch retains the existing metadata interpretation.

Source 1 · Source 2 · Source 3

The reader's historical threat model did not assume that a local wallet file was well formed. Discussion called for fuzzing against Berkeley DB, rejected assertions as validation of disk input, and recorded changes to throw exceptions and compare whether the two readers accepted a database.

The migration path must distinguish legitimate historical variation from malformed input. That is compatible with accepting an unused metadata suffix while still validating database structure; suffix tolerance is not a general exemption from input validation.

Source 1 · Source 2 · Source 3 · Source 4

Ralph's inferences

Record isolation separates storage mechanics from schema compatibility: a loader can read the fields it understands without making the next record depend on whether it consumed every byte of the previous one.

This is the design explanation inferred from the pinned code and historical suffix-compatibility premise. LoadRecords reuses key/value streams; SQLiteCursor already clears them; the head applies replacement semantics to BerkeleyROCursor and documents that contract. The boundary at issue is a local database-record boundary, not a Bitcoin consensus or peer-protocol rule.

Source 1 · Source 2