bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Missing return in unsigned long mpz_get_ui ?


From: Krzysztof Kozminski
Subject: Missing return in unsigned long mpz_get_ui ?
Date: Fri, 12 Jul 2002 10:41:04 -0700

I configured gmp 4.1 as follows:

configure --enable-cxx --enable-debug --with-gnu-ld

and the resulting include/gmp.h seems to have a problem in the aforementioned
function.

At the beginning of the header, there is this:

#if ! __GMP_WITHIN_CONFIGURE
#define __GMP_BITS_PER_MP_LIMB             32
#define __GMP_HAVE_HOST_CPU_FAMILY_power   0
#define __GMP_HAVE_HOST_CPU_FAMILY_powerpc 0
#define GMP_LIMB_BITS                      32
#define GMP_NAIL_BITS                      0
#endif

So in the procedure:

unsigned long
mpz_get_ui (mpz_srcptr __gmp_z) __GMP_NOTHROW
{
  mp_ptr __gmp_p = __gmp_z->_mp_d;
  mp_size_t __gmp_n = __gmp_z->_mp_size;
  mp_limb_t __gmp_l = __gmp_p[0];
  if (__GMP_ULONG_MAX <= GMP_NUMB_MASK)
    return __gmp_l & (-(mp_limb_t) (__gmp_n != 0));
#if GMP_NAIL_BITS != 0  /* redundant #if, shuts up compiler warnings */
  else                  /* happens for nails, but not if LONG_LONG_LIMB */
    {                   /* assume two limbs are enough to fill an ulong */
      __gmp_n = __GMP_ABS (__gmp_n);
      if (__gmp_n <= 1)
        return __gmp_l & (-(mp_limb_t) (__gmp_n != 0));
      else
        return __gmp_l + (__gmp_p[1] << GMP_NUMB_BITS);
    }
#endif
}

it appears that a random result may be returned whenever the 'if' condition is
false.  Even if the condition is always true whenver GMP_NAIL_BITS == 0, I see
that you're trying to shut up compiler warnings, so perhaps adding extra
#ifdef as below would be appropriate:

unsigned long
mpz_get_ui (mpz_srcptr __gmp_z) __GMP_NOTHROW
{
  mp_ptr __gmp_p = __gmp_z->_mp_d;
  mp_size_t __gmp_n = __gmp_z->_mp_size;
  mp_limb_t __gmp_l = __gmp_p[0];
#if GMP_NAIL_BITS != 0                      // <<<<<<<<<< ADDED BY KK
  if (__GMP_ULONG_MAX <= GMP_NUMB_MASK)
#endif                                      // <<<<<<<<<< ADDED BY KK
    return __gmp_l & (-(mp_limb_t) (__gmp_n != 0));
#if GMP_NAIL_BITS != 0  /* redundant #if, shuts up compiler warnings */
  else                  /* happens for nails, but not if LONG_LONG_LIMB */
    {                   /* assume two limbs are enough to fill an ulong */
      __gmp_n = __GMP_ABS (__gmp_n);
      if (__gmp_n <= 1)
        return __gmp_l & (-(mp_limb_t) (__gmp_n != 0));
      else
        return __gmp_l + (__gmp_p[1] << GMP_NUMB_BITS);
    }
#endif
}


KK
-------
"Divide by cucumber error.  Please reinstall the Universe and reboot."

Attachment: Krzysztof.A.Kozminski.vcf
Description: Card for Krzysztof Kozminski


reply via email to

[Prev in Thread] Current Thread [Next in Thread]