segfault in mpz_divexact()

Torbjorn Granlund tg-this-will-bounce-but-I-am-subscribed-to-the-list-honest at swox.com
Wed Apr 25 19:04:49 CEST 2007


Antony Dovgal <antony at zend.com> writes:

  > (The documentation of this function could be clarified in other ways.
  > E.g., that one should never call this function if one first need to
  > verify that the remainder vanishes.)
  
  Sure, that's clearly up to you.
  
I decided to make the code avoid the crash.
Please try this fix:

*** mpz/divexact.c      27 Apr 2006 16:30:18 -0000      1.2
--- mpz/divexact.c      25 Apr 2007 17:02:13 -0000      1.3
*************** mpz_divexact (mpz_ptr quot, mpz_srcptr n
*** 66,73 ****
    qp = quot->_mp_d;
  
!   if (nsize == 0)
      {
!       if (dsize == 0)
!       DIVIDE_BY_ZERO;
        quot->_mp_size = 0;
        return;
--- 66,74 ----
    qp = quot->_mp_d;
  
!   if (nsize < dsize)
      {
!       /* This special case avoids segfaults below when the function is
!        incorrectly called with |N| < |D|, N != 0.  It also handles the
!        well-defined case N = 0.  */
        quot->_mp_size = 0;
        return;
  

-- 
Torbjörn


More information about the gmp-bugs mailing list