As of lib/libc/time/strptime.c r1.15, certain conversions will perform
calculations using the provided tm_mday value, which will frequently
produce incorrect values for tm_mday and tm_yday.  Apologies in
advance for the mangled patch.

Thanks.

--david

Compare runs of the test program below:

/* strptime_test */
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(int argc, char **argv)
{
        struct tm tm;
        memset(&tm, 0xff, sizeof(tm));
        if (argc > 2 && strptime(argv[1], argv[2], &tm) != NULL)
                printf("(%d,%d)\n", tm.tm_yday, tm.tm_mday);
        else
                printf("arg/parse error\n");
        return 0;
}

# the two examples below should both produce (-1,-1)
./strptime_test '2014-01' '%Y-%m'
./strptime_test '2014-01' '%Y-%d'


--- strptime.c.orig 2014-02-13 13:21:12.000000000 -0500
+++ strptime.c 2014-02-13 13:45:44.000000000 -0500
@@ -594,7 +594,8 @@
  const int year = tm->tm_year + TM_YEAR_BASE;
  const int *mon_lens = mon_lengths[isleap(year)];
  if (!(fields & FIELD_TM_YDAY) &&
-     (fields & (FIELD_TM_MON|FIELD_TM_MDAY))) {
+     (fields & (FIELD_TM_MON|FIELD_TM_MDAY)) ==
+ ((FIELD_TM_MON|FIELD_TM_MDAY)) {
  tm->tm_yday = tm->tm_mday - 1;
  for (i = 0; i < tm->tm_mon; i++)
  tm->tm_yday += mon_lens[i];

Reply via email to