This is the mail archive of the glibc-bugs@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/812] New: mktime does not return -1


mktime function does not (time_t) -1 when the input is invalid.
Here is a test case.

#include <time.h>
#include <stdio.h>
                                                                               
                  
main(int argc, char** argv)
{
    time_t t1=0;
    struct tm t2;
                                                                               
                  
/* 02/29/1889 is an invalid date, mktime() should fail.          */
    t2.tm_year  = 1889 - 1900;
    t2.tm_mon   = 2 - 1 ;
    t2.tm_mday  = 29;
    t2.tm_hour  = 0;
    t2.tm_min   = 0;
    t2.tm_sec   = 1;
    t2.tm_isdst  = -1;
    t1 = mktime(&t2);
    printf("tm_year = [%d], tm_isdst = [%d], T1=[%d]\n", t2.tm_year,
t2.tm_isdst,t1);
    if (t1 != (time_t) -1) {
        printf("ERROR...mktime not returning -1 when error occurs.\n");
        return 1;
    }
    return 0;
}

Steps to Reproduce:
1. gcc version 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
2. glibc-2.3.4-2.ppc
2. Compile: gcc t.c
3. Run: a.out

Actual Results:
ERROR...mktime not returning -1 when error occurs.

Expected Results:
Return 0.

All though it has been tested on PowerPC platform, I think it's generic bug in
glibc-2.3.4.

I've also, created a fix for the above problem as shown below,
diff -Naru glibc-20041219T2331/time/mktime.c glibc-20041219T2331.mine/time/mktime.c
--- glibc-20041219T2331/time/mktime.c   2004-12-02 14:16:28.000000000 -0800
+++ glibc-20041219T2331.mine/time/mktime.c      2005-03-31 18:53:01.036619040 -0800
@@ -463,7 +463,7 @@
       t2 = t1 + sec_adjustment;
       if (((t1 < t) != (sec_requested < 0))
          | ((t2 < t1) != (sec_adjustment < 0))
-         | ! (*convert) (&t2, &tm))
+         | ! (*convert) (&t2, &tm) | (t2 < 0))
        return -1;
       t = t2;
     }
~

As it makes no sense to return the negative value other than -1 in case of
error. This patch has been tested and above change seems to fix the problem.

TESTING before the change:
Tesing with tm_year=0
tm_year = [0], tm_isdst = [0], T1=[-1]
tm_year = [1], tm_isdst = [0], T1=[-2147483648]
ERROR...mktime not returning -1 when error occurs.
tm_year = [1], tm_isdst = [0], T1=[-2147483648]
ERROR...mktime not returning -1 when error occurs.
Tesing with tm_year=1
tm_year = [1], tm_isdst = [0], T1=[-1]
tm_year = [1], tm_isdst = [0], T1=[-2147483648]
ERROR...mktime not returning -1 when error occurs.
tm_year = [1], tm_isdst = [0], T1=[-2147483648]
ERROR...mktime not returning -1 when error occurs.
Tesing with tm_year=-1
tm_year = [-1], tm_isdst = [0], T1=[-1]
tm_year = [1], tm_isdst = [0], T1=[-2147483648]
ERROR...mktime not returning -1 when error occurs.
tm_year = [1], tm_isdst = [0], T1=[-2147483648]
ERROR...mktime not returning -1 when error occurs.
Tesing with tm_year=-11
tm_year = [-11], tm_isdst = [0], T1=[-1]
tm_year = [1], tm_isdst = [0], T1=[-2147483648]
ERROR...mktime not returning -1 when error occurs.
tm_year = [1], tm_isdst = [0], T1=[-2147483648]
ERROR...mktime not returning -1 when error occurs.

TESTIG after the change:
Tesing with tm_year=0
tm_year = [0], tm_isdst = [0], T1=[-1]
tm_year = [0], tm_isdst = [1], T1=[-1]
tm_year = [0], tm_isdst = [-1], T1=[-1]
Tesing with tm_year=1
tm_year = [1], tm_isdst = [0], T1=[-1]
tm_year = [1], tm_isdst = [1], T1=[-1]
tm_year = [1], tm_isdst = [-1], T1=[-1]
Tesing with tm_year=-1
tm_year = [-1], tm_isdst = [0], T1=[-1]
tm_year = [-1], tm_isdst = [1], T1=[-1]
tm_year = [-1], tm_isdst = [-1], T1=[-1]
Tesing with tm_year=-11
tm_year = [-11], tm_isdst = [0], T1=[-1]
tm_year = [-11], tm_isdst = [1], T1=[-1]
tm_year = [-11], tm_isdst = [-1], T1=[-1]


Thanks,

Uttam

-- 
           Summary: mktime does not return -1
           Product: glibc
           Version: 2.3.4
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: gotom at debian dot or dot jp
        ReportedBy: uttamp at us dot ibm dot com
                CC: glibc-bugs at sources dot redhat dot com,uttamp at us
                    dot ibm dot com
  GCC host triplet: powerpc


http://sources.redhat.com/bugzilla/show_bug.cgi?id=812

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]