From 31a76b7a00bc78f7530303f3e3743443bd4e2077 Mon Sep 17 00:00:00 2001 From: Robert burner Schadek Date: Tue, 5 May 2026 16:58:48 +0200 Subject: [PATCH] Add assert for zero divisor in BigInt divMod to prevent infinite loop Fixes #10784 --- std/bigint.d | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/std/bigint.d b/std/bigint.d index 7fea64cd6d5..bb95725b88e 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -2293,6 +2293,7 @@ unittest */ void divMod(const BigInt dividend, const BigInt divisor, out BigInt quotient, out BigInt remainder) pure nothrow @safe { + assert(!divisor.isZero(), "BigInt division by zero"); BigUint q, r; BigUint.divMod(dividend.data, divisor.data, q, r); quotient.sign = dividend.sign != divisor.sign; @@ -2352,6 +2353,26 @@ void divMod(const BigInt dividend, const BigInt divisor, out BigInt quotient, ou assert(remainder == 0); } +// https://github.com/dlang/phobos/issues/10784 +@system unittest +{ + import core.exception : AssertError; + BigInt quotient, remainder; + bool caught; + try + divMod(BigInt("1234"), BigInt(0), quotient, remainder); + catch (AssertError) + caught = true; + assert(caught, "divMod by zero should assert"); +} + +// https://github.com/dlang/phobos/issues/10784 +@safe pure unittest +{ + import std.exception : assertThrown; + assertThrown(BigInt(100) / BigInt(0)); +} + // https://issues.dlang.org/show_bug.cgi?id=19740 @safe unittest {