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

pull brunoerg wants to merge 2 commits into bitcoin:master from brunoerg:2026-10-fuzz-spend-availablecoins-null-coincontrol changing 3 files +31 −2
  1. brunoerg commented at 5:21 PM on November 13, 2026: 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 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.

  2. wallet: guard null coin control in AvailableCoins TRUC filter
    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 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.
    
    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.
    6a5577f100
  3. fuzz: cover unconfirmed outputs and null coin control in spend target
    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.
    032be32485
  4. DrahtBot added the label Wallet on Nov 13, 2026
  5. DrahtBot commented at 5:21 PM on November 13, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    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 <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #35716 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35716.svg"></sub> (wallet: Replace mapWallet and wtxOrdered with a boost::multi_index by achow101)

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

  6. w0xlt commented at 8:14 PM on November 13, 2026: contributor

    Concept ACK

  7. Husteem commented at 1:04 PM on November 14, 2026: none

    tACK 032be32485d6f1ab386196513207c28daab6952c Tested on Ubuntu.

    Left one non-blocking nit inline

  8. in src/wallet/test/spend_tests.cpp:82 in 032be32485
      76 | @@ -77,6 +77,17 @@ BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
      77 |      BOOST_CHECK_EQUAL(fee, check_tx(fee + 123));
      78 |  }
      79 |  
      80 | +BOOST_FIXTURE_TEST_CASE(available_unconfirmed_coins_without_coin_control, TestChain100Setup)
      81 | +{
      82 | +    CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
    


    Conradgabe commented at 10:27 PM on November 18, 2026:

    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?

  9. in src/wallet/test/spend_tests.cpp:88 in 032be32485
      83 | +    auto wallet{CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey)};
      84 | +    const CScript wallet_script{WITH_LOCK(wallet->cs_wallet, return GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::BECH32, ""))))};
      85 | +    BOOST_REQUIRE(wallet->AddToWallet(MakeTransactionRef(CreateValidMempoolTransaction(m_coinbase_txns[0], /*input_vout=*/0, /*input_height=*/0, coinbaseKey, wallet_script)), TxStateInMempool{}));
      86 | +
      87 | +    LOCK(wallet->cs_wallet);
      88 | +    BOOST_CHECK_NO_THROW(AvailableCoins(*wallet));
    


    Conradgabe commented at 10:45 PM on November 18, 2026:

    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);
    
  10. Conradgabe commented at 12:31 AM on November 19, 2026: contributor

    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.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-19 19:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me