Possibly fix a mem leak in device scanning on windows

This commit is contained in:
Kovid Goyal 2012-09-09 17:44:29 +05:30
parent e72a0f2482
commit b5109cbdf0

View File

@ -168,9 +168,9 @@ winutil_set_debug(PyObject *self, PyObject *args) {
return Py_None; return Py_None;
} }
static LPTSTR static LPWSTR
get_registry_property(HDEVINFO hDevInfo, DWORD index, DWORD property, BOOL *iterate) { get_registry_property(HDEVINFO hDevInfo, DWORD index, DWORD property, BOOL *iterate) {
/* Get a the property specified by `property` from the registry for the /* Get the property specified by `property` from the registry for the
* device enumerated by `index` in the collection `hDevInfo`. `iterate` * device enumerated by `index` in the collection `hDevInfo`. `iterate`
* will be set to `FALSE` if `index` points outside `hDevInfo`. * will be set to `FALSE` if `index` points outside `hDevInfo`.
* :return: A string allocated on the heap containing the property or * :return: A string allocated on the heap containing the property or
@ -178,7 +178,7 @@ get_registry_property(HDEVINFO hDevInfo, DWORD index, DWORD property, BOOL *iter
*/ */
SP_DEVINFO_DATA DeviceInfoData; SP_DEVINFO_DATA DeviceInfoData;
DWORD DataT; DWORD DataT;
LPTSTR buffer = NULL; LPWSTR buffer = NULL;
DWORD buffersize = 0; DWORD buffersize = 0;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
@ -187,7 +187,7 @@ get_registry_property(HDEVINFO hDevInfo, DWORD index, DWORD property, BOOL *iter
return NULL; return NULL;
} }
while(!SetupDiGetDeviceRegistryProperty( while(!SetupDiGetDeviceRegistryPropertyW(
hDevInfo, hDevInfo,
&DeviceInfoData, &DeviceInfoData,
property, property,
@ -209,7 +209,7 @@ get_registry_property(HDEVINFO hDevInfo, DWORD index, DWORD property, BOOL *iter
} }
static BOOL static BOOL
check_device_id(LPTSTR buffer, unsigned int vid, unsigned int pid) { check_device_id(LPWSTR buffer, unsigned int vid, unsigned int pid) {
WCHAR xVid[9], dVid[9], xPid[9], dPid[9]; WCHAR xVid[9], dVid[9], xPid[9], dPid[9];
unsigned int j; unsigned int j;
_snwprintf_s(xVid, 9, _TRUNCATE, L"vid_%4.4x", vid); _snwprintf_s(xVid, 9, _TRUNCATE, L"vid_%4.4x", vid);
@ -672,7 +672,8 @@ winutil_get_usb_devices(PyObject *self, PyObject *args) {
HDEVINFO hDevInfo; HDEVINFO hDevInfo;
DWORD i; BOOL iterate = TRUE; DWORD i; BOOL iterate = TRUE;
PyObject *devices, *temp = (PyObject *)1; PyObject *devices, *temp = (PyObject *)1;
LPTSTR buffer; LPWSTR buffer;
BOOL ok = 1;
if (!PyArg_ParseTuple(args, "")) return NULL; if (!PyArg_ParseTuple(args, "")) return NULL;
@ -691,16 +692,17 @@ winutil_get_usb_devices(PyObject *self, PyObject *args) {
PyErr_Print(); continue; PyErr_Print(); continue;
} }
buffersize = wcslen(buffer); buffersize = wcslen(buffer);
for (j = 0; j < buffersize; j++) buffer[j] = tolower(buffer[j]); for (j = 0; j < buffersize; j++) buffer[j] = towlower(buffer[j]);
temp = PyUnicode_FromWideChar(buffer, buffersize); temp = PyUnicode_FromWideChar(buffer, buffersize);
PyMem_Free(buffer); PyMem_Free(buffer);
if (temp == NULL) { if (temp == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
ok = 0;
break; break;
} }
PyList_Append(devices, temp); PyList_Append(devices, temp); Py_DECREF(temp); temp = NULL;
} //for } //for
if (temp == NULL) { Py_DECREF(devices); devices = NULL; } if (!ok) { Py_DECREF(devices); devices = NULL; }
SetupDiDestroyDeviceInfoList(hDevInfo); SetupDiDestroyDeviceInfoList(hDevInfo);
return devices; return devices;
} }
@ -711,7 +713,7 @@ winutil_is_usb_device_connected(PyObject *self, PyObject *args) {
unsigned int vid, pid; unsigned int vid, pid;
HDEVINFO hDevInfo; HDEVINFO hDevInfo;
DWORD i; BOOL iterate = TRUE; DWORD i; BOOL iterate = TRUE;
LPTSTR buffer; LPWSTR buffer;
int found = FALSE; int found = FALSE;
PyObject *ans; PyObject *ans;