aboutsummaryrefslogtreecommitdiff
path: root/src/bignum.h
diff options
context:
space:
mode:
authorLuke Dashjr <luke-jr+git@utopios.org>2012-06-18 20:35:10 +0000
committerLuke Dashjr <luke-jr+git@utopios.org>2012-07-22 21:10:19 +0000
commit7a161e48470ad3bfe1a4f497a7f0a1bed2f2b8a1 (patch)
treecea1491ea8ed5cbce704feab5a89f4c9ba8bc7b0 /src/bignum.h
parent3bd1d6645ee39bb6a6e7b016d4cbfc15188b1a00 (diff)
downloadbitcoin-7a161e48470ad3bfe1a4f497a7f0a1bed2f2b8a1.tar.xz
CBigNum: Convert negative int64 values in a more well-defined way
Since the minimum signed integer cannot be represented as positive so long as its type is signed, and it's not well-defined what happens if you make it unsigned before negating it, we instead increment the negative integer by 1, convert it, then increment the (now positive) unsigned integer by 1 to compensate
Diffstat (limited to 'src/bignum.h')
-rw-r--r--src/bignum.h12
1 files changed, 3 insertions, 9 deletions
diff --git a/src/bignum.h b/src/bignum.h
index 1c1c1fc10f..ef8423af9e 100644
--- a/src/bignum.h
+++ b/src/bignum.h
@@ -130,15 +130,9 @@ public:
if (sn < (int64)0)
{
- // We negate in 2 steps to avoid signed subtraction overflow,
- // i.e. -(-2^63), which is an undefined operation and causes SIGILL
- // when compiled with -ftrapv.
- //
- // Note that uint64_t n = sn, when sn is an int64_t, is a
- // well-defined operation and n will be equal to sn + 2^64 when sn
- // is negative.
- n = sn;
- n = -n;
+ // Since the minimum signed integer cannot be represented as positive so long as its type is signed, and it's not well-defined what happens if you make it unsigned before negating it, we instead increment the negative integer by 1, convert it, then increment the (now positive) unsigned integer by 1 to compensate
+ n = -(sn + 1);
+ ++n;
fNegative = true;
} else {
n = sn;