vfprintf.c: Refactor magic number 32 into EXTSIZ.

The magic number 32 is used everywhere as extra size to
use when doing certain operations. This commit refactors
that into a macro so you can change this value if you're
debugging something in a local build.
This commit is contained in:
Carlos O'Donell 2017-05-08 16:11:45 -04:00
parent 1721145f03
commit 7224e32d7b
2 changed files with 33 additions and 25 deletions

View File

@ -1,3 +1,9 @@
2017-05-08 Carlos O'Donell <carlos@redhat.com>
* stdio-common/vfprintf.c (EXTSIZ): Define.
(vfprintf): Use EXTSIZ.
(printf_positional): Likewise.
2017-05-08 Joseph Myers <joseph@codesourcery.com>
* sysdeps/unix/sysv/linux/kernel-features.h

View File

@ -45,6 +45,10 @@
#define va_list _IO_va_list
#undef BUFSIZ
#define BUFSIZ _IO_BUFSIZ
/* In some cases we need extra space for all the output which is not
counted in the width of the string. We assume 32 characters is
enough. */
#define EXTSIZ 32
#define ARGCHECK(S, Format) \
do \
{ \
@ -1456,20 +1460,19 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
left = 1;
}
if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - 32))
if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
{
__set_errno (EOVERFLOW);
done = -1;
goto all_done;
}
if (width >= WORK_BUFFER_SIZE - 32)
if (width >= WORK_BUFFER_SIZE - EXTSIZ)
{
/* We have to use a special buffer. The "32" is just a safe
bet for all the output which is not counted in the width. */
size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
/* We have to use a special buffer. */
size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
if (__libc_use_alloca (needed))
workend = (CHAR_T *) alloca (needed) + width + 32;
workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
else
{
workstart = (CHAR_T *) malloc (needed);
@ -1478,7 +1481,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
done = -1;
goto all_done;
}
workend = workstart + width + 32;
workend = workstart + width + EXTSIZ;
}
}
}
@ -1489,20 +1492,19 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
width = read_int (&f);
if (__glibc_unlikely (width == -1
|| width >= INT_MAX / sizeof (CHAR_T) - 32))
|| width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
{
__set_errno (EOVERFLOW);
done = -1;
goto all_done;
}
if (width >= WORK_BUFFER_SIZE - 32)
if (width >= WORK_BUFFER_SIZE - EXTSIZ)
{
/* We have to use a special buffer. The "32" is just a safe
bet for all the output which is not counted in the width. */
size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
/* We have to use a special buffer. */
size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
if (__libc_use_alloca (needed))
workend = (CHAR_T *) alloca (needed) + width + 32;
workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
else
{
workstart = (CHAR_T *) malloc (needed);
@ -1511,7 +1513,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
done = -1;
goto all_done;
}
workend = workstart + width + 32;
workend = workstart + width + EXTSIZ;
}
}
if (*f == L_('$'))
@ -1562,23 +1564,23 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
}
else
prec = 0;
if (prec > width && prec > WORK_BUFFER_SIZE - 32)
if (prec > width && prec > WORK_BUFFER_SIZE - EXTSIZ)
{
/* Deallocate any previously allocated buffer because it is
too small. */
if (__glibc_unlikely (workstart != NULL))
free (workstart);
workstart = NULL;
if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - 32))
if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
{
__set_errno (EOVERFLOW);
done = -1;
goto all_done;
}
size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
size_t needed = ((size_t) prec + EXTSIZ) * sizeof (CHAR_T);
if (__libc_use_alloca (needed))
workend = (CHAR_T *) alloca (needed) + prec + 32;
workend = (CHAR_T *) alloca (needed) + prec + EXTSIZ;
else
{
workstart = (CHAR_T *) malloc (needed);
@ -1587,7 +1589,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
done = -1;
goto all_done;
}
workend = workstart + prec + 32;
workend = workstart + prec + EXTSIZ;
}
}
JUMP (*f, step2_jumps);
@ -1964,23 +1966,23 @@ printf_positional (_IO_FILE *s, const CHAR_T *format, int readonly_format,
}
/* Maybe the buffer is too small. */
if (MAX (prec, width) + 32 > WORK_BUFFER_SIZE)
if (MAX (prec, width) + EXTSIZ > WORK_BUFFER_SIZE)
{
if (__libc_use_alloca ((MAX (prec, width) + 32)
if (__libc_use_alloca ((MAX (prec, width) + EXTSIZ)
* sizeof (CHAR_T)))
workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
workend = ((CHAR_T *) alloca ((MAX (prec, width) + EXTSIZ)
* sizeof (CHAR_T))
+ (MAX (prec, width) + 32));
+ (MAX (prec, width) + EXTSIZ));
else
{
workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
workstart = (CHAR_T *) malloc ((MAX (prec, width) + EXTSIZ)
* sizeof (CHAR_T));
if (workstart == NULL)
{
done = -1;
goto all_done;
}
workend = workstart + (MAX (prec, width) + 32);
workend = workstart + (MAX (prec, width) + EXTSIZ);
}
}