]> granicus.if.org Git - python/commitdiff
#7419: Fix a crash on Windows in locale.setlocale() when the category
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 1 Dec 2009 21:51:04 +0000 (21:51 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 1 Dec 2009 21:51:04 +0000 (21:51 +0000)
is outside the allowed range.

Lib/test/test_locale.py
Misc/NEWS
Modules/_localemodule.c

index d9d3cd95dc89efc18ff1e1fc586ecda9deb6fb3c..451040977ab674b8862e4723375b75977488e0e2 100644 (file)
@@ -360,6 +360,17 @@ class TestMiscellaneous(unittest.TestCase):
             # test crasher from bug #3303
             self.assertRaises(TypeError, locale.strcoll, u"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 = [
index 8ac7c24fcc09183919d53f556ea3bd3b5e454478..a8b56e275ce9b68260f9c5235c53f8cf5a8537ef 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,10 +12,13 @@ What's New in Python 2.7 alpha 1
 Core and Builtins
 -----------------
 
+- Issue #7419: setlocale() could crash the interpreter on Windows when called
+  with invalid values.
+
 - Issue #3382: 'F' formatting for float and complex now convert the
   result to upper case. This only affects 'inf' and 'nan', since 'f'
   no longer converts to 'g' for large values.
-  
+
 - Remove switch from "%f" formatting to "%g" formatting for floats
   larger than 1e50 in absolute value.
 
index 94d0014bbc09b30b89910b74173821f42b92879b..f1f4abff3e0be5ea76d29e6d1a3c2a0fe819046b 100644 (file)
@@ -163,6 +163,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);