]> git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/asctime.c.patch
Libc-583.tar.gz
[apple/libc.git] / stdtime / FreeBSD / asctime.c.patch
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.
5 */
6
7 +#define EXPECTEDLEN 26
8 +
9 char *
10 -asctime_r(timeptr, buf)
11 -const struct tm * timeptr;
12 -char * buf;
13 +asctime_r(const struct tm * __restrict timeptr, char * __restrict buf)
14 {
15 static const char wday_name[][3] = {
16 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
17 @@ -36,6 +36,8 @@ char * buf;
18 };
19 const char * wn;
20 const char * mn;
21 + int len;
22 + char tmp[EXPECTEDLEN];
23
24 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
25 wn = "???";
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.
29 */
30 - (void) sprintf(buf, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
31 + /*
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.
36 + */
37 + len = snprintf(tmp, EXPECTEDLEN, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
38 wn, mn,
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)
43 + return NULL;
44 + strcpy(buf, tmp);
45 return buf;
46 }
47
48 -/*
49 -** A la X3J11, with core dump avoidance.
50 -*/
51 -
52 char *
53 asctime(timeptr)
54 const struct tm * timeptr;
55 {
56 - /*
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).
62 - */
63 - static char result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
64 - 3 + 2 + 1 + 1];
65 + static char result[EXPECTEDLEN];
66
67 return asctime_r(timeptr, result);
68 }