Skip to content

Commit

Permalink
Merge #14510: Avoid triggering undefined behaviour in base_uint<BITS>…
Browse files Browse the repository at this point in the history
…::bits()

Summary:
PR14510 backport https://github.com/bitcoin/bitcoin/pull/14510/files
96f6dc9fc50b1cc59e26d50940ebf46e1bdcc0ba Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Test Plan: `make check`

Reviewers: Fabien, #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D3126
  • Loading branch information
laanwj authored and markblundeberg committed May 27, 2019
1 parent 9c76f45 commit f3b09e4
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/arith_uint256.cpp
Expand Up @@ -152,7 +152,7 @@ template <unsigned int BITS> unsigned int base_uint<BITS>::bits() const {
for (int pos = WIDTH - 1; pos >= 0; pos--) {
if (pn[pos]) {
for (int nbits = 31; nbits > 0; nbits--) {
if (pn[pos] & 1 << nbits) {
if (pn[pos] & 1U << nbits) {
return 32 * pos + nbits + 1;
}
}
Expand Down

0 comments on commit f3b09e4

Please sign in to comment.