diff -r 457c0c9c7893 Doc/library/time.rst --- a/Doc/library/time.rst Thu Nov 15 07:10:27 2012 +0100 +++ b/Doc/library/time.rst Sun Nov 18 21:42:46 2012 +0200 @@ -129,7 +129,7 @@ .. function:: asctime([t]) Convert a tuple or :class:`struct_time` representing a time as returned by - :func:`gmtime` or :func:`localtime` to a 24-character string of the following + :func:`gmtime` or :func:`localtime` to a string of the following form: ``'Sun Jun 20 23:21:05 1993'``. If *t* is not provided, the current time as returned by :func:`localtime` is used. Locale information is not used by :func:`asctime`. diff -r 457c0c9c7893 Lib/test/test_time.py --- a/Lib/test/test_time.py Thu Nov 15 07:10:27 2012 +0100 +++ b/Lib/test/test_time.py Sun Nov 18 21:42:46 2012 +0200 @@ -2,6 +2,7 @@ import time import unittest import sys +import sysconfig class TimeTestCase(unittest.TestCase): @@ -45,57 +46,60 @@ with self.assertRaises(ValueError): time.strftime('%f') - def test_strftime_bounds_checking(self): + def _bounds_checking(self, func=time.strftime): # Make sure that strftime() checks the bounds of the various parts #of the time tuple (0 is valid for *all* values). # Check year [1900, max(int)] - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1899, 1, 1, 0, 0, 0, 0, 1, -1)) if time.accept2dyear: - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (-1, 1, 1, 0, 0, 0, 0, 1, -1)) - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (100, 1, 1, 0, 0, 0, 0, 1, -1)) # Check month [1, 12] + zero support - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, -1, 1, 0, 0, 0, 0, 1, -1)) - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 13, 1, 0, 0, 0, 0, 1, -1)) # Check day of month [1, 31] + zero support - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, -1, 0, 0, 0, 0, 1, -1)) - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 32, 0, 0, 0, 0, 1, -1)) # Check hour [0, 23] - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, -1, 0, 0, 0, 1, -1)) - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, 24, 0, 0, 0, 1, -1)) # Check minute [0, 59] - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, 0, -1, 0, 0, 1, -1)) - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, 0, 60, 0, 0, 1, -1)) # Check second [0, 61] - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, 0, 0, -1, 0, 1, -1)) # C99 only requires allowing for one leap second, but Python's docs say # allow two leap seconds (0..61) - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, 0, 0, 62, 0, 1, -1)) # No check for upper-bound day of week; # value forced into range by a ``% 7`` calculation. # Start check at -2 since gettmarg() increments value before taking # modulo. - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, 0, 0, 0, -2, 1, -1)) # Check day of the year [1, 366] + zero support - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, 0, 0, 0, 0, -1, -1)) - self.assertRaises(ValueError, time.strftime, '', + self.assertRaises(ValueError, func, (1900, 1, 1, 0, 0, 0, 0, 367, -1)) + def test_strftime_bounding_check(self): + self._bounds_checking(lambda tup: time.strftime('', tup)) + def test_default_values_for_zero(self): # Make sure that using all zeros uses the proper default values. # No test for daylight savings since strftime() does not change output @@ -121,17 +125,32 @@ def test_asctime(self): time.asctime(time.gmtime(self.t)) + + # Max year is only limited by the size of C int. + sizeof_int = sysconfig.get_config_vars('SIZEOF_INT')[0] + bigyear = (1 << 8 * sizeof_int - 1) - 1 + asc = time.asctime((bigyear, 6, 1) + (0,)*6) + self.assertEqual(asc[-len(str(bigyear)):], str(bigyear)) + self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8) self.assertRaises(TypeError, time.asctime, 0) self.assertRaises(TypeError, time.asctime, ()) - # XXX: Posix compiant asctime should refuse to convert - # year > 9999, but Linux implementation does not. - # self.assertRaises(ValueError, time.asctime, - # (12345, 1, 0, 0, 0, 0, 0, 0, 0)) - # XXX: For now, just make sure we don't have a crash: + + def test_asctime_bounding_check(self): + self._bounds_checking(time.asctime) + + def test_ctime(self): + t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1)) + self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973') + t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1)) + self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000') try: - time.asctime((12345, 1, 1, 0, 0, 0, 0, 1, 0)) - except ValueError: + bigval = time.mktime((10000, 1, 10) + (0,)*6) + except OverflowError: + # If mktime fails, ctime will fail too. This may happen + # on some platforms. pass + else: + self.assertEquals(time.ctime(bigval)[-5:], '10000') @unittest.skipIf(not hasattr(time, "tzset"), "time module has no attribute tzset") diff -r 457c0c9c7893 Misc/ACKS --- a/Misc/ACKS Thu Nov 15 07:10:27 2012 +0100 +++ b/Misc/ACKS Sun Nov 18 21:42:46 2012 +0200 @@ -471,6 +471,7 @@ Flemming Kjær Jensen Philip H. Jensen Philip Jenvey +MunSic Jeong Chris Jerdonek Jiba Pedro Diaz Jimenez @@ -554,6 +555,7 @@ Ross Lagerwall Cameron Laird Thomas Lamb +Jean-Baptiste "Jiba" Lamy Torsten Landschoff Łukasz Langa Tino Lange diff -r 457c0c9c7893 Misc/NEWS --- a/Misc/NEWS Thu Nov 15 07:10:27 2012 +0100 +++ b/Misc/NEWS Sun Nov 18 21:42:46 2012 +0200 @@ -145,6 +145,15 @@ Library ------- +- Issue #8013: time.asctime and time.ctime no longer call system + asctime and ctime functions. The year range for time.asctime is now + 1900 through maxint. The range for time.ctime is the same as for + time.localtime. The string produced by these functions is longer + than 24 characters when year is greater than 9999. + +- Issue #6608: time.asctime is now checking struct tm fields its input + before passing it to the system asctime. Patch by MunSic Jeong. + - Issue #16327: The subprocess module no longer leaks file descriptors used for stdin/stdout/stderr pipes to the child when fork() fails. diff -r 457c0c9c7893 Modules/timemodule.c --- a/Modules/timemodule.c Thu Nov 15 07:10:27 2012 +0100 +++ b/Modules/timemodule.c Sun Nov 18 21:42:46 2012 +0200 @@ -345,6 +345,9 @@ Convert seconds since the Epoch to a time tuple expressing local time.\n\ When 'seconds' is not passed in, convert the current time instead."); +/* Convert 9-item tuple to tm structure. Return 1 on success, set + * an exception and return 0 on error. + */ static int gettmarg(PyObject *args, struct tm *p) { @@ -388,6 +391,76 @@ return 1; } +/* Check values of the struct tm fields before it is passed to strftime() and + * asctime(). Return 1 if all values are valid, otherwise set an exception + * and returns 0. + */ +static int +checktm(struct tm* buf) +{ + /* Checks added to make sure strftime() and asctime() does not crash Python by + indexing blindly into some array for a textual representation + by some bad index (fixes bug #897625 and #6608). + + Also support values of zero from Python code for arguments in which + that is out of range by forcing that value to the lowest value that + is valid (fixed bug #1520914). + + Valid ranges based on what is allowed in struct tm: + + - tm_year: [0, max(int)] (1) + - tm_mon: [0, 11] (2) + - tm_mday: [1, 31] + - tm_hour: [0, 23] + - tm_min: [0, 59] + - tm_sec: [0, 60] + - tm_wday: [0, 6] (1) + - tm_yday: [0, 365] (2) + - tm_isdst: [-max(int), max(int)] + + (1) gettmarg() handles bounds-checking. + (2) Python's acceptable range is one greater than the range in C, + thus need to check against automatic decrement by gettmarg(). + */ + if (buf->tm_mon == -1) + buf->tm_mon = 0; + else if (buf->tm_mon < 0 || buf->tm_mon > 11) { + PyErr_SetString(PyExc_ValueError, "month out of range"); + return 0; + } + if (buf->tm_mday == 0) + buf->tm_mday = 1; + else if (buf->tm_mday < 0 || buf->tm_mday > 31) { + PyErr_SetString(PyExc_ValueError, "day of month out of range"); + return 0; + } + if (buf->tm_hour < 0 || buf->tm_hour > 23) { + PyErr_SetString(PyExc_ValueError, "hour out of range"); + return 0; + } + if (buf->tm_min < 0 || buf->tm_min > 59) { + PyErr_SetString(PyExc_ValueError, "minute out of range"); + return 0; + } + if (buf->tm_sec < 0 || buf->tm_sec > 61) { + PyErr_SetString(PyExc_ValueError, "seconds out of range"); + return 0; + } + /* tm_wday does not need checking of its upper-bound since taking + ``% 7`` in gettmarg() automatically restricts the range. */ + if (buf->tm_wday < 0) { + PyErr_SetString(PyExc_ValueError, "day of week out of range"); + return 0; + } + if (buf->tm_yday == -1) + buf->tm_yday = 0; + else if (buf->tm_yday < 0 || buf->tm_yday > 365) { + PyErr_SetString(PyExc_ValueError, "day of year out of range"); + return 0; + } + return 1; +} + #ifdef HAVE_STRFTIME static PyObject * time_strftime(PyObject *self, PyObject *args) @@ -407,69 +480,10 @@ if (tup == NULL) { time_t tt = time(NULL); buf = *localtime(&tt); - } else if (!gettmarg(tup, &buf)) + } + else if (!gettmarg(tup, &buf) || !checktm(&buf)) return NULL; - /* Checks added to make sure strftime() does not crash Python by - indexing blindly into some array for a textual representation - by some bad index (fixes bug #897625). - - Also support values of zero from Python code for arguments in which - that is out of range by forcing that value to the lowest value that - is valid (fixed bug #1520914). - - Valid ranges based on what is allowed in struct tm: - - - tm_year: [0, max(int)] (1) - - tm_mon: [0, 11] (2) - - tm_mday: [1, 31] - - tm_hour: [0, 23] - - tm_min: [0, 59] - - tm_sec: [0, 60] - - tm_wday: [0, 6] (1) - - tm_yday: [0, 365] (2) - - tm_isdst: [-max(int), max(int)] - - (1) gettmarg() handles bounds-checking. - (2) Python's acceptable range is one greater than the range in C, - thus need to check against automatic decrement by gettmarg(). - */ - if (buf.tm_mon == -1) - buf.tm_mon = 0; - else if (buf.tm_mon < 0 || buf.tm_mon > 11) { - PyErr_SetString(PyExc_ValueError, "month out of range"); - return NULL; - } - if (buf.tm_mday == 0) - buf.tm_mday = 1; - else if (buf.tm_mday < 0 || buf.tm_mday > 31) { - PyErr_SetString(PyExc_ValueError, "day of month out of range"); - return NULL; - } - if (buf.tm_hour < 0 || buf.tm_hour > 23) { - PyErr_SetString(PyExc_ValueError, "hour out of range"); - return NULL; - } - if (buf.tm_min < 0 || buf.tm_min > 59) { - PyErr_SetString(PyExc_ValueError, "minute out of range"); - return NULL; - } - if (buf.tm_sec < 0 || buf.tm_sec > 61) { - PyErr_SetString(PyExc_ValueError, "seconds out of range"); - return NULL; - } - /* tm_wday does not need checking of its upper-bound since taking - ``% 7`` in gettmarg() automatically restricts the range. */ - if (buf.tm_wday < 0) { - PyErr_SetString(PyExc_ValueError, "day of week out of range"); - return NULL; - } - if (buf.tm_yday == -1) - buf.tm_yday = 0; - else if (buf.tm_yday < 0 || buf.tm_yday > 365) { - PyErr_SetString(PyExc_ValueError, "day of year out of range"); - return NULL; - } /* Normalize tm_isdst just in case someone foolishly implements %Z based on the assumption that tm_isdst falls within the range of [-1, 1] */ @@ -551,34 +565,59 @@ return strptime_result; } + PyDoc_STRVAR(strptime_doc, "strptime(string, format) -> struct_time\n\ \n\ Parse a string to a time tuple according to a format specification.\n\ See the library reference manual for formatting codes (same as strftime())."); +static PyObject * +_asctime(struct tm *timeptr) +{ + /* Inspired by Open Group reference implementation available at + * http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */ + static char wday_name[7][3] = { + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" + }; + static char mon_name[12][3] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + }; + char buf[20]; /* 'Sun Sep 16 01:03:52\0' */ + int n; + + n = snprintf(buf, sizeof(buf), "%.3s %.3s%3d %.2d:%.2d:%.2d", + wday_name[timeptr->tm_wday], + mon_name[timeptr->tm_mon], + timeptr->tm_mday, timeptr->tm_hour, + timeptr->tm_min, timeptr->tm_sec); + /* XXX: since the fields used by snprintf above are validated in checktm, + * the following condition should never trigger. We keep the check because + * historically fixed size buffer used in asctime was the source of + * crashes. */ + if (n + 1 != sizeof(buf)) { + PyErr_SetString(PyExc_ValueError, "unconvertible time"); + return NULL; + } + + return PyString_FromFormat("%s %d", buf, 1900 + timeptr->tm_year); +} static PyObject * time_asctime(PyObject *self, PyObject *args) { PyObject *tup = NULL; struct tm buf; - char *p; + if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup)) return NULL; if (tup == NULL) { time_t tt = time(NULL); buf = *localtime(&tt); - } else if (!gettmarg(tup, &buf)) + } else if (!gettmarg(tup, &buf) || !checktm(&buf)) return NULL; - p = asctime(&buf); - if (p == NULL) { - PyErr_SetString(PyExc_ValueError, "invalid time"); - return NULL; - } - if (p[24] == '\n') - p[24] = '\0'; - return PyString_FromString(p); + return _asctime(&buf); } PyDoc_STRVAR(asctime_doc, @@ -593,7 +632,7 @@ { PyObject *ot = NULL; time_t tt; - char *p; + struct tm *timeptr; if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot)) return NULL; @@ -607,14 +646,12 @@ if (tt == (time_t)-1 && PyErr_Occurred()) return NULL; } - p = ctime(&tt); - if (p == NULL) { + timeptr = localtime(&tt); + if (timeptr == NULL) { PyErr_SetString(PyExc_ValueError, "unconvertible time"); return NULL; } - if (p[24] == '\n') - p[24] = '\0'; - return PyString_FromString(p); + return _asctime(timeptr); } PyDoc_STRVAR(ctime_doc,