summaryrefslogtreecommitdiffstats
path: root/Objects/bytearrayobject.c
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-11-02 17:41:56 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-11-02 17:41:56 (GMT)
commit8a03896cace7cf2b8634c1409722fe6d3f9c8bcd (patch)
tree23f5e2d94a09abf5a2c80d095c26bd7f5e391dac /Objects/bytearrayobject.c
parentc58e3a449bfcf42b4d3ec0495960472adaaa952e (diff)
parentcc23154d020723dc85d055324861f6a8f54fe0f7 (diff)
downloadcpython-8a03896cace7cf2b8634c1409722fe6d3f9c8bcd.zip
cpython-8a03896cace7cf2b8634c1409722fe6d3f9c8bcd.tar.gz
cpython-8a03896cace7cf2b8634c1409722fe6d3f9c8bcd.tar.bz2
Issue #22335: Fix crash when trying to enlarge a bytearray to 0x7fffffff bytes on a 32-bit platform.
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r--Objects/bytearrayobject.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 84447bc..47d480f 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -180,20 +180,22 @@ PyByteArray_AsString(PyObject *self)
}
int
-PyByteArray_Resize(PyObject *self, Py_ssize_t size)
+PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
{
void *sval;
PyByteArrayObject *obj = ((PyByteArrayObject *)self);
- Py_ssize_t alloc = obj->ob_alloc;
- Py_ssize_t logical_offset = obj->ob_start - obj->ob_bytes;
+ /* All computations are done unsigned to avoid integer overflows
+ (see issue #22335). */
+ size_t alloc = (size_t) obj->ob_alloc;
+ size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
+ size_t size = (size_t) requested_size;
assert(self != NULL);
assert(PyByteArray_Check(self));
- assert(size >= 0);
- assert(logical_offset >= 0);
assert(logical_offset <= alloc);
+ assert(requested_size >= 0);
- if (size == Py_SIZE(self)) {
+ if (requested_size == Py_SIZE(self)) {
return 0;
}
if (!_canresize(obj)) {
@@ -225,6 +227,10 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t size)
alloc = size + 1;
}
}
+ if (alloc > PY_SSIZE_T_MAX) {
+ PyErr_NoMemory();
+ return -1;
+ }
if (logical_offset > 0) {
sval = PyObject_Malloc(alloc);
@@ -232,7 +238,8 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t size)
PyErr_NoMemory();
return -1;
}
- memcpy(sval, PyByteArray_AS_STRING(self), Py_MIN(size, Py_SIZE(self)));
+ memcpy(sval, PyByteArray_AS_STRING(self),
+ Py_MIN(requested_size, Py_SIZE(self)));
PyObject_Free(obj->ob_bytes);
}
else {