From 442f8ce615dfd80bc24787c3624fd578f1bdbc76 Mon Sep 17 00:00:00 2001 From: dhawal543 <137651549+dhawal543@users.noreply.github.com> Date: Sun, 2 Mar 2025 05:05:09 +0530 Subject: [PATCH 1/6] Fix #10565: Prevent negative zero when multiplying BigInt zero by negative value --- std/bigint.d | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/std/bigint.d b/std/bigint.d index a8b38979597..4ad344781c0 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -264,18 +264,19 @@ public: data = BigUint.addOrSubInt!ulong(data, u, wantSub: sign == (y<0), sign); } else static if (op=="*") - { - if (y == 0) - { - sign = false; - data = 0UL; - } - else - { - sign = ( sign != (y<0) ); - data = BigUint.mulInt(data, u); - } - } +{ + if (y == 0 || data.isZero()) + { + data = 0UL; + sign = false; + return this; + } + else + { + sign = ( sign != (y<0) ); + data = BigUint.mulInt(data, u); + } +} else static if (op=="/") { assert(y != 0, "Division by zero"); From a5a3e09e96a586f24cfce92d4aad752046cfa6d1 Mon Sep 17 00:00:00 2001 From: dhawal543 <137651549+dhawal543@users.noreply.github.com> Date: Sun, 2 Mar 2025 06:07:18 +0530 Subject: [PATCH 2/6] Address review --- std/bigint.d | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/std/bigint.d b/std/bigint.d index 4ad344781c0..b314d1bf069 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -264,19 +264,19 @@ public: data = BigUint.addOrSubInt!ulong(data, u, wantSub: sign == (y<0), sign); } else static if (op=="*") -{ - if (y == 0 || data.isZero()) - { - data = 0UL; - sign = false; - return this; - } - else - { - sign = ( sign != (y<0) ); - data = BigUint.mulInt(data, u); - } -} + { + if (y == 0 || data.isZero()) + { + data = 0UL; + sign = false; + return this; + } + else + { + sign = ( sign != (y<0) ); + data = BigUint.mulInt(data, u); + } + } else static if (op=="/") { assert(y != 0, "Division by zero"); From 5e46ed1edcda6ad12107fbb419597a75569448cf Mon Sep 17 00:00:00 2001 From: dhawal543 <137651549+dhawal543@users.noreply.github.com> Date: Sun, 2 Mar 2025 06:21:28 +0530 Subject: [PATCH 3/6] Fix #10565: Prevent negative zero when multiplying BigInt zero by negative value --- std/bigint.d | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/std/bigint.d b/std/bigint.d index b314d1bf069..00b5c62dfe3 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -422,6 +422,31 @@ public: `58105600` )); } +// 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 + BigInt[] test = [a, b, c, d, e, f, g, h, i, j]; + foreach(idx, t; test) { + assert(t == 0, "BigInt should be equal to zero"); + assert(!(t < 0), "BigInt zero should not be negative"); + } +} + + + // https://issues.dlang.org/show_bug.cgi?id=24028 @system unittest From 79e244f72f9bfb4ac54fbe849d838ec3e965d85b Mon Sep 17 00:00:00 2001 From: dhawal543 <137651549+dhawal543@users.noreply.github.com> Date: Sun, 2 Mar 2025 06:44:04 +0530 Subject: [PATCH 4/6] Address review feedback: White Spaces --- std/bigint.d | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/std/bigint.d b/std/bigint.d index 00b5c62dfe3..822cb117aea 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -267,8 +267,8 @@ public: { if (y == 0 || data.isZero()) { - data = 0UL; sign = false; + data = 0UL; return this; } else @@ -422,31 +422,6 @@ public: `58105600` )); } -// 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 - BigInt[] test = [a, b, c, d, e, f, g, h, i, j]; - foreach(idx, t; test) { - assert(t == 0, "BigInt should be equal to zero"); - assert(!(t < 0), "BigInt zero should not be negative"); - } -} - - - // https://issues.dlang.org/show_bug.cgi?id=24028 @system unittest From 9f73fe101b1ec4866faabac9cdf2491164836030 Mon Sep 17 00:00:00 2001 From: dhawal543 <137651549+dhawal543@users.noreply.github.com> Date: Sun, 2 Mar 2025 17:12:46 +0530 Subject: [PATCH 5/6] Address review feedback: unittest --- std/bigint.d | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/std/bigint.d b/std/bigint.d index 822cb117aea..7fea64cd6d5 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -362,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 { From 58890f0c67c815852174cab14a4bf82adbb30e8d Mon Sep 17 00:00:00 2001 From: dhawal543 <137651549+dhawal543@users.noreply.github.com> Date: Tue, 4 Mar 2025 03:22:24 +0530 Subject: [PATCH 6/6] corrected --- std/internal/math/biguintcore.d | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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