[Python-checkins] r51248 - in python/trunk: Lib/test/test_format.py Misc/NEWS Objects/stringobject.c

neal.norwitz python-checkins at python.org
Sun Aug 13 20:11:09 CEST 2006


Author: neal.norwitz
Date: Sun Aug 13 20:11:08 2006
New Revision: 51248

Modified:
   python/trunk/Lib/test/test_format.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/stringobject.c
Log:
Fix segfault when doing string formatting on subclasses of long if
__oct__, __hex__ don't return a string.

Klocwork 308



Modified: python/trunk/Lib/test/test_format.py
==============================================================================
--- python/trunk/Lib/test/test_format.py	(original)
+++ python/trunk/Lib/test/test_format.py	Sun Aug 13 20:11:08 2006
@@ -230,6 +230,14 @@
 test_exc(u'no format', u'1', TypeError,
          "not all arguments converted during string formatting")
 
+class Foobar(long):
+    def __oct__(self):
+        # Returning a non-string should not blow up.
+        return self + 1
+
+test_exc('%o', Foobar(), TypeError,
+         "expected string or Unicode object, long found")
+
 if sys.maxint == 2**31-1:
     # crashes 2.2.1 and earlier:
     try:

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sun Aug 13 20:11:08 2006
@@ -12,6 +12,8 @@
 Core and builtins
 -----------------
 
+- Fix segfault when doing string formatting on subclasses of long.
+
 - Fix bug related to __len__ functions using values > 2**32 on 64-bit machines
   with new-style classes.
   

Modified: python/trunk/Objects/stringobject.c
==============================================================================
--- python/trunk/Objects/stringobject.c	(original)
+++ python/trunk/Objects/stringobject.c	Sun Aug 13 20:11:08 2006
@@ -4225,12 +4225,15 @@
 	if (!result)
 		return NULL;
 
+	buf = PyString_AsString(result);
+	if (!buf)
+		return NULL;
+
 	/* To modify the string in-place, there can only be one reference. */
 	if (result->ob_refcnt != 1) {
 		PyErr_BadInternalCall();
 		return NULL;
 	}
-	buf = PyString_AsString(result);
 	llen = PyString_Size(result);
 	if (llen > PY_SSIZE_T_MAX) {
 		PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong");


More information about the Python-checkins mailing list