From 5fc4939e17509534eb36727b27ac0afb941e44f7 Mon Sep 17 00:00:00 2001 From: Brotcrunsher Date: Thu, 22 Jun 2023 01:31:06 +0200 Subject: [PATCH] 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. --- src/uint256.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/uint256.h b/src/uint256.h index 1cc3721487..dc8887287a 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -22,6 +22,7 @@ class base_blob { protected: static constexpr int WIDTH = BITS / 8; + static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes."); std::array m_data; static_assert(WIDTH == sizeof(m_data), "Sanity check");