util: Return uint64_t from _MiB operator #35097

pull maflcko wants to merge 1 commits into bitcoin:master from maflcko:2604-u64 changing 3 files +25 −6
  1. maflcko commented at 10:34 AM on April 17, 2026: member

    Currently, the _MiB literal operator returns a value of type size_t. This is brittle and confusing:

    • It is inherently impossible to represent larger values, like storage device usage.
    • Similarly, it is not possible to even type an upper cap on the memory, see the failure in #34692 (comment)
    • Using size_t isn't required here. The function is evaluated at compile time, and even 32-bit compilers can evaluate an uint64_t at compile time.
    • Using size_t here encourages it to be used in more places, which will likely lead to more bugs and CVEs, such as #34109, #33724, etc.

    Fix all issues, by:

    • Marking the operator consteval, to ensure it is really only called at compile-time.
    • Returning an uint64_t value.
    • Using it in the place where it was previously not possible.

    Review note:

    This should have no downside, because the C++11 narrowing checks continue to work as expected. For example, typing uint8_t{1_MiB}; will continue to fail with the correct error message error: constant expression evaluates to 1048576 which cannot be narrowed to type 'uint8_t' (aka 'unsigned char') [-Wc++11-narrowing] (like it does on current master).

  2. DrahtBot added the label Utils/log/libs on Apr 17, 2026
  3. maflcko added the label Refactoring on Apr 17, 2026
  4. DrahtBot commented at 10:34 AM on April 17, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    Concept ACK l0rinc, hodlinator, kevkevinpal

    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:

    • #34641 (node: scale default -dbcache with system RAM by l0rinc)
    • #34435 (refactor: use _MiB/_GiB consistently for byte conversions by l0rinc)

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

  5. util: Return uint64_t from _MiB operator faeabdd21f
  6. maflcko force-pushed on Apr 17, 2026
  7. DrahtBot added the label CI failed on Apr 17, 2026
  8. DrahtBot commented at 11:19 AM on April 17, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task No wallet: https://github.com/bitcoin/bitcoin/actions/runs/24560728284/job/71810830710</sub> <sub>LLM reason (✨ experimental): CI failed to build due to C++ template parameter mismatch errors in util_tests.cpp (invalid template-template argument/static_assert compilation error).</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  9. maflcko commented at 11:21 AM on April 17, 2026: member

    Force pushed to make the older clang happy:

    -template <template <class, auto> class Op, class Int, auto v>
    +template <template <class, auto...> class Op, class Int, auto v>
    

    Godbolt: https://godbolt.org/z/ex1KEo5a3

  10. DrahtBot removed the label CI failed on Apr 17, 2026
  11. l0rinc commented at 1:58 PM on April 17, 2026: contributor

    Concept ACK, had quite a few problems because of this before. Personally I would prefer #34435 being merged first and _GiB also adjusted accordingly - that could reveal the advantage (or disadvantage) of this conversion better. But I will adapt if this will be accepted first - will review in detail later.

  12. maflcko commented at 2:23 PM on April 17, 2026: member

    Personally I would prefer #34435 being merged first and _GiB also adjusted accordingly - that could reveal the advantage (or disadvantage) of this conversion better.

    I think the advantages should be clear from the pull description and the tests, but happy to wait for the other pull. It is just a size_t->uint64_t change in two or three lines, so should be trivial to rebase one way or the other. Anything should be fine, from my side.

  13. hodlinator commented at 2:51 PM on April 17, 2026: contributor

    Concept ACK

  14. kevkevinpal commented at 9:01 PM on April 17, 2026: contributor

    Concept ACK

    It makes sense to return uint64_t instead of size_t and this code change is pretty minimal and scoped to just a few files.


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-04-21 09:12 UTC

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