Skip to content

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

Open
brunoerg wants to merge 2 commits into
bitcoin:masterfrom
brunoerg:2026-10-fuzz-spend-availablecoins-null-coincontrol
Open

brunoerg wants to merge 2 commits into
bitcoin:masterfrom
brunoerg:2026-10-fuzz-spend-availablecoins-null-coincontrol

Conversation

@brunoerg

Copy link
Copy Markdown
Contributor

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 ec2676b 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.

l0rinc and others added 2 commits November 12, 2026 15:50
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 ec2676b does not, so a call without coin control
dereferences a null pointer as soon as the wallet has an unconfirmed
output in the mempool.

No current caller hits this: RPCs, the GUI and CreateTransaction always
pass a coin control, and the tests and benchmarks that use the default
only have confirmed coins.

Treat a missing coin control like one that does not request a TRUC
transaction, and add a test that calls AvailableCoins() with the default
argument on a wallet with an unconfirmed transaction.
Add wallet transactions in the mempool state with a fuzzed version, and
call AvailableCoins() directly with an optional null coin control, an
optional fuzzed feerate and fuzzed filter params, including
min_sum_amount and max_count to reach the early-return paths.
@DrahtBot

DrahtBot commented Nov 13, 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.

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.

@w0xlt

w0xlt commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@Husteem

Husteem commented Nov 14, 2026

Copy link
Copy Markdown

tACK 032be32
Tested on Ubuntu.

Left one non-blocking nit inline


BOOST_FIXTURE_TEST_CASE(available_unconfirmed_coins_without_coin_control, TestChain100Setup)
{
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

BOOST_REQUIRE(wallet->AddToWallet(MakeTransactionRef(CreateValidMempoolTransaction(m_coinbase_txns[0], /*input_vout=*/0, /*input_height=*/0, coinbaseKey, wallet_script)), TxStateInMempool{}));

LOCK(wallet->cs_wallet);
BOOST_CHECK_NO_THROW(AvailableCoins(*wallet));

@Conradgabe Conradgabe Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Suggested change
BOOST_CHECK_NO_THROW(AvailableCoins(*wallet));
BOOST_CHECK_EQUAL(AvailableCoins(*wallet).Size(), 1U);

@Conradgabe

Copy link
Copy Markdown
Contributor

tACK 032be32

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants