Added static_assert to check that base_blob is using whole bytes. #27929

pull Brotcrunsher wants to merge 1 commits into bitcoin:master from Brotcrunsher:base_blob_whole_byte changing 1 files +1 −0
  1. Brotcrunsher commented at 11:32 PM on June 21, 2023: contributor

    Prior to this commit it was possible to create base_blobs with any arbitrary amount of bits, like base_blob<9>. One could assume that this would be a valid way to create a bit field that guarantees to have at least 9 bits. However, in such a case, base_blob would not behave as expected because the WIDTH is rounded down to the closest whole byte (simple integer division by 8). This commit makes sure that this oddity is detected and blocked by the compiler.

  2. Added static_assert to check that base_blob is using whole bytes.
    Prior to this commit it was possible to create base_blobs with any arbitrary amount of bits, like base_blob<9>. One could assume that this would be a valid way to create a bit field that guarantees to have at least 9 bits. However, in such a case, base_blob would not behave as expected because the WIDTH is rounded down to the closest whole byte (simple integer division by 8). This commit makes sure that this oddity is detected and blocked by the compiler.
    5fc4939e17
  3. DrahtBot commented at 11:32 PM on June 21, 2023: 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
    ACK MarcoFalke, theStack, stickies-v
    Concept ACK Ayush170-Future

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #27551 (Reduce calls to various SetNull methods by Bushstar)

    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.

  4. maflcko commented at 6:06 AM on June 22, 2023: member

    lgtm ACK 5fc4939e17509534eb36727b27ac0afb941e44f7

  5. theStack approved
  6. theStack commented at 12:09 PM on June 22, 2023: contributor

    ACK 5fc4939e17509534eb36727b27ac0afb941e44f7

    I doubt that we would ever instantiate base_blob with a template parameter that is not a multiple of 8, but of course it doesn't hurt to check this.

    Tested with

    diff --git a/src/uint256.cpp b/src/uint256.cpp
    index 7f81c3c448..802f6d5642 100644
    --- a/src/uint256.cpp
    +++ b/src/uint256.cpp
    @@ -63,6 +63,9 @@ template std::string base_blob<160>::ToString() const;
     template void base_blob<160>::SetHex(const char*);
     template void base_blob<160>::SetHex(const std::string&);
    
    +// Test [#27929](/bitcoin-bitcoin/27929/)
    +template std::string base_blob<9>::GetHex() const;
    +
     // Explicit instantiations for base_blob<256>
     template std::string base_blob<256>::GetHex() const;
     template std::string base_blob<256>::ToString() const;
    
    

    leading to the failed static_assert as expected:

    In file included from uint256.cpp:6:
    ./uint256.h:25:5: error: static_assert failed due to requirement '9U % 8 == 0' "base_blob currently only supports whole bytes."
        static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
        ^             ~~~~~~~~~~~~~
    uint256.cpp:67:22: note: in instantiation of template class 'base_blob<9>' requested here
    template std::string base_blob<9>::GetHex() const;
                         ^
    1 error generated.
    
  7. Ayush170-Future approved
  8. Ayush170-Future commented at 10:44 PM on June 22, 2023: contributor

    Good find! LGTM ACK.

  9. stickies-v approved
  10. stickies-v commented at 11:10 AM on June 27, 2023: contributor

    ACK 5fc4939e17509534eb36727b27ac0afb941e44f7

  11. fanquake merged this on Jun 27, 2023
  12. fanquake closed this on Jun 27, 2023

  13. sidhujag referenced this in commit 8f94ebcf27 on Jun 27, 2023
  14. bitcoin locked this on Jun 26, 2024

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-26 06:13 UTC

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