This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: vfprintf typing problem
From: "Carlos O'Donell" <carlos@systemhalted.org>
Date: Fri, 30 Mar 2012 08:55:27 -0400
> Could you explain why we don't also need to fix the same problem
> with `prec'?
Indeed, we need to adjust the 'prec' case as well, I've taken care of
that here, and added precision overflow checks to the testcase.
How does it look now?
2012-03-30 David S. Miller <davem@davemloft.net>
* stdio-common/vfprintf.c (vfprintf): Validate width and precision
against overflow of INT_MAX. Set errno to EOVERFLOW when 'done'
overflows INT_MAX. Check for overflow of in-format-string precision
values properly.
* stdio-common/bug22.c: Adjust to test both width/prevision
INT_MAX overflow as well as total length INT_MAX overflow. Check
explicitly for proper errno values.
diff --git a/stdio-common/bug22.c b/stdio-common/bug22.c
index 2228388..cb99dfe 100644
--- a/stdio-common/bug22.c
+++ b/stdio-common/bug22.c
@@ -1,12 +1,22 @@
/* BZ #5424 */
#include <stdio.h>
+#include <errno.h>
+/* INT_MAX + 1 */
#define N 2147483648
+/* (INT_MAX / 2) + 2 */
+#define N2 1073741825
+
+/* INT_MAX - 3 */
+#define N3 2147483644
+
#define STRINGIFY(S) #S
#define MAKE_STR(S) STRINGIFY(S)
#define SN MAKE_STR(N)
+#define SN2 MAKE_STR(N2)
+#define SN3 MAKE_STR(N3)
static int
do_test (void)
@@ -20,11 +30,25 @@ do_test (void)
return 1;
}
- ret = fprintf (fp, "%" SN "d%" SN "d", 1, 1);
+ ret = fprintf (fp, "%" SN "d", 1);
+ printf ("ret = %d\n", ret);
+ if (ret != -1 || errno != ERANGE)
+ return 1;
+
+ ret = fprintf (fp, "%." SN "d", 1);
+ printf ("ret = %d\n", ret);
+ if (ret != -1 || errno != ERANGE)
+ return 1;
+
+ ret = fprintf (fp, "%." SN3 "d", 1);
+ printf ("ret = %d\n", ret);
+ if (ret != -1 || errno != ERANGE)
+ return 1;
+ ret = fprintf (fp, "%" SN2 "d%" SN2 "d", 1, 1);
printf ("ret = %d\n", ret);
- return ret != -1;
+ return ret != -1 || errno != EOVERFLOW;
}
#define TIMEOUT 30
diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
index 1e90483..340abd3 100644
--- a/stdio-common/vfprintf.c
+++ b/stdio-common/vfprintf.c
@@ -71,6 +71,7 @@
< _val, 0)) \
{ \
done = -1; \
+ __set_errno (EOVERFLOW); \
goto all_done; \
} \
done += _val; \
@@ -141,12 +142,18 @@
do \
{ \
assert ((size_t) done <= (size_t) INT_MAX); \
- if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len) \
- || (size_t) INT_MAX - (size_t) done < (size_t) (Len)) \
+ if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
{ \
done = -1; \
goto all_done; \
} \
+ if (__builtin_expect ((unsigned int) INT_MAX - (unsigned int) done \
+ < (Len), 0)) \
+ { \
+ done = -1; \
+ __set_errno (EOVERFLOW); \
+ goto all_done; \
+ } \
done += (Len); \
} \
while (0)
@@ -1449,7 +1456,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
left = 1;
}
- if (__builtin_expect (width >= (size_t) -1 / sizeof (CHAR_T) - 32, 0))
+ if (__builtin_expect (width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
{
__set_errno (ERANGE);
done = -1;
@@ -1481,7 +1488,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
LABEL (width):
width = read_int (&f);
- if (__builtin_expect (width >= (size_t) -1 / sizeof (CHAR_T) - 32, 0))
+ if (__builtin_expect (width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
{
__set_errno (ERANGE);
done = -1;
@@ -1529,13 +1536,24 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
prec = -1;
}
else if (ISDIGIT (*f))
- prec = read_int (&f);
+ {
+ prec = read_int (&f);
+
+ /* The precision was specified in this case as an extremely
+ large positive value. */
+ if (prec < 0)
+ {
+ __set_errno (ERANGE);
+ done = -1;
+ goto all_done;
+ }
+ }
else
prec = 0;
if (prec > width
&& prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
{
- if (__builtin_expect (prec >= (size_t) -1 / sizeof (CHAR_T) - 32, 0))
+ if (__builtin_expect (prec >= INT_MAX / sizeof (CHAR_T) - 32, 0))
{
__set_errno (ERANGE);
done = -1;