summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew M. Kuchling <amk@amk.ca>2006-10-05 17:18:13 +0000
committerAndrew M. Kuchling <amk@amk.ca>2006-10-05 17:18:13 +0000
commit1af9f68b4a6f8da34481670b9741854081344321 (patch)
treef7f0c326816ebeea40890c3b420bdf12a7a4d7c7
parent68e1e0372baab7d9734c219258cf84c82e28ab9d (diff)
downloadcpython-git-1af9f68b4a6f8da34481670b9741854081344321.tar.gz
[Backport r51248 | neal.norwitz]
Fix segfault when doing string formatting on subclasses of long if __oct__, __hex__ don't return a string. Klocwork 308
-rw-r--r--Lib/test/test_format.py8
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/stringobject.c5
3 files changed, 15 insertions, 2 deletions
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index 29594475c2..a9b31707a2 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -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:
diff --git a/Misc/NEWS b/Misc/NEWS
index 7b7ce1c482..d68e31ff35 100644
--- 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.
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index e7ce15f0c5..9abd6dcf52 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -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;