test: Inline incorrect check in `util_tests` #35689

pull rustaceanrob wants to merge 1 commits into bitcoin:master from rustaceanrob:26-7-9-underflow changing 1 files +1 −2
  1. rustaceanrob commented at 8:37 AM on July 9, 2026: member

    Assigning ToIntegral<int64_t>("-1") to the optional<uint64_t> n is a silent underflow. BOOST_CHECK_EQUAL then promotes int to uint64_t, which also underflows. The correct check is to do this inline. Part of clean up in #35587

    <details> <summary>gdb</summary>

    (gdb) next
    886	    n = ToIntegral<int64_t>("-1", 16);
    1: n = {<std::_Optional_base<unsigned long, true, true>> = {<std::_Optional_base_impl<unsigned long, std::_Optional_base<unsigned long, true, true> >> = {<No data fields>}, _M_payload = {<std::_Optional_payload_base<unsigned long>> = {_M_payload = {
              _M_empty = {<No data fields>}, _M_value = 18446744073709551615},
            _M_engaged = true}, <No data fields>}}, <std::_Enable_copy_move<true, true, true, true, std::optional<unsigned long> >> = {<No data fields>}, <No data fields>}
    2: n = {<std::_Optional_base<unsigned long, true, true>> = {<std::_Optional_base_impl<unsigned long, std::_Optional_base<unsigned long, true, true> >> = {<No data fields>}, _M_payload = {<std::_Optional_payload_base<unsigned long>> = {_M_payload = {
              _M_empty = {<No data fields>}, _M_value = 18446744073709551615},
            _M_engaged = true}, <No data fields>}}, <std::_Enable_copy_move<true, true, true, true, std::optional<unsigned long> >> = {<No data fields>}, <No data fields>}
    (gdb) next
    887	    BOOST_CHECK_EQUAL(*n, -1);
    1: n = {<std::_Optional_base<unsigned long, true, true>> = {<std::_Optional_base_impl<unsigned long, std::_Optional_base<unsigned long, true, true> >> = {<No data fields>}, _M_payload = {<std::_Optional_payload_base<unsigned long>> = {_M_payload = {
              _M_empty = {<No data fields>}, _M_value = 18446744073709551615},
            _M_engaged = true}, <No data fields>}}, <std::_Enable_copy_move<true, true, true, true, std::optional<unsigned long> >> = {<No data fields>}, <No data fields>}
    2: n = {<std::_Optional_base<unsigned long, true, true>> = {<std::_Optional_base_impl<unsigned long, std::_Optional_base<unsigned long, true, true> >> = {<No data fields>}, _M_payload = {<std::_Optional_payload_base<unsigned long>> = {_M_payload = {
              _M_empty = {<No data fields>}, _M_value = 18446744073709551615},
            _M_engaged = true}, <No data fields>}}, <std::_Enable_copy_move<true, true, true, true, std::optional<unsigned long> >> = {<No data fields>}, <No data fields>}
    

    </details>

  2. DrahtBot added the label Tests on Jul 9, 2026
  3. DrahtBot commented at 8:37 AM on July 9, 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/35689.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    Type Reviewers
    ACK maflcko, 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-->

  4. in src/test/util_tests.cpp:887 in fa1cab673d
     882 | @@ -883,8 +883,8 @@ BOOST_AUTO_TEST_CASE(test_ToIntegralHex)
     883 |      BOOST_CHECK_EQUAL(*n, 0);
     884 |      n = ToIntegral<uint64_t>("FfFfFfFfFfFfFfFf", 16);
     885 |      BOOST_CHECK_EQUAL(*n, 0xFfFfFfFfFfFfFfFfULL);
     886 | -    n = ToIntegral<int64_t>("-1", 16);
     887 | -    BOOST_CHECK_EQUAL(*n, -1);
     888 | +    auto signed_n = ToIntegral<int64_t>("-1", 16);
     889 | +    BOOST_CHECK_EQUAL(*signed_n, -1);
    


    maflcko commented at 11:03 AM on July 9, 2026:

    style-nit: I find it a bit ugly to re-use state between test cases and my recommendation would still be to use BOOST_CHECK_EQUAL(*ToIntegral<int64_t>("-1", 16), -1); (from #34242 (review))

    For anyone wondering why not sanitizer is complaining: Apparently both std::optional and BOOST_CHECK_EQUAL close both eyes (from the same review thread in #34242 (review))


    sedited commented at 11:09 AM on July 9, 2026:

    Would also prefer this.


    rustaceanrob commented at 11:46 AM on July 9, 2026:

    Adopted in cd2a4bc5101ea28661a3fa3a70b6735b68ae70fc

  5. maflcko approved
  6. maflcko commented at 11:05 AM on July 9, 2026: member

    Thx, nice change!

    It should be simple to avoid any dirty state between test cases and avoid the std::optional::operator=() by just in-lining the test cases, so that the same (and other) issues does not happen again in the future.

    but this lgtm.

    ACK fa1cab673d4f5c6943249cfd5da01a025f113ff9

  7. test: Redeclare variable as signed in `util_tests`
    Assigning `ToIntegral<int64_t>("-1")` to the `optional<uint64_t>` `n`
    is a silent underflow. `BOOST_CHECK_EQUAL` then promotes `int` to
    `uint64_t`, which also underflows. The correct check is to do this
    inline.
    cd2a4bc510
  8. rustaceanrob force-pushed on Jul 9, 2026
  9. rustaceanrob renamed this:
    test: Redeclare variable as signed in `util_tests`
    test: Inline incorrect check in `util_tests`
    on Jul 9, 2026
  10. maflcko commented at 11:57 AM on July 9, 2026: member

    review ACK cd2a4bc5101ea28661a3fa3a70b6735b68ae70fc 🎬

    <details><summary>Show signature</summary>

    Signature:

    untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
    RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
    trusted comment: review ACK cd2a4bc5101ea28661a3fa3a70b6735b68ae70fc 🎬
    UXNRx0gq+6iXl9myTblyRHz4r4wAiPRD5in+P5eirU6dphRcuefnsu6OsSsfYiT1/NBmdtwOpOMourI+kdzNBw==
    

    </details>

  11. sedited approved
  12. sedited commented at 12:13 PM on July 9, 2026: contributor

    ACK cd2a4bc5101ea28661a3fa3a70b6735b68ae70fc

  13. sedited merged this on Jul 9, 2026
  14. sedited closed this on Jul 9, 2026

  15. oleonardolima referenced this in commit 685fdce9a1 on Jul 9, 2026
  16. rustaceanrob deleted the branch on Jul 9, 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-07-11 18:51 UTC

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