changeset: 90111:afa7fb2cbe3b branch: 2.7 parent: 90101:1c112eb4eb56 user: Benjamin Peterson date: Wed Apr 02 12:15:06 2014 -0400 files: Lib/test/test_exceptions.py Misc/NEWS Objects/exceptions.c description: bail in unicode error's __str__ methods if the objects are not properly initialized (closes #21134) diff -r 1c112eb4eb56 -r afa7fb2cbe3b Lib/test/test_exceptions.py --- a/Lib/test/test_exceptions.py Tue Apr 01 22:11:34 2014 -0700 +++ b/Lib/test/test_exceptions.py Wed Apr 02 12:15:06 2014 -0400 @@ -431,6 +431,12 @@ 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 diff -r 1c112eb4eb56 -r afa7fb2cbe3b Misc/NEWS --- a/Misc/NEWS Tue Apr 01 22:11:34 2014 -0700 +++ b/Misc/NEWS Wed Apr 02 12:15:06 2014 -0400 @@ -12,6 +12,9 @@ - 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. diff -r 1c112eb4eb56 -r afa7fb2cbe3b Objects/exceptions.c --- a/Objects/exceptions.c Tue Apr 01 22:11:34 2014 -0700 +++ b/Objects/exceptions.c Wed Apr 02 12:15:06 2014 -0400 @@ -1648,6 +1648,10 @@ 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 @@ 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 @@ 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);