]> 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 80e921a44e5aff0ed02664a02794b9d0cab3f794..61347de11e75a787ef53fddda771c51dac13cb82 100644 (file)
@@ -800,6 +800,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)), "")
+
     @no_tracing
     def test_badisinstance(self):
         # Bug #2542: if issubclass(e, MyException) raises an exception,
index 0458c4ec794c029fa1b802d29a3abc1bad907bd9..9e59154011eb2865a1186477e8f48711e154634f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: TBA
 Core and Builtins
 -----------------
 
+- Issue #21134: Fix segfault when str is called on an uninitialized
+  UnicodeEncodeError, UnicodeDecodeError, or UnicodeTranslateError object.
+
 - Issue #19537: Fix PyUnicode_DATA() alignment under m68k.  Patch by
   Andreas Schwab.
 
index 44e60dd0440c3f75c47cc59e0a2390832a177902..861dbc7d0ae28e20d675a8779e5faa83db6e4115 100644 (file)
@@ -1837,6 +1837,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);
@@ -1955,6 +1959,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);
@@ -2049,6 +2057,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);