]> granicus.if.org Git - python/commitdiff
bail in unicode error's __str__ methods if the objects are not properly initialized...
authorBenjamin Peterson <benjamin@python.org>
Wed, 2 Apr 2014 16:15:06 +0000 (12:15 -0400)
committerBenjamin Peterson <benjamin@python.org>
Wed, 2 Apr 2014 16:15:06 +0000 (12:15 -0400)
Lib/test/test_exceptions.py
Misc/NEWS
Objects/exceptions.c

index a485cba893c0e234fb5d9aa61f6bd1b47ccc7637..7f55aabdc31c7d1009477e3a9c195cb83dfa72c0 100644 (file)
@@ -431,6 +431,12 @@ class ExceptionTests(unittest.TestCase):
         u.start = 1000
         self.assertEqual(str(u), "can't translate characters in position 1000-4: 965230951443685724997")
 
+    def test_unicode_errors_no_object(self):
+        # See issue #21134.
+        klasses = UnicodeDecodeError, UnicodeDecodeError, UnicodeTranslateError
+        for klass in klasses:
+            self.assertEqual(str(klass.__new__(klass)), "")
+
     def test_badisinstance(self):
         # Bug #2542: if issubclass(e, MyException) raises an exception,
         # it should be ignored
index 24047bbd48679377ee2d6cf5c9f1c20069402205..4933cb249b7180ab2561037032cd04275a0e55a5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ Core and Builtins
 
 - Issue #20437: Fixed 43 potential bugs when deleting objects references.
 
+- Issue #21134: Fix segfault when str is called on an uninitialized
+  UnicodeEncodeError, UnicodeDecodeError, or UnicodeTranslateError object.
+
 - Issue #20494: Ensure that free()d memory arenas are really released on POSIX
   systems supporting anonymous memory mappings.  Patch by Charles-François
   Natali.
index 0f86cfb194237f39f6974dbf65de380f4e6070df..e16552823fbdca33175db220786b6f4cbf763324 100644 (file)
@@ -1648,6 +1648,10 @@ UnicodeEncodeError_str(PyObject *self)
     PyObject *reason_str = NULL;
     PyObject *encoding_str = NULL;
 
+    if (!uself->object)
+        /* Not properly initialized. */
+        return PyUnicode_FromString("");
+
     /* Get reason and encoding as strings, which they might not be if
        they've been modified after we were contructed. */
     reason_str = PyObject_Str(uself->reason);
@@ -1733,6 +1737,10 @@ UnicodeDecodeError_str(PyObject *self)
     PyObject *reason_str = NULL;
     PyObject *encoding_str = NULL;
 
+    if (!uself->object)
+        /* Not properly initialized. */
+        return PyUnicode_FromString("");
+
     /* Get reason and encoding as strings, which they might not be if
        they've been modified after we were contructed. */
     reason_str = PyObject_Str(uself->reason);
@@ -1830,6 +1838,10 @@ UnicodeTranslateError_str(PyObject *self)
     PyObject *result = NULL;
     PyObject *reason_str = NULL;
 
+    if (!uself->object)
+        /* Not properly initialized. */
+        return PyUnicode_FromString("");
+
     /* Get reason as a string, which it might not be if it's been
        modified after we were contructed. */
     reason_str = PyObject_Str(uself->reason);