wallet: Clear streams before advancing BDB cursor #36871

Open
jaromil wants to merge 1 commit from fix/bdb-cursor-stream-reset into master AGit
jaromil commented 2026-11-14 14:49:46 +00:00 (Migrated from github.com)

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
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 commented 2026-11-14 14:49:55 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36871 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36871.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36871#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36167](https://github.com/bitcoin/bitcoin/pull/36167) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36167.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 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).

Review coverage was incomplete.


Reviewed: 00bb61e694cf · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4afac00dcf2100063250ef321636839508f727ef6ae0cc9ede404a011c46a59f:87c7358e6b1a9709279e41fe01f3f324759c2be7a6239dcb2b0a6d875f8630b0 --> <!-- ralph-base:a527a0077a0fcae913188410214efe98021c0cca --> <!-- ralph-head:00bb61e694cfd8ca77dda806b0509094597ba3d4 --> <!-- ralph-case:004c197dfed1f5ab10516c4e729a191ada2c5a6b59c438c60028cc011f32c974 --> **🔵 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`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/00bb61e694cfd8ca77dda806b0509094597ba3d4/src/wallet/test/db_tests.cpp#L64>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`00bb61e694cf`](<https://git.fish.foo/bitcoin/bitcoin/commit/00bb61e694cfd8ca77dda806b0509094597ba3d4>) · [Review report](<https://review.fish.foo/report/36871-00bb61e694cfd8ca77dda806b0509094597ba3d4-2685-2.html>) · [Previous reports](<https://review.fish.foo/traces/36871/>) [permalink](<https://review.fish.foo/report/36871-00bb61e694cfd8ca77dda806b0509094597ba3d4-2685-2.html>)
DrahtBot commented 2026-11-24 16:45:53 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36871
No description provided.