summaryrefslogtreecommitdiffstats
path: root/Objects/bytearrayobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-07-10 17:48:43 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-07-10 17:48:43 (GMT)
commit06cfb0cd7037795cc7dca2729a241ed2a1fb1628 (patch)
treebad810ee3e949b78287631096ea2fe6e444c927b /Objects/bytearrayobject.c
parent537ad7ad9fdefa44fdfd7f5cbee198ad381deb60 (diff)
downloadcpython-06cfb0cd7037795cc7dca2729a241ed2a1fb1628.zip
cpython-06cfb0cd7037795cc7dca2729a241ed2a1fb1628.tar.gz
cpython-06cfb0cd7037795cc7dca2729a241ed2a1fb1628.tar.bz2
Issue #27473: Fixed possible integer overflow in bytes and bytearray
concatenations. Patch by Xiang Zhang.
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r--Objects/bytearrayobject.c21
1 files changed, 9 insertions, 12 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 388e990..b21acfb 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -246,7 +246,6 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
PyObject *
PyByteArray_Concat(PyObject *a, PyObject *b)
{
- Py_ssize_t size;
Py_buffer va, vb;
PyByteArrayObject *result = NULL;
@@ -259,13 +258,13 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
goto done;
}
- size = va.len + vb.len;
- if (size < 0) {
- PyErr_NoMemory();
- goto done;
+ if (va.len > PY_SSIZE_T_MAX - vb.len) {
+ PyErr_NoMemory();
+ goto done;
}
- result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
+ result = (PyByteArrayObject *) \
+ PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
if (result != NULL) {
memcpy(result->ob_bytes, va.buf, va.len);
memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
@@ -315,7 +314,6 @@ bytearray_length(PyByteArrayObject *self)
static PyObject *
bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
{
- Py_ssize_t mysize;
Py_ssize_t size;
Py_buffer vo;
@@ -325,17 +323,16 @@ bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
return NULL;
}
- mysize = Py_SIZE(self);
- size = mysize + vo.len;
- if (size < 0) {
+ size = Py_SIZE(self);
+ if (size > PY_SSIZE_T_MAX - vo.len) {
PyBuffer_Release(&vo);
return PyErr_NoMemory();
}
- if (PyByteArray_Resize((PyObject *)self, size) < 0) {
+ if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
PyBuffer_Release(&vo);
return NULL;
}
- memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len);
+ memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
PyBuffer_Release(&vo);
Py_INCREF(self);
return (PyObject *)self;