wallet: guard null coin control in AvailableCoins TRUC filter #36859

Open
brunoerg wants to merge 2 commits from 2026-10-fuzz-spend-availablecoins-null-coincontrol into master AGit
brunoerg commented 2026-11-13 17:21:10 +00:00 (Migrated from github.com)

AvailableCoins() takes an optional CCoinControl pointer that defaults to nullptr, and the other uses of it check for null first. The TRUC version filter added in github.com/bitcoin/bitcoin@ec2676becd does not, so a call without coin control dereferences a null pointer as soon as the wallet has an unconfirmed output in the mempool.

Found this issue with few seconds of fuzzing with the change in the second commit which adds wallet transactions in the mempool state with a fuzzed version, and call AvailableCoins() directly with an optional null coin control.

AvailableCoins() takes an optional CCoinControl pointer that defaults to nullptr, and the other uses of it check for null first. The TRUC version filter added in https://github.com/bitcoin/bitcoin/commit/ec2676becdf488f7a1151345a019c05dec926308 does not, so a call without coin control dereferences a null pointer as soon as the wallet has an unconfirmed output in the mempool. Found this issue with few seconds of fuzzing with the change in the second commit which adds wallet transactions in the mempool state with a fuzzed version, and call `AvailableCoins()` directly with an optional null coin control.
DrahtBot commented 2026-11-13 17:21:20 +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.

Type Reviewers
ACK Husteem, Conradgabe
Concept ACK w0xlt

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

<!--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/36859 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36859.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36859#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. | Type | Reviewers | | ---- | --------- | | ACK | [Husteem](https://github.com/bitcoin/bitcoin/pull/36859#issuecomment-6877666050), [Conradgabe](https://github.com/bitcoin/bitcoin/pull/36859#issuecomment-6995851841) | | Concept ACK | [w0xlt](https://github.com/bitcoin/bitcoin/pull/36859#issuecomment-6863760782) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Null-coin-control test leaves coin membership unchecked

The regression test exercises the null call but would also pass a fix that drops all unconfirmed outputs or skips the intended non-TRUC filtering.

Location: src/wallet/test/spend_tests.cpp:88 (head).

🔵 Suggestion · Direct fuzz call leaves TRUC result invariants unchecked

With unsafe inputs enabled, the new fuzz fixture could check TRUC output membership and the single-transaction restriction, but discarding the result leaves those invariants unchecked.

Location: src/wallet/test/fuzz/spend.cpp:108 (head).

Review coverage was incomplete.


Reviewed: 032be32485d6 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:6a916fa059320c1e3f0a79c4f7490636461ad967153b8442c21450419af74010:24633968ff336fb935794e53e586f29b4eaaa2a15575a2c0392a2c8b9ace382c --> <!-- ralph-base:a527a0077a0fcae913188410214efe98021c0cca --> <!-- ralph-head:032be32485d6f1ab386196513207c28daab6952c --> <!-- ralph-case:76f21a241200138a3628b68de6c115862321909dcf65b0c30f4cf364c1f7b396 --> **🔵 Suggestion · Null-coin-control test leaves coin membership unchecked** The regression test exercises the null call but would also pass a fix that drops all unconfirmed outputs or skips the intended non-TRUC filtering. Location: [`src/wallet/test/spend_tests.cpp:88`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/032be32485d6f1ab386196513207c28daab6952c/src/wallet/test/spend_tests.cpp#L88>) (head). **🔵 Suggestion · Direct fuzz call leaves TRUC result invariants unchecked** With unsafe inputs enabled, the new fuzz fixture could check TRUC output membership and the single-transaction restriction, but discarding the result leaves those invariants unchecked. Location: [`src/wallet/test/fuzz/spend.cpp:108`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/032be32485d6f1ab386196513207c28daab6952c/src/wallet/test/fuzz/spend.cpp#L108>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`032be32485d6`](<https://git.fish.foo/bitcoin/bitcoin/commit/032be32485d6f1ab386196513207c28daab6952c>) · [Review report](<https://review.fish.foo/report/36859-032be32485d6f1ab386196513207c28daab6952c-2652-1.html>) · [Previous reports](<https://review.fish.foo/traces/36859/>) [permalink](<https://review.fish.foo/report/36859-032be32485d6f1ab386196513207c28daab6952c-2652-1.html>)
w0xlt commented 2026-11-13 20:14:25 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
Husteem commented 2026-11-14 13:04:30 +00:00 (Migrated from github.com)

tACK 032be32485
Tested on Ubuntu.

Left one non-blocking nit inline

tACK 032be32485d6f1ab386196513207c28daab6952c Tested on Ubuntu. Left one non-blocking nit inline
Conradgabe (Migrated from github.com) reviewed 2026-11-18 23:05:25 +00:00
Conradgabe (Migrated from github.com) commented 2026-11-18 22:27:40 +00:00

nit: The CreateAndProcessBlock line might be unnecessary, the block's coinbase is already spendable and mature at height 100, and without this line, the test still crashes and passes too. Was this on purpose?

nit: The `CreateAndProcessBlock` line might be unnecessary, the block's coinbase is already spendable and mature at height 100, and without this line, the test still crashes and passes too. Was this on purpose?
Conradgabe (Migrated from github.com) commented 2026-11-18 22:45:33 +00:00

nit: Without the BOOST_CHECK_NO_THROW wrap, the test still crashes without the fix and passes with it. I think that's because null dereference isn't an exception, so it wouldn't be caught. You could consider checking the results of AvailableCoins instead, like the size.

(I checked the size was 1 locally)

    BOOST_CHECK_EQUAL(AvailableCoins(*wallet).Size(), 1U);
nit: Without the `BOOST_CHECK_NO_THROW` wrap, the test still crashes without the fix and passes with it. I think that's because null dereference isn't an exception, so it wouldn't be caught. You could consider checking the results of `AvailableCoins` instead, like the size. (I checked the size was 1 locally) ```suggestion BOOST_CHECK_EQUAL(AvailableCoins(*wallet).Size(), 1U); ```
Conradgabe commented 2026-11-19 00:31:29 +00:00 (Migrated from github.com)

tACK 032be32485

tested both the unit test and the fuzz test individually, they crashed without the fix and passed with it. I left two non-blocking inline comments.

Also, I replayed the existing qa-assets corpus and noticed less coverage on this PR compared to master (cov 19380 → 18366, ft 109275 → 67187 on my machine). Since the new ConsumeBool() calls change how the inputs are read, the corpus may need regenerating.

tACK 032be32485d6f1ab386196513207c28daab6952c tested both the unit test and the fuzz test individually, they crashed without the fix and passed with it. I left two non-blocking inline comments. Also, I replayed the existing qa-assets corpus and noticed less coverage on this PR compared to master (cov 19380 → 18366, ft 109275 → 67187 on my machine). Since the new `ConsumeBool()` calls change how the inputs are read, the corpus may need regenerating.
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!36859
No description provided.