Skip to content

Commit

Permalink
Fix undefined behaviour
Browse files Browse the repository at this point in the history
Shifting negative signed integers is undefined behaviour, so use unsigned integers everywhere.
  • Loading branch information
kimwalisch committed Nov 25, 2017
1 parent 02957fa commit 910c13c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions include/int256_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ class int256_t

bool operator<(const int256_t& other) const
{
return high < other.high ||
(high == other.high && low < other.low);
int128_t hi1 = high;
int128_t hi2 = other.high;

return hi1 < hi2 || (hi1 == hi2 && low < other.low);
}

bool operator<=(const int256_t& other) const
Expand Down Expand Up @@ -157,8 +159,8 @@ class int256_t

if (low <= max64 &&
other.low <= max64 &&
((high == 0 || high == -1) &&
(other.high == 0 || other.high == -1)))
((high == 0 || ~high == 0) &&
(other.high == 0 || ~other.high == 0)))
{
return int256_t(low * other.low,
(high == other.high) ? 0 : -1);
Expand Down Expand Up @@ -466,9 +468,9 @@ class int256_t

private:
uint128_t low;
int128_t high;
uint128_t high;

int256_t(uint128_t low, int128_t high)
int256_t(uint128_t low, uint128_t high)
: low(low)
, high(high)
{
Expand Down

0 comments on commit 910c13c

Please sign in to comment.