summaryrefslogtreecommitdiffstats
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-09-29 23:11:05 (GMT)
committerBenjamin Peterson <benjamin@python.org>2014-09-29 23:11:05 (GMT)
commit36a24f3f43c5eab4f02bbff5d7c47accfb509f65 (patch)
tree7a6bcc321016328e14e630c58f7568f1b39ecacf /Objects/bytesobject.c
parent10e4b2545e62614c5f2c6519a02af7508e153889 (diff)
parent18f836fb658a5a545e8f9e3c72751759836322f8 (diff)
downloadcpython-36a24f3f43c5eab4f02bbff5d7c47accfb509f65.zip
cpython-36a24f3f43c5eab4f02bbff5d7c47accfb509f65.tar.gz
cpython-36a24f3f43c5eab4f02bbff5d7c47accfb509f65.tar.bz2
merge 3.4 (#22519)
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 4f2da4c..caea124 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -629,28 +629,27 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
newsize = 3; /* b'' */
s = (unsigned char*)op->ob_sval;
for (i = 0; i < length; i++) {
+ Py_ssize_t incr = 1;
switch(s[i]) {
- case '\'': squotes++; newsize++; break;
- case '"': dquotes++; newsize++; break;
+ case '\'': squotes++; break;
+ case '"': dquotes++; break;
case '\\': case '\t': case '\n': case '\r':
- newsize += 2; break; /* \C */
+ incr = 2; break; /* \C */
default:
if (s[i] < ' ' || s[i] >= 0x7f)
- newsize += 4; /* \xHH */
- else
- newsize++;
+ incr = 4; /* \xHH */
}
+ if (newsize > PY_SSIZE_T_MAX - incr)
+ goto overflow;
+ newsize += incr;
}
quote = '\'';
if (smartquotes && squotes && !dquotes)
quote = '"';
- if (squotes && quote == '\'')
+ if (squotes && quote == '\'') {
+ if (newsize > PY_SSIZE_T_MAX - squotes)
+ goto overflow;
newsize += squotes;
-
- if (newsize > (PY_SSIZE_T_MAX - sizeof(PyUnicodeObject) - 1)) {
- PyErr_SetString(PyExc_OverflowError,
- "bytes object is too large to make repr");
- return NULL;
}
v = PyUnicode_New(newsize, 127);
@@ -682,6 +681,11 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
*p++ = quote;
assert(_PyUnicode_CheckConsistency(v, 1));
return v;
+
+ overflow:
+ PyErr_SetString(PyExc_OverflowError,
+ "bytes object is too large to make repr");
+ return NULL;
}
static PyObject *