Merged revisions 77576 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r77576 | antoine.pitrou | 2010-01-17 13:38:54 +0100 (dim., 17 janv. 2010) | 12 lines

  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.
  ........
................
This commit is contained in:
Antoine Pitrou 2010-01-17 12:43:00 +00:00
parent 1b64331f49
commit 20d6c15327
4 changed files with 21 additions and 6 deletions

View file

@ -6,6 +6,7 @@
#include "bytes_methods.h"
static PyByteArrayObject *nullbytes = NULL;
char _PyByteArray_empty_string[] = "";
void
PyByteArray_Fini(void)
@ -65,10 +66,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 +150,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 */
}