From 196263128175d166a5c998baea3225f9067415c3 Mon Sep 17 00:00:00 2001 From: Mark Hammond Date: Sat, 25 Mar 2023 14:35:13 +1100 Subject: [PATCH] Fix leak in win32pdh.GetFormattedCounterArray() (#2013) --- win32/src/win32pdhmodule.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/win32/src/win32pdhmodule.cpp b/win32/src/win32pdhmodule.cpp index 5bb417f15..26d5df5c2 100644 --- a/win32/src/win32pdhmodule.cpp +++ b/win32/src/win32pdhmodule.cpp @@ -693,17 +693,23 @@ static PyObject *PyPdhGetFormattedCounterArray(PyObject *self, PyObject *args) DWORD count; PDH_FMT_COUNTERVALUE_ITEM *pItems = NULL; - Py_BEGIN_ALLOW_THREADS pdhStatus = (*pPdhGetFormattedCounterArray)(handle, format, &size, &count, pItems); - Py_END_ALLOW_THREADS if (pdhStatus != PDH_MORE_DATA) return PyWin_SetAPIError("PdhGetFormattedCounterArray", - pdhStatus); + Py_BEGIN_ALLOW_THREADS; + pdhStatus = (*pPdhGetFormattedCounterArray)(handle, format, &size, &count, pItems); + Py_END_ALLOW_THREADS; + if (pdhStatus != PDH_MORE_DATA) { + return PyWin_SetAPIError("PdhGetFormattedCounterArray", + pdhStatus); + } pItems = (PDH_FMT_COUNTERVALUE_ITEM *)malloc(size); if (pItems == NULL) { PyErr_NoMemory(); return NULL; } - Py_BEGIN_ALLOW_THREADS pdhStatus = (*pPdhGetFormattedCounterArray)(handle, format, &size, &count, pItems); - Py_END_ALLOW_THREADS if (pdhStatus != ERROR_SUCCESS) + Py_BEGIN_ALLOW_THREADS; + pdhStatus = (*pPdhGetFormattedCounterArray)(handle, format, &size, &count, pItems); + Py_END_ALLOW_THREADS; + if (pdhStatus != ERROR_SUCCESS) { free(pItems); return PyWin_SetAPIError("PdhGetFormattedCounterArray", pdhStatus); @@ -729,10 +735,13 @@ static PyObject *PyPdhGetFormattedCounterArray(PyObject *self, PyObject *args) else { PyErr_SetString(PyExc_ValueError, "Dont know how to convert the result"); Py_XDECREF(rc); + Py_XDECREF(key); rc = NULL; break; } PyDict_SetItem(rc, key, value); + Py_XDECREF(key); + Py_XDECREF(value); } free(pItems); return rc;