[Issue 16264] BigInt multiplication crashes on 64-bit (biguintnoasm.d(276): Range violation)

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jul 11 07:33:44 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16264

--- Comment #2 from Kirill Kryukov <kkryukov at gmail.com> ---
Since this crash occurs with particularly large operands, I took a look at the
dependence of this bug upon operand sizes. D's BigInt (and BigUint) is
represented internally as an array of uint (regardless of whether it's a 32-bit
and 64-bit compile).

The initial bug-report is with operand of 52 and 82 uints. I took a look at
other nearby sizes, and noticed that this bug lives in a narrow sector starting
around operand sizes 48 and 74 (in uints). The sector starts there and grows
wider as it goes:

http://kirr.homeunix.org/temp/d-bug-16264-operand-size-table.png

Code used to fill this table (compile a.d into a.exe, then run test.pl):

===== a.d start =====
import std.conv: to;
import std.internal.math.biguintcore;

void main(string[] args)
{
    uint alen = to!uint(args[1]);
    uint blen = to!uint(args[2]);
    string unitstr = args[3];

    string astr = ``, bstr = ``;
    for (int i = 0; i < alen; i++) astr ~= unitstr;
    for (int i = 0; i < blen; i++) bstr ~= unitstr;

    BigUint a, b;
    a.fromHexString(astr);
    b.fromHexString(bstr);

    assert(a.uintLength() == alen);
    assert(b.uintLength() == blen);

    BigUint c = BigUint.mul(a,b);  // Crashes
}
===== a.d end =====

===== test.pl start =====
my ($mina,$maxa,$minb,$maxb) = (40,80,70,120);
print '    ';
for (my $b = $minb; $b <= $maxb; $b++) { printf " %3d", $b; }
print "\n";
for (my $a = $mina; $a <= $maxa; $a++)
{
    printf " %3d", $a;
    for (my $b = $minb; $b <= $maxb; $b++)
    {
        my $e = system("a.exe $a $b FFFFFFFF 2>nul");
        print '   ', ($e ? 1 : 0);
    }
    print "\n";
}
===== test.pl end =====

I also checked 32-bit compiles - no crashes with any of these operand sizes.

--


More information about the Digitalmars-d-bugs mailing list