[Python-checkins] r65342 - in python/trunk: Lib/test/test_unicode.py Objects/unicodeobject.c

amaury.forgeotdarc python-checkins at python.org
Fri Aug 1 01:39:06 CEST 2008


Author: amaury.forgeotdarc
Date: Fri Aug  1 01:39:05 2008
New Revision: 65342

Log:
Correct a crash when two successive unicode allocations fail with a MemoryError:
the freelist contained half-initialized objects with freed pointers.

The comment 
/* XXX UNREF/NEWREF interface should be more symmetrical */
was copied from tupleobject.c, and appears in some other places.
I sign the petition.


Modified:
   python/trunk/Lib/test/test_unicode.py
   python/trunk/Objects/unicodeobject.c

Modified: python/trunk/Lib/test/test_unicode.py
==============================================================================
--- python/trunk/Lib/test/test_unicode.py	(original)
+++ python/trunk/Lib/test/test_unicode.py	Fri Aug  1 01:39:05 2008
@@ -1113,6 +1113,20 @@
         #  will fail
         self.assertRaises(UnicodeEncodeError, "foo{0}".format, u'\u1000bar')
 
+    def test_raiseMemError(self):
+        # Ensure that the freelist contains a consistent object, even
+        # when a string allocation fails with a MemoryError.
+        # This used to crash the interpreter,
+        # or leak references when the number was smaller.
+        try:
+            u"a" * (sys.maxint // 2 - 100)
+        except MemoryError:
+            pass
+        try:
+            u"a" * (sys.maxint // 2 - 100)
+        except MemoryError:
+            pass
+
 def test_main():
     test_support.run_unittest(__name__)
 

Modified: python/trunk/Objects/unicodeobject.c
==============================================================================
--- python/trunk/Objects/unicodeobject.c	(original)
+++ python/trunk/Objects/unicodeobject.c	Fri Aug  1 01:39:05 2008
@@ -315,7 +315,7 @@
 	    if ((unicode->length < length) &&
                 unicode_resize(unicode, length) < 0) {
 		PyObject_DEL(unicode->str);
-		goto onError;
+		unicode->str = NULL;
 	    }
 	}
         else {
@@ -352,6 +352,8 @@
     return unicode;
 
  onError:
+    /* XXX UNREF/NEWREF interface should be more symmetrical */
+    _Py_DEC_REFTOTAL;
     _Py_ForgetReference((PyObject *)unicode);
     PyObject_Del(unicode);
     return NULL;


More information about the Python-checkins mailing list