summaryrefslogtreecommitdiff
path: root/lib/vasnprintf.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2007-10-20 14:56:19 +0200
committerBruno Haible <bruno@clisp.org>2007-10-20 14:56:19 +0200
commit49a0e39c7b3278e55cb6e2d94bf5cc275918b419 (patch)
tree5073cf81c7cc6d0e05d75283a10fd7a0b158fc8f /lib/vasnprintf.c
parentc532b2a8ef48d317a1bc348584c00880ea7c5ba6 (diff)
downloadgnulib-49a0e39c7b3278e55cb6e2d94bf5cc275918b419.tar.gz
Don't report an unjustified overflow error.
Diffstat (limited to 'lib/vasnprintf.c')
-rw-r--r--lib/vasnprintf.c33
1 files changed, 21 insertions, 12 deletions
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