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