Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1365 from tsbockman/issue_14785
Browse files Browse the repository at this point in the history
Fix Issue 14785 - Some corner cases are not handled properly by core.checkedint.
  • Loading branch information
DmitryOlshansky committed Sep 6, 2015
2 parents 3e3426f + 799a689 commit acb8611
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/core/checkedint.d
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ long subs(long x, long y, ref bool overflow)
{
long r = cast(ulong)x - cast(ulong)y;
if (x < 0 && y >= 0 && r >= 0 ||
x >= 0 && y < 0 && r < 0 ||
y == long.min)
x >= 0 && y < 0 && (r < 0 || y == long.min))
overflow = true;
return r;
}
Expand All @@ -233,6 +232,8 @@ unittest
assert(!overflow);
assert(subs(long.min + 1, 1, overflow) == long.min);
assert(!overflow);
assert(subs(-1L, long.min, overflow) == long.max);
assert(!overflow);
assert(subs(long.max, -1, overflow) == long.min);
assert(overflow);
overflow = false;
Expand Down Expand Up @@ -415,7 +416,8 @@ pragma(inline, true)
long muls(long x, long y, ref bool overflow)
{
long r = cast(ulong)x * cast(ulong)y;
if (x && (r / x) != y)
enum not0or1 = ~1L;
if((x & not0or1) && ((r == y)? r : (r / x) != y))
overflow = true;
return r;
}
Expand All @@ -434,6 +436,9 @@ unittest
assert(muls(long.max, 2L, overflow) == (long.max * 2));
assert(overflow);
overflow = false;
assert(muls(-1L, long.min, overflow) == long.min);
assert(overflow);
overflow = false;
assert(muls(long.min, -1L, overflow) == long.min);
assert(overflow);
assert(muls(0L, 0L, overflow) == 0);
Expand Down

0 comments on commit acb8611

Please sign in to comment.