changeset 3188:876c185d433b draft

Merge pull request #1497 from luke-jr/bugfix_neguint CBigNum: Convert negative int64 values in a more well-defined way
author Gregory Maxwell <greg@xiph.org>
date Wed, 11 Jul 2012 16:56:18 -0700
parents bf05196d4f7f (current diff) 07d49de16f88 (diff)
children ac9abbf669b5
files src/bignum.h
diffstat 1 files changed, 3 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/bignum.h
+++ b/src/bignum.h
@@ -131,15 +131,9 @@
 
         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;