Amaury Forgeot d'Arc avatar Amaury Forgeot d'Arc committed 1b2afcc

#7419: Fix a crash on Windows in locale.setlocale() when the category
is outside the allowed range.

Comments (0)

Files changed (3)

Lib/test/test_locale.py

             # 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 = [
 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.
 

Modules/_localemodule.c

     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);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.