Bug 5451 - Incorrect processing of some conversion specifications by getdate() and strptime() functions
Summary: Incorrect processing of some conversion specifications by getdate() and strpt...
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.4
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-12-06 14:28 UTC by Alexey Miheev
Modified: 2014-07-03 12:01 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexey Miheev 2007-12-06 14:28:55 UTC
There are some problems in getdate() function.
<ol>
<li> Conversion specifications "%S" and "%M" are not processed, when used 
without "%H" conversion specification. </li>
<li> Templates "%Y", "%y", "%d", "%C", "%C %y", "%C %y %m" are not processed 
separately. </li>
<li> When template is "%Y %m", day of month is not set to 1 as the POSIX 
standard requires. </li>
</ol>
<p>Also there is a problem in strptime() function.
<ol>
<li> While using "%U", "%W" no data is written in corresponding fields of 
structure tm. </li>
</ol>
<p>
The sample code below reproduces these problems. Call the compiled program with 
the parameter, that specify a path to file, where templates will be stored.
<p>
<code>
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

struct tm *getdate(const char *string);
char* templ_filename;
extern int getdate_err;

// Writes template given as parameter to file,
// specified as the argument
void outputToTemplateFile(char* str)
{
    FILE *fd=fopen(templ_filename, "w");
    if(fd==NULL)
    {
        printf("Can not open file for writing\n");
        exit(1);
    }

    fprintf(fd, "%s\n", str);
    fclose(fd);
}

// Calls getdate() function with specified parameter,
// specified as the argument, also checks the contents of
// file with template and prints the result
void processGetdateOn(char* str)
{
struct tm * res;
char templ[1000];
FILE *fd=fopen(templ_filename, "r");

    if(fd==NULL)
    {
        printf("Can not open file for reading\n");
        exit(1);
    }

    if(fgets(templ, 1000, fd)==NULL)
    {
        printf("Can not read file\n");
        exit(1);
    }
    fclose(fd);

    res=getdate(str); 
    if(res==NULL)
    {
        printf("Failed on getdate(\"%s\"), template is: %s", str, templ);
        printf("Error number: %d\n\n", getdate_err);
    }
    else
    {
        printf("Success on getdate(\"%s\"), template is: %s\n",  str, templ);
        printf("Result is\n");
        printf("Seconds: %d\n", res->tm_sec);
        printf("Minutes: %d\n", res->tm_min);
        printf("Hour: %d\n", res->tm_hour);
        printf("Day of month: %d\n", res->tm_mday);
        printf("Month of year: %d\n", res->tm_mon);
        printf("Years since 1900: %d\n", res->tm_year);
        printf("Day of week: %d\n", res->tm_wday);
        printf("Day of year: %d\n", res->tm_yday);
        printf("Daylight Savings flag: %d\n\n", res->tm_isdst);
    }
}

// Executes strptime() function with parameters
// buf and format and prints the result
void processStrptimeOn(char* buf, char* format)
{
char *res;
struct tm tms;
	
    res=(char*)strptime(buf, format, &tms); 
    if(res!=NULL)
    {
        printf("Result of strptime() on template %s and buf %s is\n", format, 
buf);
        printf("Seconds: %d\n", tms.tm_sec);
        printf("Minutes: %d\n", tms.tm_min);
        printf("Hour: %d\n", tms.tm_hour);
        printf("Day of month: %d\n", tms.tm_mday);
        printf("Month of year: %d\n", tms.tm_mon);
        printf("Years since 1900: %d\n", tms.tm_year);
        printf("Day of week: %d\n", tms.tm_wday);
        printf("Day of year: %d\n", tms.tm_yday);
        printf("Daylight Savings flag: %d\n\n", tms.tm_isdst);
    }
    else
    {
        printf("Error: strptime()\n");
    }
}

int
main (int argc, char* argv [])
{

    if(argc < 2)
    {
        printf("Command line: progname template_filename_full_path\n");
        exit(1);
    }
    templ_filename=argv[1];

    setenv("DATEMSK", templ_filename, 1);

    /*
     * The following 4 testcases reproduce the problem:
     * 1. Templates "%S" and "%M" are not processed, 
     *    when used without "%H" template
     */	
    outputToTemplateFile("%M"); 
    processGetdateOn("1");     // Bug: getdate() returns error
 
    outputToTemplateFile("%M %H");
    processGetdateOn("1 2");   // OK

    outputToTemplateFile("%S");
    processGetdateOn("1");     //Bug: getdate() returns error

    outputToTemplateFile("%S %H");
    processGetdateOn("1 2");   // OK

    /*
     * The following 9 testcases reproduce the problem:
     * 2. Templates "%Y", "%y", "%d", "%C", "%C %y" 
     *    are not processed separately
     */	
    outputToTemplateFile("%Y");
    processGetdateOn("2001");     // Bug: getdate() returns error

    outputToTemplateFile("%Y %m");
    processGetdateOn("2001 3");   // OK
		
    outputToTemplateFile("%y");
    processGetdateOn("70");       // Bug: getdate() returns error

    outputToTemplateFile("%y %m");
    processGetdateOn("70 3");     // OK
		
    outputToTemplateFile("%d");
    processGetdateOn("06");       // Bug: getdate() returns error

    outputToTemplateFile("%d %m");
    processGetdateOn("25 3");     // OK

    outputToTemplateFile("%C");
    processGetdateOn("98");       // Bug: getdate() returns error

    outputToTemplateFile("%C %y %m");
    processGetdateOn("98 3 2");   // Bug: getdate() returns error
		
    outputToTemplateFile("%C %y");
    processGetdateOn("21 5");     // Bug: getdate() returns error

    /*
     * The following testcase reproduces the problem:
     * 3. When template is "%Y %m", day of month is not set 
     *    to 1 as standard requires 
     */	
    outputToTemplateFile("%Y %m");
    processGetdateOn("2008 3");   // Bug in getdate(): 
                                  // day of month is not 1 as required by POSIX

    /*
     * The following testcases reproduce the problem
     * of strptime().
     * 1. While using "%U", "%W" nothing happens.
     */	
    processStrptimeOn("3", "%U");
    processStrptimeOn("3", "%W");
	
    return 0;
}
</code>
Comment 1 Ulrich Drepper 2007-12-10 02:29:14 UTC
getdate should provide correct defaults.  But your strptime assumptions are just
wrong.  cvs contains the fixes/