diff options
author | Guido van Rossum <guido@python.org> | 2007-05-04 04:27:16 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-05-04 04:27:16 (GMT) |
commit | 6c1e6741f8e3f44fbf7dc05e3ec79a1290c63c11 (patch) | |
tree | 5dfdaae9dcdc8d968588a22f47c1b7c500f920df /Objects/bytesobject.c | |
parent | 09dc34fc9c51f37f94cd9030ab8760677b360396 (diff) | |
download | cpython-6c1e6741f8e3f44fbf7dc05e3ec79a1290c63c11.zip cpython-6c1e6741f8e3f44fbf7dc05e3ec79a1290c63c11.tar.gz cpython-6c1e6741f8e3f44fbf7dc05e3ec79a1290c63c11.tar.bz2 |
Add trailing null bytes to a few more places.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r-- | Objects/bytesobject.c | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 75b7939..987a3c5 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -138,11 +138,12 @@ PyBytes_Resize(PyObject *self, Py_ssize_t size) if (size < alloc / 2) { /* Major downsize; resize down to exact size */ - alloc = size; + alloc = size + 1; } else if (size < alloc) { /* Within allocated size; quick exit */ ((PyBytesObject *)self)->ob_size = size; + ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */ return 0; } else if (size <= alloc * 1.125) { @@ -151,10 +152,8 @@ PyBytes_Resize(PyObject *self, Py_ssize_t size) } else { /* Major upsize; resize up to exact size */ - alloc = size; - } - if (alloc <= size) alloc = size + 1; + } sval = PyMem_Realloc(((PyBytesObject *)self)->ob_bytes, alloc); if (sval == NULL) { @@ -165,7 +164,6 @@ PyBytes_Resize(PyObject *self, Py_ssize_t size) ((PyBytesObject *)self)->ob_bytes = sval; ((PyBytesObject *)self)->ob_size = size; ((PyBytesObject *)self)->ob_alloc = alloc; - ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */ return 0; @@ -231,8 +229,10 @@ bytes_iconcat(PyBytesObject *self, PyObject *other) size = mysize + osize; if (size < 0) return PyErr_NoMemory(); - if (size < self->ob_alloc) + if (size < self->ob_alloc) { self->ob_size = size; + self->ob_bytes[self->ob_size] = '\0'; /* Trailing null byte */ + } else if (PyBytes_Resize((PyObject *)self, size) < 0) return NULL; memcpy(self->ob_bytes + mysize, optr, osize); @@ -278,8 +278,10 @@ bytes_irepeat(PyBytesObject *self, Py_ssize_t count) size = mysize * count; if (count != 0 && size / count != mysize) return PyErr_NoMemory(); - if (size < self->ob_alloc) + if (size < self->ob_alloc) { self->ob_size = size; + self->ob_bytes[self->ob_size] = '\0'; /* Trailing null byte */ + } else if (PyBytes_Resize((PyObject *)self, size) < 0) return NULL; @@ -713,8 +715,10 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds) } bytes = PyString_AS_STRING(encoded); size = PyString_GET_SIZE(encoded); - if (size < self->ob_alloc) + if (size < self->ob_alloc) { self->ob_size = size; + self->ob_bytes[self->ob_size] = '\0'; /* Trailing null byte */ + } else if (PyBytes_Resize((PyObject *)self, size) < 0) { Py_DECREF(encoded); return -1; |