segfault in mpz_divexact()

Antony Dovgal antony at
Wed Apr 18 15:06:06 CEST 2007

Hello all.

The code below segfaults on Linux x86 using GMP 4.2.1 compiled with GCC 3.3.5.
I know that the function is expected to return correct results only if/when A 
is known to be divisible by B, but I can't find anywhere in the docs that segfault is ok too.
Hence the report.

#include <stdlib.h>
#include <gmp.h>

int main() {
    mpz_t a, b, res;

    mpz_init_set_si(a, 5);
    /* does not segfault */
    /* mpz_init_set_str(b, "233", 0); */

    /* segfaults */
    mpz_init_set_str(b, "23345555555555555555555555", 0);

    mpz_divexact(res, a, b);

    return 0;

GDB backtrace is not very informative.
With -O0 -g:

#0  __gmpn_copyi () at tmp-copyi.s:75
#1  0x40048fc8 in __gmpz_divexact (quot=0xbfffef40, num=0xbfffef60, den=0xbfffef50) at divexact.c:113


#0  0x4006f09d in __gmp_exception () from /usr/lib/
#1  0x4006f0ce in __gmp_divide_by_zero () from /usr/lib/
#2  0x400794ca in __gmpz_divexact () from /usr/lib/

If you need any other information - let me know, I'd be happy to help.

Antony Dovgal

More information about the gmp-bugs mailing list