Skip to content

Commit

Permalink
Fix leak in win32pdh.GetFormattedCounterArray() (#2013)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhammond committed Mar 25, 2023
1 parent 8b4eb22 commit 1962631
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions win32/src/win32pdhmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 1962631

Please sign in to comment.