diff --git a/src/arith_uint256.h b/src/arith_uint256.h --- a/src/arith_uint256.h +++ b/src/arith_uint256.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -28,11 +29,19 @@ public: base_uint() { + static_assert( + BITS / 32 > 0 && BITS % 32 == 0, + "Template parameter BITS must be a positive multiple of 32."); + for (int i = 0; i < WIDTH; i++) pn[i] = 0; } base_uint(const base_uint &b) { + static_assert( + BITS / 32 > 0 && BITS % 32 == 0, + "Template parameter BITS must be a positive multiple of 32."); + for (int i = 0; i < WIDTH; i++) pn[i] = b.pn[i]; } @@ -44,6 +53,10 @@ } base_uint(uint64_t b) { + static_assert( + BITS / 32 > 0 && BITS % 32 == 0, + "Template parameter BITS must be a positive multiple of 32."); + pn[0] = (unsigned int)b; pn[1] = (unsigned int)(b >> 32); for (int i = 2; i < WIDTH; i++) @@ -146,7 +159,7 @@ base_uint &operator++() { // prefix operator int i = 0; - while (++pn[i] == 0 && i < WIDTH - 1) + while (i < WIDTH && ++pn[i] == 0) i++; return *this; } @@ -161,7 +174,7 @@ base_uint &operator--() { // prefix operator int i = 0; - while (--pn[i] == (uint32_t)-1 && i < WIDTH - 1) + while (i < WIDTH && --pn[i] == std::numeric_limits::max()) i++; return *this; } diff --git a/src/arith_uint256.cpp b/src/arith_uint256.cpp --- a/src/arith_uint256.cpp +++ b/src/arith_uint256.cpp @@ -14,6 +14,9 @@ template base_uint::base_uint(const std::string &str) { + static_assert(BITS / 32 > 0 && BITS % 32 == 0, + "Template parameter BITS must be a positive multiple of 32."); + SetHex(str); }