diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py index 8dfaf9052d7..8c17f8770c7 100644 --- a/Lib/test/test_locale.py +++ b/Lib/test/test_locale.py @@ -353,6 +353,17 @@ class TestMiscellaneous(unittest.TestCase): self.assertRaises(TypeError, locale.strcoll, "a", None) self.assertRaises(TypeError, locale.strcoll, b"a", None) + def test_setlocale_category(self): + locale.setlocale(locale.LC_ALL) + locale.setlocale(locale.LC_TIME) + locale.setlocale(locale.LC_CTYPE) + locale.setlocale(locale.LC_COLLATE) + locale.setlocale(locale.LC_MONETARY) + locale.setlocale(locale.LC_NUMERIC) + + # crasher from bug #7419 + self.assertRaises(locale.Error, locale.setlocale, 12345) + def test_main(): tests = [ diff --git a/Misc/NEWS b/Misc/NEWS index d3da7b9621f..46a591224bf 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1? Core and Builtins ----------------- +- Issue #7419: setlocale() could crash the interpreter on Windows when called + with invalid values. + - Issue #6077: On Windows, files opened with tempfile.TemporaryFile in "wt+" mode would appear truncated on the first '0x1a' byte (aka. Ctrl+Z). diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c index fa6ab8fc6d8..aabc3ac8646 100644 --- a/Modules/_localemodule.c +++ b/Modules/_localemodule.c @@ -133,6 +133,14 @@ PyLocale_setlocale(PyObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale)) return NULL; +#if defined(MS_WINDOWS) + if (category < LC_MIN || category > LC_MAX) + { + PyErr_SetString(Error, "invalid locale category"); + return NULL; + } +#endif + if (locale) { /* set locale */ result = setlocale(category, locale);