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