summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-07-12 12:46:57 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-07-12 12:46:57 (GMT)
commit373773d5b2790a65ad99a0b855709245544afc30 (patch)
tree2f129ce993ce14e8e80ca1bc73eb02c88d72b64f /Objects/stringobject.c
parentbf2dca96fbda4193552a56f7f1258ba5d16f2a60 (diff)
downloadcpython-373773d5b2790a65ad99a0b855709245544afc30.zip
cpython-373773d5b2790a65ad99a0b855709245544afc30.tar.gz
cpython-373773d5b2790a65ad99a0b855709245544afc30.tar.bz2
Issue #27473: Fixed possible integer overflow in str, unicode and bytearray
concatenations and repetitions. Based on patch by Xiang Zhang.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 1a04b78..342b2db 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -1040,7 +1040,6 @@ string_concat(register PyStringObject *a, register PyObject *bb)
Py_INCREF(a);
return (PyObject *)a;
}
- size = Py_SIZE(a) + Py_SIZE(b);
/* Check that string sizes are not negative, to prevent an
overflow in cases where we are passed incorrectly-created
strings with negative lengths (due to a bug in other code).
@@ -1051,6 +1050,7 @@ string_concat(register PyStringObject *a, register PyObject *bb)
"strings are too large to concat");
return NULL;
}
+ size = Py_SIZE(a) + Py_SIZE(b);
/* Inline PyObject_NewVar */
if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) {
@@ -1081,15 +1081,15 @@ string_repeat(register PyStringObject *a, register Py_ssize_t n)
size_t nbytes;
if (n < 0)
n = 0;
- /* watch out for overflows: the size can overflow int,
+ /* watch out for overflows: the size can overflow Py_ssize_t,
* and the # of bytes needed can overflow size_t
*/
- size = Py_SIZE(a) * n;
- if (n && size / n != Py_SIZE(a)) {
+ if (n && Py_SIZE(a) > PY_SSIZE_T_MAX / n) {
PyErr_SetString(PyExc_OverflowError,
"repeated string is too long");
return NULL;
}
+ size = Py_SIZE(a) * n;
if (size == Py_SIZE(a) && PyString_CheckExact(a)) {
Py_INCREF(a);
return (PyObject *)a;