[Python-checkins] cpython (2.7): Issue #16445: Fix potential segmentation fault when deleting an exception

mark.dickinson python-checkins at python.org
Sun Mar 3 12:13:48 CET 2013


http://hg.python.org/cpython/rev/0e41c4466d58
changeset:   82458:0e41c4466d58
branch:      2.7
parent:      82446:2f500533e9b9
user:        Mark Dickinson <dickinsm at gmail.com>
date:        Sun Mar 03 11:13:34 2013 +0000
summary:
  Issue #16445: Fix potential segmentation fault when deleting an exception message.

files:
  Lib/test/test_exceptions.py |  12 ++++++++++++
  Misc/NEWS                   |   3 +++
  Objects/exceptions.c        |   3 +--
  3 files changed, 16 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -479,6 +479,18 @@
         except AssertionError as e:
             self.assertEqual(str(e), "(3,)")
 
+    def test_bad_exception_clearing(self):
+        # See issue 16445: use of Py_XDECREF instead of Py_CLEAR in
+        # BaseException_set_message gave a possible way to segfault the
+        # interpreter.
+        class Nasty(str):
+            def __del__(message):
+                del e.message
+
+        e = ValueError(Nasty("msg"))
+        e.args = ()
+        del e.message
+
 
 # Helper class used by TestSameStrAndUnicodeMsg
 class ExcWithOverriddenStr(Exception):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@
 Core and Builtins
 -----------------
 
+- Issue #16445: Fixed potential segmentation fault when deleting an exception
+  message.
+
 - Issue #17275: Corrected class name in init error messages of the C version of
   BufferedWriter and BufferedRandom.
 
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -349,8 +349,7 @@
             if (PyDict_DelItemString(self->dict, "message") < 0)
                 return -1;
         }
-        Py_XDECREF(self->message);
-        self->message = NULL;
+        Py_CLEAR(self->message);
         return 0;
     }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list