From bbdaf401190d0c96c4ebbe7c624c006bb9c0f142 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Wed, 25 Feb 2015 12:14:35 -0500 Subject: [PATCH] Fix #4154: Return a writable buffer from conv_color --- src/_image_wrapper.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/_image_wrapper.cpp b/src/_image_wrapper.cpp index fa8262c6ff6a..f743905b15f1 100644 --- a/src/_image_wrapper.cpp +++ b/src/_image_wrapper.cpp @@ -165,14 +165,21 @@ static PyObject *PyImage_color_conv(PyImage *self, PyObject *args, PyObject *kwd return NULL; } - PyObject *result = PyBytes_FromStringAndSize(NULL, self->x->rowsOut * self->x->colsOut * 4); - if (result == NULL) { - return NULL; + Py_ssize_t size = self->x->rowsOut * self->x->colsOut * 4; + agg::int8u *buff = (agg::int8u *)malloc(size); + if (buff == NULL) { + PyErr_SetString(PyExc_MemoryError, "Out of memory"); } CALL_CPP_CLEANUP("color_conv", - (self->x->color_conv(format, (agg::int8u *)PyBytes_AsString(result))), - Py_DECREF(result)); + (self->x->color_conv(format, buff)), + free(buff)); + + PyObject *result = PyByteArray_FromStringAndSize((const char *)buff, size); + if (result == NULL) { + free(buff); + return NULL; + } return Py_BuildValue("nnN", self->x->rowsOut, self->x->colsOut, result); }