summaryrefslogtreecommitdiff
path: root/mpq/get_str.c
diff options
context:
space:
mode:
authorMarco Bodrato <bodrato@mail.dm.unipi.it>2018-05-04 21:43:34 +0200
committerMarco Bodrato <bodrato@mail.dm.unipi.it>2018-05-04 21:43:34 +0200
commit071f5e609c5601165a07d68fb077f88140592c7f (patch)
tree537792b5431c315ceecb6892aea2ea948a443f12 /mpq/get_str.c
parent0bbd79a595602e3f72bb7234342ff69fd0adc2d3 (diff)
downloadgmp-071f5e609c5601165a07d68fb077f88140592c7f.tar.gz
mpq_*_str: document and support the correct base range
Diffstat (limited to 'mpq/get_str.c')
-rw-r--r--mpq/get_str.c13
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);