Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.

This commit is contained in:
Senthil Kumaran 2011-04-06 14:27:47 +08:00
parent 5f511826c2
commit 792eb5dc84
2 changed files with 9 additions and 1 deletions

View File

@ -1,6 +1,7 @@
from test import test_support from test import test_support
import time import time
import unittest import unittest
import sys
class TimeTestCase(unittest.TestCase): class TimeTestCase(unittest.TestCase):
@ -37,6 +38,13 @@ class TimeTestCase(unittest.TestCase):
except ValueError: except ValueError:
self.fail('conversion specifier: %r failed.' % format) self.fail('conversion specifier: %r failed.' % format)
# Issue #10762: Guard against invalid/non-supported format string
# so that Python don't crash (Windows crashes when the format string
# input to [w]strftime is not kosher.
if sys.platform.startswith('win'):
with self.assertRaises(ValueError):
time.strftime('%f')
def test_strftime_bounds_checking(self): def test_strftime_bounds_checking(self):
# Make sure that strftime() checks the bounds of the various parts # Make sure that strftime() checks the bounds of the various parts
#of the time tuple (0 is valid for *all* values). #of the time tuple (0 is valid for *all* values).

View File

@ -487,7 +487,7 @@ time_strftime(PyObject *self, PyObject *args)
if (outbuf[1]=='#') if (outbuf[1]=='#')
++outbuf; /* not documented by python, */ ++outbuf; /* not documented by python, */
if (outbuf[1]=='\0' || if (outbuf[1]=='\0' ||
!strchr("aAbBcdfHIjmMpSUwWxXyYzZ%", outbuf[1])) !strchr("aAbBcdHIjmMpSUwWxXyYzZ%", outbuf[1]))
{ {
PyErr_SetString(PyExc_ValueError, "Invalid format string"); PyErr_SetString(PyExc_ValueError, "Invalid format string");
return 0; return 0;