diff options
Diffstat (limited to 'mpq/get_str.c')
-rw-r--r-- | mpq/get_str.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/mpq/get_str.c b/mpq/get_str.c index 881340fb9..d0cfb58dd 100644 --- a/mpq/get_str.c +++ b/mpq/get_str.c @@ -1,6 +1,6 @@ /* mpq_get_str -- mpq to string conversion. -Copyright 2001, 2002, 2006, 2011 Free Software Foundation, Inc. +Copyright 2001, 2002, 2006, 2011, 2018 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -47,6 +47,8 @@ mpq_get_str (char *str, int base, mpq_srcptr q) /* This is an overestimate since we don't bother checking how much of the high limbs of num and den are used. +2 for rounding up the chars per bit of num and den. +3 for sign, slash and '\0'. */ + if (ABS(base) < 2) + base = 10; DIGITS_IN_BASE_PER_LIMB (str_alloc, ABSIZ(NUM(q)) + SIZ(DEN(q)), ABS(base)); str_alloc += 6; @@ -64,9 +66,12 @@ mpq_get_str (char *str, int base, mpq_srcptr q) ASSERT (len == strlen(str)); ASSERT (str_alloc == 0 || len+1 <= str_alloc); - ASSERT (len+1 <= /* size recommended to applications */ - mpz_sizeinbase (mpq_numref(q), ABS(base)) + - mpz_sizeinbase (mpq_denref(q), ABS(base)) + 3); + ASSERT (len+1 <= 3 + /* size recommended to applications */ + (ABS(base) < 2 ? + mpz_sizeinbase (mpq_numref(q), 10) + + mpz_sizeinbase (mpq_denref(q), 10) + : mpz_sizeinbase (mpq_numref(q), ABS(base)) + + mpz_sizeinbase (mpq_denref(q), ABS(base)))); if (str_alloc != 0) __GMP_REALLOCATE_FUNC_MAYBE_TYPE (str, str_alloc, len+1, char); |