Added static_assert to check that base_blob is using whole bytes.#27929
Added static_assert to check that base_blob is using whole bytes.#27929fanquake merged 1 commit intobitcoin:masterfrom
Conversation
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.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
|
lgtm ACK 5fc4939 |
There was a problem hiding this comment.
ACK 5fc4939
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
+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.
Ayush170-Future
left a comment
There was a problem hiding this comment.
Good find! LGTM ACK.
…sing whole bytes. 5fc4939 Added static_assert to check that base_blob is using whole bytes. (Brotcrunsher) Pull request description: 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. ACKs for top commit: MarcoFalke: lgtm ACK 5fc4939 theStack: ACK 5fc4939 stickies-v: ACK 5fc4939 Tree-SHA512: 6a06760f09d4a9e6f0b9338d4dddd4091f2ac59a843a443d9302959936d72c55f7cccd55a51ec3a5a799921f68be1b87968ef3c9c11d3389cbd369b5045bb50a
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.