segfault in mpz_divexact()

Torbjorn Granlund tg-this-will-bounce-but-I-am-subscribed-to-the-list-honest at swox.com
Wed Apr 18 15:21:14 CEST 2007


Antony Dovgal <antony at zend.com> writes:

  
  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(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.

Yes, it is.  :-) The __gmp_divide_by_zero function is there,
suggesting that GMP detects an operand error.

  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
  
  Without:
  
Without what?

  #0  0x4006f09d in __gmp_exception () from /usr/lib/libgmp.so.3
  #1  0x4006f0ce in __gmp_divide_by_zero () from /usr/lib/libgmp.so.3
  #2  0x400794ca in __gmpz_divexact () from /usr/lib/libgmp.so.3
  
  If you need any other information - let me know, I'd be happy to help.
  
I don't see any evidence that you're getting a segfault (SIGSEGV
signal), except that you say so.  If you actually mean that GMP
crashes with a divide-by-zero (perhaps SIGFPE) that please say so.

-- 
Torbjörn


More information about the gmp-bugs mailing list