except ValueError:
self.fail('conversion specifier: %r failed.' % format)
+ def test_strftime_bounds_checking(self):
+ # Make sure that strftime() checks the bounds of the various parts
+ #of the time tuple.
+
+ # Check year
+ self.assertRaises(ValueError, time.strftime, '',
+ (1899, 1, 1, 0, 0, 0, 0, 1, -1))
+ if time.accept2dyear:
+ self.assertRaises(ValueError, time.strftime, '',
+ (-1, 1, 1, 0, 0, 0, 0, 1, -1))
+ self.assertRaises(ValueError, time.strftime, '',
+ (100, 1, 1, 0, 0, 0, 0, 1, -1))
+ # Check month
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 0, 1, 0, 0, 0, 0, 1, -1))
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 13, 1, 0, 0, 0, 0, 1, -1))
+ # Check day of month
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 0, 0, 0, 0, 0, 1, -1))
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 32, 0, 0, 0, 0, 1, -1))
+ # Check hour
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 1, -1, 0, 0, 0, 1, -1))
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 1, 24, 0, 0, 0, 1, -1))
+ # Check minute
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 1, 0, -1, 0, 0, 1, -1))
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 1, 0, 60, 0, 0, 1, -1))
+ # Check second
+ self.assertRaises(ValueError, time.strftime, '',
+ (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, '',
+ (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, '',
+ (1900, 1, 1, 0, 0, 0, -2, 1, -1))
+ # Check day of the year
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 1, 0, 0, 0, 0, 0, -1))
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 1, 0, 0, 0, 0, 367, -1))
+ # Check daylight savings flag
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 1, 0, 0, 0, 0, 1, -2))
+ self.assertRaises(ValueError, time.strftime, '',
+ (1900, 1, 1, 0, 0, 0, 0, 1, 2))
+
def test_strptime(self):
tt = time.gmtime(self.t)
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
} else if (!gettmarg(tup, &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).
+
+ No check for year since handled in gettmarg().
+ */
+ if (buf.tm_mon < 0 || buf.tm_mon > 11) {
+ PyErr_SetString(PyExc_ValueError, "month out of range");
+ return NULL;
+ }
+ if (buf.tm_mday < 1 || 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 < 0 || buf.tm_yday > 365) {
+ PyErr_SetString(PyExc_ValueError, "day of year out of range");
+ return NULL;
+ }
+ if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "daylight savings flag out of range");
+ return NULL;
+ }
+
fmtlen = strlen(fmt);
/* I hate these functions that presume you know how big the output