Loading...
stdtime/getdate.c Libc-391 Libc-1669.0.4
--- Libc/Libc-391/stdtime/getdate.c
+++ Libc/Libc-1669.0.4/stdtime/getdate.c
@@ -24,6 +24,7 @@
 #include <stdio.h>
 #include <time.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
@@ -48,7 +49,7 @@
 #define	TM_YEAR_SET	0x04
 #define	UNDEFINED	-1
 
-static struct tm tmundef = {
+static const struct tm tmundef = {
     UNDEFINED,
     UNDEFINED,
     UNDEFINED,
@@ -69,10 +70,11 @@
 struct tm *
 getdate(const char *str)
 {
-    static struct tm tm, *now, *result = NULL;
+    static struct tm tm;
+    struct tm *now, *result = NULL;
     time_t t;
     FILE *fp;
-    int bufsiz, offset, len, dateset, timeset, saveerrno, wday_set;
+    int bufsiz, offset, len, dateset, timeset, saveerrno, wday_set, save_mon;
     char *buf;
     struct stat st;
     char *file = getenv(DATEMSK);
@@ -218,7 +220,7 @@
 		case TM_YEAR_SET:
 		case TM_YEAR_SET | TM_MON_SET:
 		    if(!(dateset & TM_MON_SET))
-			tm.tm_mon = 1;
+			tm.tm_mon = 0;
 		    tm.tm_mday = 1;
 		    break;
 
@@ -232,19 +234,25 @@
 		tm.tm_wday = now->tm_wday;
 		tm.tm_gmtoff = now->tm_gmtoff;	/* XXX: can't grok timezones */
 		tm.tm_isdst = -1;
-
+		save_mon = tm.tm_mon;
 		if(mktime(&tm) == (time_t)-1) {
 		    getdate_err = INVALID_DATE;
 		    break;
-		}
-		if(wday_set != UNDEFINED) {
+		} else if ((dateset & TM_MON_SET) && (tm.tm_mon != save_mon)) { /* Did mktime fixup an overflow date? */
+			getdate_err = INVALID_DATE;
+			break;
+		}
+		if(wday_set != UNDEFINED &&
+			(dateset != (TM_YEAR_SET | TM_MON_SET | TM_MDAY_SET))) {
+		    /*
+		     * We got back a week day, but not enough information to resolve it
+		     * to a specific day, so we need to push forward the time to the
+		     * correct wday.  <rdar://problem/27439823>
+		     */
 		    int delta = wday_set - tm.tm_wday;
-		    if(delta && (dateset & TM_MDAY_SET)) {
-			getdate_err = INVALID_DATE;
-			break;
-		    }
-		    if(delta < 0)
+		    if(delta < 0) {
 			delta += 7;
+		    }
 		    tm.tm_mday += delta;
 		    if(mktime(&tm) == (time_t)-1) {
 			getdate_err = INVALID_DATE;
@@ -252,6 +260,7 @@
 		    }
 		}
 		result = &tm;
+		break;
 	    }
 	} while(0);