summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2017-05-22 09:29:51 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2017-05-22 09:29:51 +0000
commit6bb4430fe26265c06bd63c8222e103defe3c136a (patch)
tree23048d3a6b5d2f8642af36487baf36f0800dc8b4
parent25a521e674cada3fe67998693bcaeb065cfdcf24 (diff)
downloadmpfr-6bb4430fe26265c06bd63c8222e103defe3c136a.tar.gz
[src/vasprintf.c] Partially fixed check for integer overflow when
the long and int types have the same size. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@11515 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--src/vasprintf.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/vasprintf.c b/src/vasprintf.c
index 3f6fd8295..44bf6afe5 100644
--- a/src/vasprintf.c
+++ b/src/vasprintf.c
@@ -1600,7 +1600,7 @@ partition_number (struct number_parts *np, mpfr_srcptr p,
struct printf_spec spec)
{
char *str;
- long total;
+ unsigned int total; /* significantly larger than an int */
int uppercase;
/* WARNING: left justification means right space padding */
@@ -1820,30 +1820,31 @@ partition_number (struct number_parts *np, mpfr_srcptr p,
total = np->sign ? 1 : 0;
total += np->prefix_size;
total += np->ip_size;
- if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
+ if (MPFR_UNLIKELY (total > INT_MAX))
goto error;
total += np->ip_trailing_zeros;
- if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
+ if (MPFR_UNLIKELY (total > INT_MAX))
goto error;
+ MPFR_ASSERTD (np->ip_size + np->ip_trailing_zeros >= 1);
if (np->thousands_sep)
/* ' flag, style f and the thousands separator in current locale is not
reduced to the null character */
total += (np->ip_size + np->ip_trailing_zeros - 1) / 3;
- if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
+ if (MPFR_UNLIKELY (total > INT_MAX))
goto error;
if (np->point)
++total;
total += np->fp_leading_zeros;
- if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
+ if (MPFR_UNLIKELY (total > INT_MAX))
goto error;
total += np->fp_size;
- if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
+ if (MPFR_UNLIKELY (total > INT_MAX))
goto error;
total += np->fp_trailing_zeros;
- if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
+ if (MPFR_UNLIKELY (total > INT_MAX))
goto error;
total += np->exp_size;
- if (MPFR_UNLIKELY (total < 0 || total > INT_MAX))
+ if (MPFR_UNLIKELY (total > INT_MAX))
goto error;
if (spec.width > total)
@@ -1852,6 +1853,8 @@ partition_number (struct number_parts *np, mpfr_srcptr p,
np->pad_size = spec.width - total;
total += np->pad_size; /* here total == spec.width,
so 0 < total < INT_MAX */
+ if (MPFR_UNLIKELY (total > INT_MAX))
+ goto error;
}
return total;