Non-canonical assignment shouldn't fail.

Axel Simon A.Simon at kent.ac.uk
Thu Dec 8 17:31:41 CET 2005


Torbjörn,

using gmp 4.1.4 on ia32 and gcc version 4.0.0 the following C++ program
segfaults:

#include<gmpxx.h>

int main(void) {
  mpz_class num(1);
  mpz_class den(-1);
  mpq_class rat(0);
  rat=mpq_class(num, den);
  rat.canonicalize();
  return 0;
};


The reason is that constructing the rational by calling the constructor
is fine, but assigning it to 'rat' is not since the denominator is
negative, thereby allocating (-1) bytes in mpq_set. Although the
documentation for the C functions says that the fractions must be
canonical before calling any arithmetic function, it seems overly
restrictive to assume that for the assignment operator, since it makes
perfectly legal C++ program such as the one above crash. Could mpq_set
be changed so it deals with negative denominators?

Thanks,
Axel.
 



More information about the gmp-bugs mailing list