aboutsummaryrefslogtreecommitdiff
path: root/src/bignum.h
diff options
context:
space:
mode:
authorGregory Maxwell <greg@xiph.org>2012-07-11 16:56:18 -0700
committerGregory Maxwell <greg@xiph.org>2012-07-11 16:56:18 -0700
commit3dbca25b69ec9fb7d37191364e696996136e56af (patch)
tree57414432e104d470ffb5bcf0cd4630239517a72f /src/bignum.h
parentff20f323385db9401789047359735c8a44da3351 (diff)
parent0f5a2a82d93f55e1f5ad1c5401b8b5ceeb38b0df (diff)
downloadbitcoin-3dbca25b69ec9fb7d37191364e696996136e56af.tar.xz
Merge pull request #1497 from luke-jr/bugfix_neguint
CBigNum: Convert negative int64 values in a more well-defined way
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 9af934051a..47f6229431 100644
--- a/src/bignum.h
+++ b/src/bignum.h
@@ -131,15 +131,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;