kernel: prevent dangling iterators from temporary ranges #35952

pull l0rinc wants to merge 2 commits into bitcoin:master from l0rinc:l0rinc/kernel-range-iterator-lifetime changing 2 files +12 −7
  1. l0rinc commented at 11:47 PM on August 11, 2026: contributor

    Problem: Kernel wrapper methods return Range views by value, but their iterators point to the Range object. Saving an iterator from a temporary view, such as block.Transactions().begin(), leaves it pointing to the destroyed view, so later use has undefined behavior.

    Fix: Make range iterators point to the underlying Kernel wrapper object and use the range's compile-time getter for element access. Remove operator->, which returned elements by value and could not easily support arrow expressions.

  2. DrahtBot added the label Validation on Aug 11, 2026
  3. DrahtBot commented at 11:48 PM on August 11, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35952.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    Concept ACK purpleKarrot
    Stale ACK sedited

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

    LLM Linter (✨ experimental)

    Possible places where comparison-specific test macros should replace generic comparisons:

    • src/test/kernel/test_kernel.cpp BOOST_CHECK_THROW(Block{invalid_data}, std::runtime_error); -> Consider BOOST_CHECK_EXCEPTION(...) with a predicate that checks the exception message, so the failure is more specific than just matching std::runtime_error.

    <sup>2026-08-13 19:42:30</sup>

  4. l0rinc force-pushed on Aug 11, 2026
  5. DrahtBot added the label CI failed on Aug 11, 2026
  6. DrahtBot removed the label CI failed on Aug 12, 2026
  7. sedited approved
  8. sedited commented at 9:56 AM on August 12, 2026: contributor

    ACK cb083e719aa531bf448e5e4631fc6d4f102deba1

    Thanks @l0rinc this seems like a pretty elegant solution.

  9. in src/kernel/bitcoinkernel_wrapper.h:213 in cb083e719a
     208 | @@ -209,8 +209,8 @@ class Iterator
     209 |      Iterator(const Collection* ptr, size_t idx) : m_collection{ptr}, m_idx{idx} {}
     210 |  
     211 |      // This is just a view, so return a copy.
     212 | -    auto operator*() const { return (*m_collection)[m_idx]; }
     213 | -    auto operator->() const { return (*m_collection)[m_idx]; }
     214 | +    auto operator*() const { return std::invoke(GetFunc, *m_collection, m_idx); }
     215 | +    auto operator->() const { return operator*(); }
    


    purpleKarrot commented at 6:05 PM on August 13, 2026:

    operator-> is not implemented correctly. Not your fault; it was broken before. But since you touch that code, you might want to fix it and cover it in a test.

    For operator-> to work correctly, it has to return something that provides operator-> itself. If operator* returns an rvalue, you may need an arrow_proxy.


    l0rinc commented at 7:31 PM on August 13, 2026:

    Good catch, I ended up removing operator-> instead of adding an arrow proxy, the standard specification for transform_view::iterator likewise provides operator* without operator->. Added you as a co-author.

  10. purpleKarrot approved
  11. purpleKarrot commented at 6:05 PM on August 13, 2026: contributor

    Concept ACK. You might want to have a look at operator->.

  12. test: characterize kernel range iterators 0936c55f62
  13. kernel: keep range iterators tied to their owner
    `Range` iterators point to the view, so iterators saved from temporary views dangle.
    Point them to the underlying container, use the range's getter for element access, and remove `operator->`, which returned elements by value and could not support arrow expressions.
    
    Co-authored-by: Daniel Pfeifer <daniel@pfeifer-mail.de>
    fc0dcf950f
  14. l0rinc force-pushed on Aug 13, 2026
  15. l0rinc closed this on Aug 13, 2026

  16. l0rinc reopened this on Aug 13, 2026

  17. DrahtBot added the label CI failed on Aug 13, 2026
  18. DrahtBot removed the label CI failed on Aug 13, 2026

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-08-14 17:51 UTC

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