Skip to content

wallet: Clear streams before advancing BDB cursor - #36871

Open
jaromil wants to merge 1 commit into
bitcoin:masterfrom
jaromil:fix/bdb-cursor-stream-reset
Open

jaromil wants to merge 1 commit into
bitcoin:masterfrom
jaromil:fix/bdb-cursor-stream-reset

Conversation

@jaromil

@jaromil jaromil commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

This fixes a case where migratewallet can reject an old wallet as corrupt, i.e. this occurs on wallets used in Bitcoin Knots. The v2 key metadata may include an extra flag byte that Core’s metadata loader leaves unread.

LoadRecords reuses its key and value streams between records. BerkeleyROCursor::Next() appends to those streams, so unread bytes from one record become part of the next and can cause a deserialization error.

Clear both streams before copying the next record, matching SQLiteCursor::Next(). This keeps the existing metadata parsing and migration code.

The regression test reads two records while leaving part of the first key and value unread, then checks that the second record replaces both outputs.

Testing on Linux:

  • Without the fix, the regression fails both stream-content checks.
  • With the fix, test_bitcoin --run_test=db_tests passes all tests

@DrahtBot

DrahtBot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36167 (build: Enable -Wunused by fanquake)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LoadRecords reuses its key and value streams between cursor calls. Record
loaders may leave optional suffix fields unread, which BerkeleyROCursor
currently appends to the next record. This can cause valid legacy wallets
to fail migration with a deserialization error.

Clear both streams before writing the next record, matching SQLiteCursor.
Document the cursor's replacement contract, add a BerkeleyRO regression
that leaves part of both streams unread, and reuse unread streams in the
SQLite prefix tests to cover the same contract.
@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants