diff --git a/std/bigint.d b/std/bigint.d index a8b38979597..7fea64cd6d5 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -265,10 +265,11 @@ public: } else static if (op=="*") { - if (y == 0) + if (y == 0 || data.isZero()) { sign = false; data = 0UL; + return this; } else { @@ -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 { diff --git a/std/internal/math/biguintcore.d b/std/internal/math/biguintcore.d index 9c794b60de1..5b6252fcc65 100644 --- a/std/internal/math/biguintcore.d +++ b/std/internal/math/biguintcore.d @@ -2785,6 +2785,10 @@ do adjustRemainder(quotient[0 .. k], u[0 .. v.length], v, k, scratch[0 .. 2 * k]); } + () @trusted { + if (!__ctfe) + GC.free(scratchbuff.ptr); + } (); } // rem -= quot * v[0 .. k]. @@ -2842,7 +2846,10 @@ pure nothrow @safe m -= v.length; } recursiveDivMod(quotient[0 .. m], u[0 .. m + v.length], v, scratch); - () @trusted { GC.free(scratch.ptr); } (); + () @trusted { + if (!__ctfe) + GC.free(scratch.ptr); + } (); } @system unittest