summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-01-17 12:38:54 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-01-17 12:38:54 (GMT)
commitfc8d6f4b73c995b6de2e2b1058140192cf1f7e72 (patch)
tree497231809932ff9a24c549b6d103e02f73779a0b /Objects
parent1119a649167f8057f75b8fd26fa6cd2c1fc59a83 (diff)
downloadcpython-fc8d6f4b73c995b6de2e2b1058140192cf1f7e72.zip
cpython-fc8d6f4b73c995b6de2e2b1058140192cf1f7e72.tar.gz
cpython-fc8d6f4b73c995b6de2e2b1058140192cf1f7e72.tar.bz2
Merged revisions 77573 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r77573 | antoine.pitrou | 2010-01-17 13:26:20 +0100 (dim., 17 janv. 2010) | 6 lines Issue #7561: Operations on empty bytearrays (such as `int(bytearray())`) could crash in many places because of the PyByteArray_AS_STRING() macro returning NULL. The macro now returns a statically allocated empty string instead. ........
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytearrayobject.c17
1 files changed, 3 insertions, 14 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 823c800..7d52522 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -5,23 +5,16 @@
#include "structmember.h"
#include "bytes_methods.h"
-static PyByteArrayObject *nullbytes = NULL;
+char _PyByteArray_empty_string[] = "";
void
PyByteArray_Fini(void)
{
- Py_CLEAR(nullbytes);
}
int
PyByteArray_Init(void)
{
- nullbytes = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
- if (nullbytes == NULL)
- return 0;
- nullbytes->ob_bytes = NULL;
- Py_SIZE(nullbytes) = nullbytes->ob_alloc = 0;
- nullbytes->ob_exports = 0;
return 1;
}
@@ -65,10 +58,7 @@ bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
obj->ob_exports++;
return 0;
}
- if (obj->ob_bytes == NULL)
- ptr = "";
- else
- ptr = obj->ob_bytes;
+ ptr = (void *) PyByteArray_AS_STRING(obj);
ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
if (ret >= 0) {
obj->ob_exports++;
@@ -152,7 +142,7 @@ PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
Py_DECREF(new);
return PyErr_NoMemory();
}
- if (bytes != NULL)
+ if (bytes != NULL && size > 0)
memcpy(new->ob_bytes, bytes, size);
new->ob_bytes[size] = '\0'; /* Trailing null byte */
}
@@ -1038,7 +1028,6 @@ bytearray_dealloc(PyByteArrayObject *self)
#define STRINGLIB_LEN PyByteArray_GET_SIZE
#define STRINGLIB_STR PyByteArray_AS_STRING
#define STRINGLIB_NEW PyByteArray_FromStringAndSize
-#define STRINGLIB_EMPTY nullbytes
#define STRINGLIB_ISSPACE Py_ISSPACE
#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact