diff options
author | Ricardo M. Correia <rcorreia@wizy.org> | 2012-05-14 02:50:01 +0200 |
---|---|---|
committer | Luke Dashjr <luke-jr+git@utopios.org> | 2012-06-18 19:01:46 +0000 |
commit | 7ff54e08aa42f10dae424711a0a3764a67eae295 (patch) | |
tree | 0495aacc357cdabcca24b6d6ad9608b18c2db3cf /src | |
parent | fa57170187415bf939885fc71cea5bffc30a32f8 (diff) | |
download | bitcoin-7ff54e08aa42f10dae424711a0a3764a67eae295.tar.xz |
Don't overflow signed ints in CBigNum::setint64().
CBigNum::setint64() does 'n <<= 8', where n is of type "long long".
This leads to shifting onto and past the sign bit, which is undefined
behavior in C++11 and can cause problems in the future.
Diffstat (limited to 'src')
-rw-r--r-- | src/bignum.h | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/bignum.h b/src/bignum.h index 4a3fb38b00..e203b26a05 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -121,16 +121,22 @@ public: return (n > std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n); } - void setint64(int64 n) + void setint64(int64 sn) { - unsigned char pch[sizeof(n) + 6]; + unsigned char pch[sizeof(sn) + 6]; unsigned char* p = pch + 4; - bool fNegative = false; - if (n < (int64)0) + bool fNegative; + uint64 n; + + if (sn < (int64)0) { - n = -n; + n = -sn; fNegative = true; + } else { + n = sn; + fNegative = false; } + bool fLeadingZeroes = true; for (int i = 0; i < 8; i++) { |