Skip to content

Commit

Permalink
Fix #10565: Prevent negative zero when multiplying BigInt zero by neg… (
Browse files Browse the repository at this point in the history
  • Loading branch information
dhawal543 authored Mar 2, 2025
1 parent 6722d0a commit 4b39202
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion std/bigint.d
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ public:
}
else static if (op=="*")
{
if (y == 0)
if (y == 0 || data.isZero())
{
sign = false;
data = 0UL;
return this;
}
else
{
Expand Down Expand Up @@ -361,6 +362,29 @@ public:
return this;
}

// https://issues.dlang.org/show_bug.cgi?id=10565
@safe unittest
{
// Test cases from the issue
BigInt a = BigInt("0");
BigInt b = BigInt("-0");
BigInt c = BigInt("0") * -1;
BigInt d = BigInt("0") * -42;
BigInt e = BigInt("0"); e *= -1;
BigInt f = BigInt(c);
BigInt g = BigInt("0") * cast(byte) -1;
BigInt h = BigInt("0"); h *= BigInt("-1");
BigInt i = BigInt("0"); i -= 2 * i;
BigInt j = BigInt("0"); j = -j;
// All of these should be zero and not negative
auto values = [a, b, c, d, e, f, g, h, i, j];
foreach (val; values)
{
assert(val == 0, "BigInt value should be equal to zero");
assert(!(val < 0), "BigInt zero should not be negative");
}
}

///
@safe unittest
{
Expand Down

0 comments on commit 4b39202

Please sign in to comment.