summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2008-12-06 21:27:53 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2008-12-06 21:27:53 (GMT)
commit5504e893f84f07d5930094f07bcd479d859b5f0c (patch)
tree6e1f23c5a9e2ddfcb1a591fd97e07a120674e745 /Objects
parente6d4a9bdbc73a5314890d41a6e1e447ea848998f (diff)
downloadcpython-5504e893f84f07d5930094f07bcd479d859b5f0c.zip
cpython-5504e893f84f07d5930094f07bcd479d859b5f0c.tar.gz
cpython-5504e893f84f07d5930094f07bcd479d859b5f0c.tar.bz2
Issue #4509: bugs in bytearray with exports (buffer protocol)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytearrayobject.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 707c844..1620652 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -100,6 +100,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 *
@@ -180,6 +191,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;
@@ -199,16 +217,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();
@@ -473,6 +481,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------>|
@@ -605,6 +617,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
@@ -640,6 +654,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;
@@ -1401,7 +1417,7 @@ bytes_translate(PyByteArrayObject *self, PyObject *args)
}
goto done;
}
-
+
for (i = 0; i < 256; i++)
trans_table[i] = Py_CHARMASK(table[i]);
@@ -2659,6 +2675,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);
@@ -2689,6 +2707,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)