Bug 15346

Summary: getdate() doesn't ignore trailing whitespaces
Product: glibc Reporter: Siddhesh Poyarekar <siddhesh>
Component: libcAssignee: Siddhesh Poyarekar <siddhesh>
Status: RESOLVED FIXED    
Severity: normal CC: drepper.fsp
Priority: P2 Flags: fweimer: security-
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Siddhesh Poyarekar 2013-04-08 12:28:21 UTC
The POSIX description of getdate says that:

"Extra whitespace in either the template file or in string shall be ignored."

but it does not.

Steps to Reproduce:

$ cat > tfile
%M
^D
$ cat > date.c
#define _GNU_SOURCE 500
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  struct tm *tmp;
  int j;

  for (j = 1; j < argc; j++)
    {
      tmp = getdate (argv[j]);

      if (tmp == NULL)
        {
          printf ("Call %d failed; getdate_err = %d\n", j, getdate_err);
          continue;
        }

      printf ("Call %d (\"%s\") succeeded:\n", j, argv[j]);
      printf ("    tm_sec   = %d\n", tmp->tm_sec);
      printf ("    tm_min   = %d\n", tmp->tm_min);
      printf ("    tm_hour  = %d\n", tmp->tm_hour);
      printf ("    tm_mday  = %d\n", tmp->tm_mday);
      printf ("    tm_mon   = %d\n", tmp->tm_mon);
      printf ("    tm_year  = %d\n", tmp->tm_year);
      printf ("    tm_wday  = %d\n", tmp->tm_wday);
      printf ("    tm_yday  = %d\n", tmp->tm_yday);
      printf ("    tm_isdst = %d\n", tmp->tm_isdst);
    }

  exit (EXIT_SUCCESS);
}
^D
$ gcc date.c
$ DATEMSK=tfile ./a.out '1 '

Actual Result:

Call 1 failed; getdate_err = 7

Expected Result:

Call 1 ("1 ") succeeded:
    tm_sec   = 0
    tm_min   = 1
    tm_hour  = 0
    tm_mday  = 9
    tm_mon   = 3
    tm_year  = 113
    tm_wday  = 2
    tm_yday  = 98
    tm_isdst = 0
Comment 1 Siddhesh Poyarekar 2013-04-10 06:03:53 UTC
Fixed in master:

commit abe7f530bf5c741fe6f0658da7be59d8db168f7f
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
Date:   Wed Apr 10 11:31:46 2013 +0530

    Accept leading and trailing spaces in getdate input string
    
    Fixes #15346.
    
    The POSIX description of getdate allows for extra spaces in the
    getdate input string.  __getdate_r uses strptime internally, which
    works fine with extra spaces between format strings (and hence within
    an input string) but not with leading and trailing spaces.  So we trim
    off the leading and trailing spaces before we pass it on to strptime.