00db10
commit abe7f530bf5c741fe6f0658da7be59d8db168f7f
00db10
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
00db10
Date:   Wed Apr 10 11:31:46 2013 +0530
00db10
00db10
    Accept leading and trailing spaces in getdate input string
00db10
    
00db10
    Fixes #15346.
00db10
    
00db10
    The POSIX description of getdate allows for extra spaces in the
00db10
    getdate input string.  __getdate_r uses strptime internally, which
00db10
    works fine with extra spaces between format strings (and hence within
00db10
    an input string) but not with leading and trailing spaces.  So we trim
00db10
    off the leading and trailing spaces before we pass it on to strptime.
00db10
00db10
diff --git glibc-2.17-c758a686/time/getdate.c glibc-2.17-c758a686/time/getdate.c
00db10
index 637dd18..eadebc3 100644
00db10
--- glibc-2.17-c758a686/time/getdate.c
00db10
+++ glibc-2.17-c758a686/time/getdate.c
00db10
@@ -25,6 +25,8 @@
00db10
 #include <time.h>
00db10
 #include <unistd.h>
00db10
 #include <sys/stat.h>
00db10
+#include <ctype.h>
00db10
+#include <alloca.h>
00db10
 
00db10
 #define TM_YEAR_BASE 1900
00db10
 
00db10
@@ -135,6 +137,44 @@ __getdate_r (const char *string, struct tm *tp)
00db10
   /* No threads reading this stream.  */
00db10
   __fsetlocking (fp, FSETLOCKING_BYCALLER);
00db10
 
00db10
+  /* Skip leading whitespace.  */
00db10
+  while (isspace (*string))
00db10
+    string++;
00db10
+
00db10
+  size_t inlen, oldlen;
00db10
+
00db10
+  oldlen = inlen = strlen (string);
00db10
+
00db10
+  /* Skip trailing whitespace.  */
00db10
+  while (inlen > 0 && isspace (string[inlen - 1]))
00db10
+    inlen--;
00db10
+
00db10
+  char *instr = NULL;
00db10
+
00db10
+  if (inlen < oldlen)
00db10
+    {
00db10
+      bool using_malloc = false;
00db10
+
00db10
+      if (__libc_use_alloca (inlen + 1))
00db10
+	instr = alloca (inlen + 1);
00db10
+      else
00db10
+	{
00db10
+	  instr = malloc (inlen + 1);
00db10
+	  if (instr == NULL)
00db10
+	    {
00db10
+	      fclose (fp);
00db10
+	      return 6;
00db10
+	    }
00db10
+	  using_malloc = true;
00db10
+	}
00db10
+      memcpy (instr, string, inlen);
00db10
+      instr[inlen] = '\0';
00db10
+      string = instr;
00db10
+
00db10
+      if (!using_malloc)
00db10
+	instr = NULL;
00db10
+    }
00db10
+
00db10
   line = NULL;
00db10
   len = 0;
00db10
   do
00db10
@@ -159,6 +199,8 @@ __getdate_r (const char *string, struct tm *tp)
00db10
     }
00db10
   while (!feof_unlocked (fp));
00db10
 
00db10
+  free (instr);
00db10
+
00db10
   /* Free the buffer.  */
00db10
   free (line);
00db10
 
00db10
diff --git glibc-2.17-c758a686/time/tst-getdate.c glibc-2.17-c758a686/time/tst-getdate.c
00db10
index 7604e83..dc8ecf4 100644
00db10
--- glibc-2.17-c758a686/time/tst-getdate.c
00db10
+++ glibc-2.17-c758a686/time/tst-getdate.c
00db10
@@ -31,6 +31,10 @@ static const struct
00db10
 } tests [] =
00db10
 {
00db10
   {"21:01:10 1999-1-31", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
00db10
+  {"21:01:10    1999-1-31", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
00db10
+  {"   21:01:10 1999-1-31", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
00db10
+  {"21:01:10 1999-1-31   ", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
00db10
+  {"    21:01:10 1999-1-31   ", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
00db10
   {"21:01:10 1999-2-28", "Universal", 0, {10, 1, 21, 28, 1, 99, 0, 0, 0}},
00db10
   {"16:30:46 2000-2-29", "Universal", 0, {46, 30,16, 29, 1, 100, 0, 0, 0}},
00db10
   {"01-08-2000 05:06:07", "Europe/Berlin", 0, {7, 6, 5, 1, 7, 100, 0, 0, 0}}