Skip to content
This repository was archived by the owner on Feb 5, 2022. It is now read-only.

Commit 9ca230d

Browse files
author
Ulrich Drepper
committed
* stdio-common/vfprintf.c (vfprintf): Compute necessary buffer size
with size_t type. * stdio-common/printf_fp.c (__print_fp): Change chars_needed type to size_t. Add casts where needed.
1 parent eb46bc8 commit 9ca230d

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
2007-11-06 Ulrich Drepper <drepper@redhat.com>
22

3+
* stdio-common/vfprintf.c (vfprintf): Compute necessary buffer size
4+
with size_t type.
5+
* stdio-common/printf_fp.c (__print_fp): Change chars_needed type to
6+
size_t. Add casts where needed.
7+
38
* nscd/selinux.c (nscd_request_avc_has_perm): When compiled with
49
old headers, don't call avc_has_perm if we don't have the
510
permission information.

stdio-common/printf_fp.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ ___printf_fp (FILE *fp,
808808
{
809809
int width = info->width;
810810
wchar_t *wstartp, *wcp;
811-
int chars_needed;
811+
size_t chars_needed;
812812
int expscale;
813813
int intdig_max, intdig_no = 0;
814814
int fracdig_min;
@@ -823,7 +823,7 @@ ___printf_fp (FILE *fp,
823823
type = info->spec;
824824
intdig_max = 1;
825825
fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
826-
chars_needed = 1 + 1 + fracdig_max + 1 + 1 + 4;
826+
chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
827827
/* d . ddd e +- ddd */
828828
dig_max = INT_MAX; /* Unlimited. */
829829
significant = 1; /* Does not matter here. */
@@ -838,12 +838,12 @@ ___printf_fp (FILE *fp,
838838
{
839839
intdig_max = exponent + 1;
840840
/* This can be really big! */ /* XXX Maybe malloc if too big? */
841-
chars_needed = exponent + 1 + 1 + fracdig_max;
841+
chars_needed = (size_t) exponent + 1 + 1 + (size_t) fracdig_max;
842842
}
843843
else
844844
{
845845
intdig_max = 1;
846-
chars_needed = 1 + 1 + fracdig_max;
846+
chars_needed = 1 + 1 + (size_t) fracdig_max;
847847
}
848848
}
849849
else
@@ -858,7 +858,7 @@ ___printf_fp (FILE *fp,
858858
type = isupper (info->spec) ? 'E' : 'e';
859859
fracdig_max = dig_max - 1;
860860
intdig_max = 1;
861-
chars_needed = 1 + 1 + fracdig_max + 1 + 1 + 4;
861+
chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
862862
}
863863
else
864864
{
@@ -870,7 +870,7 @@ ___printf_fp (FILE *fp,
870870
zeros can be as many as would be required for
871871
exponential notation with a negative two-digit
872872
exponent, which is 4. */
873-
chars_needed = dig_max + 1 + 4;
873+
chars_needed = (size_t) dig_max + 1 + 4;
874874
}
875875
fracdig_min = info->alt ? fracdig_max : 0;
876876
significant = 0; /* We count significant digits. */
@@ -888,16 +888,17 @@ ___printf_fp (FILE *fp,
888888
it is possible that we need two more characters in front of all the
889889
other output. If the amount of memory we have to allocate is too
890890
large use `malloc' instead of `alloca'. */
891+
size_t wbuffer_to_alloc = (2 + (size_t) chars_needed) * sizeof (wchar_t);
891892
buffer_malloced = ! __libc_use_alloca (chars_needed * 2 * sizeof (wchar_t));
892893
if (__builtin_expect (buffer_malloced, 0))
893894
{
894-
wbuffer = (wchar_t *) malloc ((2 + chars_needed) * sizeof (wchar_t));
895+
wbuffer = (wchar_t *) malloc (wbuffer_to_alloc);
895896
if (wbuffer == NULL)
896897
/* Signal an error to the caller. */
897898
return -1;
898899
}
899900
else
900-
wbuffer = (wchar_t *) alloca ((2 + chars_needed) * sizeof (wchar_t));
901+
wbuffer = (wchar_t *) alloca (wbuffer_to_alloc);
901902
wcp = wstartp = wbuffer + 2; /* Let room for rounding. */
902903

903904
/* Do the real work: put digits in allocated buffer. */

stdio-common/vfprintf.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
747747
{ \
748748
int temp = width; \
749749
width = prec; \
750-
PAD (L_('0'));; \
750+
PAD (L_('0')); \
751751
width = temp; \
752752
} \
753753
\
@@ -1499,18 +1499,24 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
14991499
if (prec > width
15001500
&& prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0])))
15011501
{
1502-
if (__libc_use_alloca ((prec + 32) * sizeof (CHAR_T)))
1503-
workend = ((CHAR_T *) alloca ((prec + 32) * sizeof (CHAR_T)))
1504-
+ (prec + 32);
1502+
if (__builtin_expect (prec > ~((size_t) 0) - 31, 0))
1503+
{
1504+
done = -1;
1505+
goto all_done;
1506+
}
1507+
size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1508+
1509+
if (__libc_use_alloca (needed))
1510+
workend = (((CHAR_T *) alloca (needed)) + ((size_t) prec + 32));
15051511
else
15061512
{
1507-
workstart = (CHAR_T *) malloc ((prec + 32) * sizeof (CHAR_T));
1513+
workstart = (CHAR_T *) malloc (needed);
15081514
if (workstart == NULL)
15091515
{
15101516
done = -1;
15111517
goto all_done;
15121518
}
1513-
workend = workstart + (prec + 32);
1519+
workend = workstart + ((size_t) prec + 32);
15141520
}
15151521
}
15161522
JUMP (*f, step2_jumps);

0 commit comments

Comments
 (0)