8ae002
commit d36c75fc0d44deec29635dd239b0fbd206ca49b7
8ae002
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
8ae002
Date:   Sat Sep 26 13:27:48 2015 -0700
8ae002
8ae002
    Fix BZ #18985 -- out of range data to strftime() causes a segfault
8ae002
8ae002
diff --git a/time/strftime_l.c b/time/strftime_l.c
8ae002
index b48ef34..4eb647c 100644
8ae002
--- a/time/strftime_l.c
8ae002
+++ b/time/strftime_l.c
8ae002
@@ -510,13 +510,17 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument
8ae002
      only a few elements.  Dereference the pointers only if the format
8ae002
      requires this.  Then it is ok to fail if the pointers are invalid.  */
8ae002
 # define a_wkday \
8ae002
-  ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday))
8ae002
+  ((const CHAR_T *) (tp->tm_wday < 0 || tp->tm_wday > 6			     \
8ae002
+		     ? "?" : _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday)))
8ae002
 # define f_wkday \
8ae002
-  ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday))
8ae002
+  ((const CHAR_T *) (tp->tm_wday < 0 || tp->tm_wday > 6			     \
8ae002
+		     ? "?" : _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday)))
8ae002
 # define a_month \
8ae002
-  ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon))
8ae002
+  ((const CHAR_T *) (tp->tm_mon < 0 || tp->tm_mon > 11			     \
8ae002
+		     ? "?" : _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon)))
8ae002
 # define f_month \
8ae002
-  ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon))
8ae002
+  ((const CHAR_T *) (tp->tm_mon < 0 || tp->tm_mon > 11			     \
8ae002
+		     ? "?" : _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon)))
8ae002
 # define ampm \
8ae002
   ((const CHAR_T *) _NL_CURRENT (LC_TIME, tp->tm_hour > 11		      \
8ae002
 				 ? NLW(PM_STR) : NLW(AM_STR)))
8ae002
@@ -526,8 +530,10 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument
8ae002
 # define ap_len STRLEN (ampm)
8ae002
 #else
8ae002
 # if !HAVE_STRFTIME
8ae002
-#  define f_wkday (weekday_name[tp->tm_wday])
8ae002
-#  define f_month (month_name[tp->tm_mon])
8ae002
+#  define f_wkday (tp->tm_wday < 0 || tp->tm_wday > 6	\
8ae002
+		   ? "?" : weekday_name[tp->tm_wday])
8ae002
+#  define f_month (tp->tm_mon < 0 || tp->tm_mon > 11	\
8ae002
+		   ? "?" : month_name[tp->tm_mon])
8ae002
 #  define a_wkday f_wkday
8ae002
 #  define a_month f_month
8ae002
 #  define ampm (L_("AMPM") + 2 * (tp->tm_hour > 11))
8ae002
@@ -1321,7 +1327,7 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument
8ae002
 		  *tzset_called = true;
8ae002
 		}
8ae002
 # endif
8ae002
-	      zone = tzname[tp->tm_isdst];
8ae002
+	      zone = tp->tm_isdst <= 1 ? tzname[tp->tm_isdst] : "?";
8ae002
 	    }
8ae002
 #endif
8ae002
 	  if (! zone)
8ae002
diff --git a/time/tst-strftime.c b/time/tst-strftime.c
8ae002
index 374fba4..af3ff72 100644
8ae002
--- a/time/tst-strftime.c
8ae002
+++ b/time/tst-strftime.c
8ae002
@@ -4,6 +4,56 @@
8ae002
 #include <time.h>
8ae002
 
8ae002
 
8ae002
+static int
8ae002
+do_bz18985 (void)
8ae002
+{
8ae002
+  char buf[1000];
8ae002
+  struct tm ttm;
8ae002
+  int rc, ret = 0;
8ae002
+
8ae002
+  memset (&ttm, 1, sizeof (ttm));
8ae002
+  ttm.tm_zone = NULL;  /* Dereferenced directly if non-NULL.  */
8ae002
+  rc = strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm);
8ae002
+
8ae002
+  if (rc == 66)
8ae002
+    {
8ae002
+      const char expected[]
8ae002
+	= "? ? ? ? ? ? 16843009 16843009:16843009:16843009 16844909 +467836 ?";
8ae002
+      if (0 != strcmp (buf, expected))
8ae002
+	{
8ae002
+	  printf ("expected:\n  %s\ngot:\n  %s\n", expected, buf);
8ae002
+	  ret += 1;
8ae002
+	}
8ae002
+    }
8ae002
+  else
8ae002
+    {
8ae002
+      printf ("expected 66, got %d\n", rc);
8ae002
+      ret += 1;
8ae002
+    }
8ae002
+
8ae002
+  /* Check negative values as well.  */
8ae002
+  memset (&ttm, 0xFF, sizeof (ttm));
8ae002
+  ttm.tm_zone = NULL;  /* Dereferenced directly if non-NULL.  */
8ae002
+  rc = strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm);
8ae002
+
8ae002
+  if (rc == 30)
8ae002
+    {
8ae002
+      const char expected[] = "? ? ? ? ? ? -1 -1:-1:-1 1899  ";
8ae002
+      if (0 != strcmp (buf, expected))
8ae002
+	{
8ae002
+	  printf ("expected:\n  %s\ngot:\n  %s\n", expected, buf);
8ae002
+	  ret += 1;
8ae002
+	}
8ae002
+    }
8ae002
+  else
8ae002
+    {
8ae002
+      printf ("expected 30, got %d\n", rc);
8ae002
+      ret += 1;
8ae002
+    }
8ae002
+
8ae002
+  return ret;
8ae002
+}
8ae002
+
8ae002
 static struct
8ae002
 {
8ae002
   const char *fmt;
8ae002
@@ -104,7 +154,7 @@ do_test (void)
8ae002
 	}
8ae002
     }
8ae002
 
8ae002
-  return result;
8ae002
+  return result + do_bz18985 ();
8ae002
 }
8ae002
 
8ae002
 #define TEST_FUNCTION do_test ()