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