From 4930b66aa90c4e835bcc8b2875fca5471884dc7b Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Mon, 23 Feb 2015 06:00:41 +0100 Subject: [PATCH] webp: fix memory leak The "S" format specifier for Py_BuildValue *increases* the object reference count. --- _webp.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/_webp.c b/_webp.c index 96826dde5..70532726c 100644 --- a/_webp.c +++ b/_webp.c @@ -138,7 +138,7 @@ PyObject* WebPDecode_wrapper(PyObject* self, PyObject* args) PyBytesObject *webp_string; const uint8_t *webp; Py_ssize_t size; - PyObject *ret, *bytes, *pymode, *icc_profile = Py_None, *exif = Py_None; + PyObject *ret = Py_None, *bytes = NULL, *pymode = NULL, *icc_profile = NULL, *exif = NULL; WebPDecoderConfig config; VP8StatusCode vp8_status_code = VP8_STATUS_OK; char* mode = "RGB"; @@ -194,10 +194,8 @@ PyObject* WebPDecode_wrapper(PyObject* self, PyObject* args) #endif } - if (vp8_status_code != VP8_STATUS_OK) { - WebPFreeDecBuffer(&config.output); - Py_RETURN_NONE; - } + if (vp8_status_code != VP8_STATUS_OK) + goto end; if (config.output.colorspace < MODE_YUV) { bytes = PyBytes_FromStringAndSize((char *)config.output.u.RGBA.rgba, @@ -215,8 +213,21 @@ PyObject* WebPDecode_wrapper(PyObject* self, PyObject* args) pymode = PyString_FromString(mode); #endif ret = Py_BuildValue("SiiSSS", bytes, config.output.width, - config.output.height, pymode, icc_profile, exif); + config.output.height, pymode, + NULL == icc_profile ? Py_None : icc_profile, + NULL == exif ? Py_None : exif); + +end: WebPFreeDecBuffer(&config.output); + + Py_XDECREF(bytes); + Py_XDECREF(pymode); + Py_XDECREF(icc_profile); + Py_XDECREF(exif); + + if (Py_None == ret) + Py_RETURN_NONE; + return ret; }