1 --- asctime.c.orig 2008-12-15 11:41:07.000000000 -0800
2 +++ asctime.c 2009-01-21 17:09:27.000000000 -0800
3 @@ -22,10 +22,10 @@ __FBSDID("$FreeBSD: src/lib/libc/stdtime
4 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, Second Edition, 1996-07-12.
7 +#define EXPECTEDLEN 26
10 -asctime_r(timeptr, buf)
11 -const struct tm * timeptr;
13 +asctime_r(const struct tm * __restrict timeptr, char * __restrict buf)
15 static const char wday_name[][3] = {
16 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
17 @@ -36,6 +36,8 @@ char * buf;
22 + char tmp[EXPECTEDLEN];
24 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
26 @@ -48,31 +50,28 @@ char * buf;
27 ** "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
28 ** Since the .2 in 02.2d is ignored, we drop it.
30 - (void) sprintf(buf, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
32 + ** Because various values in the tm structure may cause the
33 + ** resulting string to be longer than the 26-bytes that is
34 + ** specified in the spec, we should return NULL rather than
35 + ** possibly overwrite beyond the string.
37 + len = snprintf(tmp, EXPECTEDLEN, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
39 timeptr->tm_mday, timeptr->tm_hour,
40 timeptr->tm_min, timeptr->tm_sec,
41 TM_YEAR_BASE + timeptr->tm_year);
42 + if (len >= EXPECTEDLEN)
49 -** A la X3J11, with core dump avoidance.
54 const struct tm * timeptr;
57 - ** Big enough for something such as
58 - ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
59 - ** (two three-character abbreviations, five strings denoting integers,
60 - ** three explicit spaces, two explicit colons, a newline,
61 - ** and a trailing ASCII nul).
63 - static char result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
65 + static char result[EXPECTEDLEN];
67 return asctime_r(timeptr, result);