From 49a0e39c7b3278e55cb6e2d94bf5cc275918b419 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 20 Oct 2007 14:56:19 +0200 Subject: [PATCH] Don't report an unjustified overflow error. --- ChangeLog | 6 ++++++ lib/vasnprintf.c | 33 +++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index d3f95b760c..96a9a09bdf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-10-20 Bruno Haible + + * lib/vasnprintf.c (VASNPRINTF): Don't report overflow if the available + length is INT_MAX and sizeof (DCHAR_T) > sizeof (TCHAR_T). + Reported by Ralf Wildenhues . + 2007-10-20 Bruno Haible * tests/test-floorf2.c (correct_result_p): Don't rely on excess diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 5d818aa642..42d0e747a7 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -3442,7 +3442,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* SNPRINTF can fail if its second argument is > INT_MAX. */ if (maxlen > INT_MAX / TCHARS_PER_DCHAR) - goto overflow; + maxlen = INT_MAX / TCHARS_PER_DCHAR; maxlen = maxlen * TCHARS_PER_DCHAR; # define SNPRINTF_BUF(arg) \ switch (prefix_count) \ @@ -3663,17 +3663,26 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* Handle overflow of the allocated buffer. */ if (count >= maxlen) { - /* Need at least count * sizeof (TCHAR_T) bytes. But - allocate proportionally, to avoid looping eternally - if snprintf() reports a too small count. */ - size_t n = - xmax (xsum (length, - (count + TCHARS_PER_DCHAR - 1) - / TCHARS_PER_DCHAR), - xtimes (allocated, 2)); - - ENSURE_ALLOCATION (n); - continue; + /* If maxlen already has attained its allowed maximum, + allocating more memory will not increase maxlen. + Instead of looping, bail out. */ + if (maxlen == INT_MAX / TCHARS_PER_DCHAR) + goto overflow; + else + { + /* Need at least count * sizeof (TCHAR_T) bytes. + But allocate proportionally, to avoid looping + eternally if snprintf() reports a too small + count. */ + size_t n = + xmax (xsum (length, + (count + TCHARS_PER_DCHAR - 1) + / TCHARS_PER_DCHAR), + xtimes (allocated, 2)); + + ENSURE_ALLOCATION (n); + continue; + } } #endif -- 2.30.2