diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2008-12-07 00:07:51 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2008-12-07 00:07:51 (GMT) |
commit | 599db7f133ee842e94c89ceaa1724a0a84dded11 (patch) | |
tree | 0bcca713ebdd5e30abc4c1ed0da8cbcda3b47608 /Objects | |
parent | b7387a5e0d6e7adc28758e1f7597387d318e6ea7 (diff) | |
download | cpython-599db7f133ee842e94c89ceaa1724a0a84dded11.zip cpython-599db7f133ee842e94c89ceaa1724a0a84dded11.tar.gz cpython-599db7f133ee842e94c89ceaa1724a0a84dded11.tar.bz2 |
Merged revisions 67619 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r67619 | antoine.pitrou | 2008-12-06 22:29:24 +0100 (sam., 06 déc. 2008) | 1 line
Issue #4509: bugs in bytearray with exports (buffer protocol)
........
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/bytearrayobject.c | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 99bb0f2..d75eb53 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -154,6 +154,17 @@ _getbuffer(PyObject *obj, Py_buffer *view) return view->len; } +static int +_canresize(PyByteArrayObject *self) +{ + if (self->ob_exports > 0) { + PyErr_SetString(PyExc_BufferError, + "Existing exports of data: object cannot be re-sized"); + return 0; + } + return 1; +} + /* Direct API functions */ PyObject * @@ -229,6 +240,13 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t size) assert(PyByteArray_Check(self)); assert(size >= 0); + if (size == Py_SIZE(self)) { + return 0; + } + if (!_canresize((PyByteArrayObject *)self)) { + return -1; + } + if (size < alloc / 2) { /* Major downsize; resize down to exact size */ alloc = size + 1; @@ -248,16 +266,6 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t size) alloc = size + 1; } - if (((PyByteArrayObject *)self)->ob_exports > 0) { - /* - fprintf(stderr, "%d: %s", ((PyByteArrayObject *)self)->ob_exports, - ((PyByteArrayObject *)self)->ob_bytes); - */ - PyErr_SetString(PyExc_BufferError, - "Existing exports of data: object cannot be re-sized"); - return -1; - } - sval = PyMem_Realloc(((PyByteArrayObject *)self)->ob_bytes, alloc); if (sval == NULL) { PyErr_NoMemory(); @@ -522,6 +530,10 @@ bytes_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, if (avail != needed) { if (avail > needed) { + if (!_canresize(self)) { + res = -1; + goto finish; + } /* 0 lo hi old_size | |<----avail----->|<-----tomove------>| @@ -654,6 +666,8 @@ bytes_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) stop = start; if (step == 1) { if (slicelen != needed) { + if (!_canresize(self)) + return -1; if (slicelen > needed) { /* 0 start stop old_size @@ -689,6 +703,8 @@ bytes_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) /* Delete slice */ Py_ssize_t cur, i; + if (!_canresize(self)) + return -1; if (step < 0) { stop = start + 1; start = stop + step * (slicelen - 1) - 1; @@ -1473,7 +1489,7 @@ bytes_translate(PyByteArrayObject *self, PyObject *args) } goto done; } - + for (i = 0; i < 256; i++) trans_table[i] = Py_CHARMASK(table[i]); @@ -2730,6 +2746,8 @@ bytes_pop(PyByteArrayObject *self, PyObject *args) PyErr_SetString(PyExc_IndexError, "pop index out of range"); return NULL; } + if (!_canresize(self)) + return NULL; value = self->ob_bytes[where]; memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); @@ -2760,6 +2778,8 @@ bytes_remove(PyByteArrayObject *self, PyObject *arg) PyErr_SetString(PyExc_ValueError, "value not found in bytes"); return NULL; } + if (!_canresize(self)) + return NULL; memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |