Index: lib/libc/nameser/ns_name.c =================================================================== --- lib/libc/nameser/ns_name.c (revision 269651) +++ lib/libc/nameser/ns_name.c (working copy) @@ -427,11 +427,12 @@ } if (len < 0) len = srcp - src + 1; - srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff)); - if (srcp < msg || srcp >= eom) { /*%< Out of range. */ + l = ((n & 0x3f) << 8) | (*srcp & 0xff); + if (l >= eom - msg) { /*%< Out of range. */ errno = EMSGSIZE; return (-1); } + srcp = msg + l; checked += 2; /* * Check for loops in the compressed name; Index: lib/libc/stdtime/strptime.3 =================================================================== --- lib/libc/stdtime/strptime.3 (revision 269651) +++ lib/libc/stdtime/strptime.3 (working copy) @@ -164,9 +164,7 @@ .Fa \&%U and .Fa %W -format specifiers accept any value within the range 00 to 53 -without validating against other values supplied (like month -or day of the year, for example). +format specifiers accept a two-digit decimal within the range 00 to 53. .Pp The .Fa %Z Index: lib/libc/stdtime/strptime.c =================================================================== --- lib/libc/stdtime/strptime.c (revision 269651) +++ lib/libc/stdtime/strptime.c (working copy) @@ -56,10 +56,27 @@ #include "libc_private.h" #include "timelocal.h" +#include "tzfile.h" + static char * _strptime(const char *, const char *, struct tm *, int *, locale_t); -#define asizeof(a) (sizeof (a) / sizeof ((a)[0])) +#define asizeof(a) (sizeof(a) / sizeof((a)[0])) +/* + * Calculate the week day of the first day of a year. Valid for + * the Gregorian calendar, which began Sept 14, 1752 in the UK + * and its colonies. Ref: + * http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week/ + */ + +static int +first_wday_of(int year) +{ + return (((2 * (3 - (year / 100) % 4)) + (year % 100) + + ((year % 100) / 4) + (isleap(year) ? 6 : 0) + 1) % 7); +} + + static char * _strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp, locale_t locale) @@ -66,9 +83,13 @@ { char c; const char *ptr; + int day_offset = -1; int i, len; int Ealternative, Oalternative; - struct lc_time_T *tptr = __get_current_time_locale(locale); + const struct lc_time_T *tptr = __get_current_time_locale(locale); + static int start_of_month[2][13] = { + {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, + {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}}; ptr = fmt; while (*ptr != 0) { @@ -119,7 +140,7 @@ if (i < 19) return (NULL); - tm->tm_year = i * 100 - 1900; + tm->tm_year = i * 100 - TM_YEAR_BASE; break; case 'c': @@ -303,6 +324,27 @@ return (NULL); tm->tm_wday = i; + if (day_offset >= 0 && (i - day_offset) != 0) { + tm->tm_yday += i - day_offset; + i = 0; + while (tm->tm_yday >= + start_of_month[isleap(tm->tm_year + + TM_YEAR_BASE)][i]) + i++; + if (i > 12) + { + i = 1; + tm->tm_yday -= + start_of_month[ + isleap(tm->tm_year + TM_YEAR_BASE)] + [12]; + tm->tm_year++; + } + tm->tm_mon = i - 1; + tm->tm_mday = tm->tm_yday - + start_of_month[isleap(tm->tm_year + + TM_YEAR_BASE)][i - 1] + 1; + } buf += len; break; @@ -309,10 +351,8 @@ case 'U': case 'W': /* - * XXX This is bogus, as we can not assume any valid - * information present in the tm structure at this - * point to calculate a real value, so just check the - * range for now. + * We expect that the year has already been + * parsed. */ if (!isdigit_l((unsigned char)*buf, locale)) return (NULL); @@ -327,6 +367,43 @@ if (i > 53) return (NULL); + /* Week numbers are 1-origin. So that we can always + * return the date of a Sunday (or Monday), treat week + * 0 as week 1. + */ + + if (i == 0) + i = 1; + + if (c == 'U') + day_offset = TM_SUNDAY; + else + day_offset = TM_MONDAY; + + /* + * Set the date to the first Sunday (or Monday) + * of the specified week of the year. + */ + + tm->tm_yday = (7 - first_wday_of(tm->tm_year + + TM_YEAR_BASE) + day_offset) % 7 + (i - 1) * 7; + i = 0; + while (tm->tm_yday >= + start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)][i]) + i++; + if (i > 12) + { + i = 1; + tm->tm_yday -= + start_of_month[ + isleap(tm->tm_year + TM_YEAR_BASE)][12]; + tm->tm_year++; + } + tm->tm_mon = i - 1; + tm->tm_mday = tm->tm_yday - + start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)][i - 1] + 1; + tm->tm_wday = day_offset; + break; case 'w': @@ -471,7 +548,7 @@ len--; } if (c == 'Y') - i -= 1900; + i -= TM_YEAR_BASE; if (c == 'y' && i < 69) i += 100; if (i < 0) @@ -546,7 +623,6 @@ return ((char *)buf); } - char * strptime_l(const char * __restrict buf, const char * __restrict fmt, struct tm * __restrict tm, locale_t loc) @@ -564,6 +640,7 @@ return (ret); } + char * strptime(const char * __restrict buf, const char * __restrict fmt, struct tm * __restrict tm)