1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-04-28 10:05:08 +03:00
2003-01-19  Ulrich Drepper  <drepper@redhat.com>

	* time/strftime.c (my_strftime): Handle very large width
	specifications for numeric values correctly.  Improve checks for
	overflow.
	* time/Makefile (tests): Add tst-strftime.
	* time/tst-strftime.c: New file.
This commit is contained in:
Ulrich Drepper 2003-01-19 10:05:55 +00:00
parent 6675b19146
commit b1a173f75e
4 changed files with 128 additions and 19 deletions

View File

@ -1,3 +1,11 @@
2003-01-19 Ulrich Drepper <drepper@redhat.com>
* time/strftime.c (my_strftime): Handle very large width
specifications for numeric values correctly. Improve checks for
overflow.
* time/Makefile (tests): Add tst-strftime.
* time/tst-strftime.c: New file.
2003-01-18 Ulrich Drepper <drepper@redhat.com> 2003-01-18 Ulrich Drepper <drepper@redhat.com>
* nis/nss_nis/nis-hosts.c: Make _nss_nis_endhostent an alias of * nis/nss_nis/nis-hosts.c: Make _nss_nis_endhostent an alias of

View File

@ -1,4 +1,4 @@
# Copyright (C) 1991-1999,2000,01,02 Free Software Foundation, Inc. # Copyright (C) 1991-2002, 2003 Free Software Foundation, Inc.
# This file is part of the GNU C Library. # This file is part of the GNU C Library.
# The GNU C Library is free software; you can redistribute it and/or # The GNU C Library is free software; you can redistribute it and/or
@ -34,7 +34,7 @@ aux := era alt_digit lc-time-cleanup
distribute := datemsk distribute := datemsk
tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \ tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \
tst-getdate tst-mktime tst-ftime_l tst-getdate tst-mktime tst-ftime_l tst-strftime
include ../Rules include ../Rules

View File

@ -1,4 +1,5 @@
/* Copyright (C) 1991-1999, 2000, 2001, 2002 Free Software Foundation, Inc. /* Copyright (C) 1991-1999, 2000, 2001, 2002, 2003
Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@ -263,7 +264,7 @@ static const CHAR_T zeroes[16] = /* "0000000000000000" */
int _n = (n); \ int _n = (n); \
int _delta = width - _n; \ int _delta = width - _n; \
int _incr = _n + (_delta > 0 ? _delta : 0); \ int _incr = _n + (_delta > 0 ? _delta : 0); \
if (i + _incr >= maxsize) \ if ((size_t) _incr >= maxsize - i) \
return 0; \ return 0; \
if (p) \ if (p) \
{ \ { \
@ -743,8 +744,15 @@ my_strftime (s, maxsize, format, tp ut_argument LOCALE_PARAM)
width = 0; width = 0;
do do
{ {
width *= 10; if (width > INT_MAX / 10
width += *f - L_('0'); || (width == INT_MAX / 10 && *f - L_('0') > INT_MAX % 10))
/* Avoid overflow. */
width = INT_MAX;
else
{
width *= 10;
width += *f - L_('0');
}
++f; ++f;
} }
while (ISDIGIT (*f)); while (ISDIGIT (*f));
@ -768,10 +776,10 @@ my_strftime (s, maxsize, format, tp ut_argument LOCALE_PARAM)
switch (format_char) switch (format_char)
{ {
#define DO_NUMBER(d, v) \ #define DO_NUMBER(d, v) \
digits = width == -1 ? d : width; \ digits = d > width ? d : width; \
number_value = v; goto do_number number_value = v; goto do_number
#define DO_NUMBER_SPACEPAD(d, v) \ #define DO_NUMBER_SPACEPAD(d, v) \
digits = width == -1 ? d : width; \ digits = d > width ? d : width; \
number_value = v; goto do_number_spacepad number_value = v; goto do_number_spacepad
case L_('%'): case L_('%'):
@ -1033,18 +1041,37 @@ my_strftime (s, maxsize, format, tp ut_argument LOCALE_PARAM)
int padding = digits - (buf + (sizeof (buf) / sizeof (buf[0])) int padding = digits - (buf + (sizeof (buf) / sizeof (buf[0]))
- bufp); - bufp);
if (pad == L_('_')) if (padding > 0)
{ {
while (0 < padding--) if (pad == L_('_'))
*--bufp = L_(' '); {
} if ((size_t) padding >= maxsize - i)
else return 0;
{
bufp += negative_number; if (p)
while (0 < padding--) memset_space (p, padding);
*--bufp = L_('0'); i += padding;
if (negative_number) width = width > padding ? width - padding : 0;
*--bufp = L_('-'); }
else
{
if ((size_t) digits >= maxsize - i)
return 0;
if (negative_number)
{
++bufp;
if (p)
*p++ = L_('-');
++i;
}
if (p)
memset_zero (p, padding);
i += padding;
width = 0;
}
} }
} }

74
time/tst-strftime.c Normal file
View File

@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static struct
{
const char *fmt;
size_t min;
size_t max;
} tests[] =
{
{ "%2000Y", 2000, 4000 },
{ "%02000Y", 2000, 4000 },
{ "%_2000Y", 2000, 4000 },
{ "%-2000Y", 2000, 4000 },
};
#define ntests (sizeof (tests) / sizeof (tests[0]))
static int
do_test (void)
{
size_t cnt;
int result = 0;
time_t tnow = time (NULL);
struct tm *now = localtime (&tnow);
for (cnt = 0; cnt < ntests; ++cnt)
{
size_t size = 0;
int res;
char *buf = NULL;
do
{
size += 500;
buf = (char *) realloc (buf, size);
if (buf == NULL)
{
puts ("out of memory");
exit (1);
}
res = strftime (buf, size, tests[cnt].fmt, now);
if (res != 0)
break;
}
while (size < tests[cnt].max);
if (res == 0)
{
printf ("%Zu: %s: res == 0 despite size == %Zu\n",
cnt, tests[cnt].fmt, size);
result = 1;
}
else if (size < tests[cnt].min)
{
printf ("%Zu: %s: size == %Zu was enough\n",
cnt, tests[cnt].fmt, size);
result = 1;
}
else
printf ("%Zu: %s: size == %Zu: OK\n", cnt, tests[cnt].fmt, size);
free (buf);
}
return result;
}
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"