Commit ca0da9b0 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #27473: Fixed possible integer overflow in bytes and bytearray

concatenations.  Patch by Xiang Zhang.
parents ce85acff 06cfb0cd
...@@ -10,6 +10,9 @@ What's New in Python 3.6.0 alpha 3 ...@@ -10,6 +10,9 @@ What's New in Python 3.6.0 alpha 3
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #27473: Fixed possible integer overflow in bytes and bytearray
concatenations. Patch by Xiang Zhang.
- Issue #23034: The output of a special Python build with defined COUNT_ALLOCS, - Issue #23034: The output of a special Python build with defined COUNT_ALLOCS,
SHOW_ALLOC_COUNT or SHOW_TRACK_COUNT macros is now off by default. It can SHOW_ALLOC_COUNT or SHOW_TRACK_COUNT macros is now off by default. It can
be re-enabled using the "-X showalloccount" option. It now outputs to stderr be re-enabled using the "-X showalloccount" option. It now outputs to stderr
......
...@@ -246,7 +246,6 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) ...@@ -246,7 +246,6 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
PyObject * PyObject *
PyByteArray_Concat(PyObject *a, PyObject *b) PyByteArray_Concat(PyObject *a, PyObject *b)
{ {
Py_ssize_t size;
Py_buffer va, vb; Py_buffer va, vb;
PyByteArrayObject *result = NULL; PyByteArrayObject *result = NULL;
...@@ -259,13 +258,13 @@ PyByteArray_Concat(PyObject *a, PyObject *b) ...@@ -259,13 +258,13 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
goto done; goto done;
} }
size = va.len + vb.len; if (va.len > PY_SSIZE_T_MAX - vb.len) {
if (size < 0) {
PyErr_NoMemory(); PyErr_NoMemory();
goto done; goto done;
} }
result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size); result = (PyByteArrayObject *) \
PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
if (result != NULL) { if (result != NULL) {
memcpy(result->ob_bytes, va.buf, va.len); memcpy(result->ob_bytes, va.buf, va.len);
memcpy(result->ob_bytes + va.len, vb.buf, vb.len); memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
...@@ -290,7 +289,6 @@ bytearray_length(PyByteArrayObject *self) ...@@ -290,7 +289,6 @@ bytearray_length(PyByteArrayObject *self)
static PyObject * static PyObject *
bytearray_iconcat(PyByteArrayObject *self, PyObject *other) bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
{ {
Py_ssize_t mysize;
Py_ssize_t size; Py_ssize_t size;
Py_buffer vo; Py_buffer vo;
...@@ -300,17 +298,16 @@ bytearray_iconcat(PyByteArrayObject *self, PyObject *other) ...@@ -300,17 +298,16 @@ bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
return NULL; return NULL;
} }
mysize = Py_SIZE(self); size = Py_SIZE(self);
size = mysize + vo.len; if (size > PY_SSIZE_T_MAX - vo.len) {
if (size < 0) {
PyBuffer_Release(&vo); PyBuffer_Release(&vo);
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
if (PyByteArray_Resize((PyObject *)self, size) < 0) { if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
PyBuffer_Release(&vo); PyBuffer_Release(&vo);
return NULL; 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); PyBuffer_Release(&vo);
Py_INCREF(self); Py_INCREF(self);
return (PyObject *)self; return (PyObject *)self;
......
...@@ -1388,7 +1388,6 @@ bytes_length(PyBytesObject *a) ...@@ -1388,7 +1388,6 @@ bytes_length(PyBytesObject *a)
static PyObject * static PyObject *
bytes_concat(PyObject *a, PyObject *b) bytes_concat(PyObject *a, PyObject *b)
{ {
Py_ssize_t size;
Py_buffer va, vb; Py_buffer va, vb;
PyObject *result = NULL; PyObject *result = NULL;
...@@ -1413,13 +1412,12 @@ bytes_concat(PyObject *a, PyObject *b) ...@@ -1413,13 +1412,12 @@ bytes_concat(PyObject *a, PyObject *b)
goto done; goto done;
} }
size = va.len + vb.len; if (va.len > PY_SSIZE_T_MAX - vb.len) {
if (size < 0) {
PyErr_NoMemory(); PyErr_NoMemory();
goto done; goto done;
} }
result = PyBytes_FromStringAndSize(NULL, size); result = PyBytes_FromStringAndSize(NULL, va.len + vb.len);
if (result != NULL) { if (result != NULL) {
memcpy(PyBytes_AS_STRING(result), va.buf, va.len); memcpy(PyBytes_AS_STRING(result), va.buf, va.len);
memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment