mini-gmp: mpz_init_set_str fails on leading zeroes

Axel Miller axel.miller at ppi.de
Wed Jul 20 15:34:36 UTC 2016


Hi!

The execution of the following code snippet fails with an assertion 
"mini-gmp.c:502: mpn_mul_1: Assertion `n >= 1' failed.":

    mpz_t v;
    mpz_init_set_str(v, "00000000000000000005", 10);


If I remove the leading zeroes, everythine works fine.

I used mini-gmp.c from GMP 6.1.1.

Output from uname -a and gcc --version:

mil at mil-vm-debian64-1:~/mini-gmp$ uname -a
Linux mil-vm-debian64-1 2.6.32-5-amd64 #1 SMP Tue May 13 16:34:35 UTC 2014 
x86_64 GNU/Linux

mil at mil-vm-debian64-1:~/mini-gmp$ gcc --version
gcc (Debian 4.4.5-8) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR 
PURPOSE.


The error seems to be in function mpn_set_str_other.
With a sufficient number of leading zeroes in sp, the computation in lines 
1314-1317 will assign zero to the variable w.

1314   j = 0;
1315   w = sp[j++];
1316   while (--k != 0)
1317     w = w * b + sp[j++];
1318 

If we have some digits left, j will still be smaller than sn. Thus, the 
loop in line 1321 will be executed at least once:

1321   for (rn = (w > 0); j < sn;)
1322     {
1323       mp_limb_t cy;
1324 
1325       w = sp[j++];
1326       for (k = 1; k < info->exp; k++)
1327     w = w * b + sp[j++];
1328 
1329       cy = mpn_mul_1 (rp, rp, rn, info->bb);
1330       cy += mpn_add_1 (rp, rp, rn, w);
1331       if (cy > 0)
1332     rp[rn++] = cy;
1333     }
1334   assert (j == sn);
1321   for (rn = (w > 0); j < sn;)

Since w is zero, the loop will assing zero to rn at the beginning.
This causes the assertion in mpn_mul_1 (called in line 1329).


HTH
Axel Miller


More information about the gmp-bugs mailing list