]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[Backport r51248 | neal.norwitz]
authorAndrew M. Kuchling <amk@amk.ca>
Thu, 5 Oct 2006 17:18:13 +0000 (17:18 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Thu, 5 Oct 2006 17:18:13 +0000 (17:18 +0000)
Fix segfault when doing string formatting on subclasses of long if
__oct__, __hex__ don't return a string.

Klocwork 308

Lib/test/test_format.py
Misc/NEWS
Objects/stringobject.c

index 29594475c25339ec3c181a956d93766f01d40ddd..a9b31707a24917c8e357fb136998a9588a413630 100644 (file)
@@ -230,6 +230,14 @@ test_exc(u'no format', '1', TypeError,
 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:
index 7b7ce1c482aac1315dd64e2e1b195ab7968cb1aa..d68e31ff354b800e4ef1b33341056f273a5de75a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,7 +30,9 @@ Core and builtins
 - Fix memory leak of coding spec in Parser/tokenizer.c.
 
 - Fix memory leak in file_init.
+
+- Fix segfault when doing string formatting on subclasses of long.
+
 - Overflow checking code in integer division ran afoul of new gcc
   optimizations.  Changed to be more standard-conforming.
 
index e7ce15f0c5c533b52ede9794025dfafdf75ae1ea..9abd6dcf5249ed3e0ac6c9f52abecd372b7826d5 100644 (file)
@@ -3665,12 +3665,15 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
        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);
        len = PyString_Size(result);
        if (buf[len-1] == 'L') {
                --len;