]> granicus.if.org Git - python/commitdiff
Issue #13546: Fixed an overflow issue that could crash the intepreter when
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Wed, 7 Dec 2011 20:46:48 +0000 (21:46 +0100)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Wed, 7 Dec 2011 20:46:48 +0000 (21:46 +0100)
calling sys.setrecursionlimit((1<<31)-1).

2.7 only.

Lib/test/test_sys.py
Misc/NEWS
Python/errors.c

index 49f538829123a0e715795470a276aeeba5ac39b1..6c5fc2408f6503190ca823820c1407952d5c5380 100644 (file)
@@ -224,6 +224,18 @@ class SysModuleTest(unittest.TestCase):
         self.assertEqual(sys.getrecursionlimit(), 10000)
         sys.setrecursionlimit(oldlimit)
 
+        self.assertRaises(OverflowError, sys.setrecursionlimit, 1 << 31)
+        try:
+            sys.setrecursionlimit((1 << 31) - 5)
+            try:
+                # issue13546: isinstance(e, ValueError) used to fail
+                # when the recursion limit is close to 1<<31
+                raise ValueError()
+            except ValueError, e:
+                pass
+        finally:
+            sys.setrecursionlimit(oldlimit)
+
     def test_getwindowsversion(self):
         # Raise SkipTest if sys doesn't have getwindowsversion attribute
         test.test_support.get_attribute(sys, "getwindowsversion")
index bf631537802efdebe6eed8080558d485573bfb22..6a089e373b3ce208b252ae79d9b040a4920d8803 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.3?
 Core and Builtins
 -----------------
 
+- Issue #13546: Fixed an overflow issue that could crash the intepreter when
+  calling sys.setrecursionlimit((1<<31)-1).
+
 - Issue #13333: The UTF-7 decoder now accepts lone surrogates (the encoder
   already accepts them).
 
index 4294f2f29623420285b0422a21bba58132fe031e..64ba05dd6cd72190ff4f14914f5287ec8d17ebe8 100644 (file)
@@ -111,9 +111,11 @@ PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
         PyErr_Fetch(&exception, &value, &tb);
         /* Temporarily bump the recursion limit, so that in the most
            common case PyObject_IsSubclass will not raise a recursion
-           error we have to ignore anyway. */
+           error we have to ignore anyway.  Don't do it when the limit
+           is already insanely high, to avoid overflow */
         reclimit = Py_GetRecursionLimit();
-        Py_SetRecursionLimit(reclimit + 5);
+        if (reclimit < (1 << 30))
+            Py_SetRecursionLimit(reclimit + 5);
         res = PyObject_IsSubclass(err, exc);
         Py_SetRecursionLimit(reclimit);
         /* This function must not fail, so print the error here */