Author: hpoussin
Date: Sun May 7 17:35:44 2006
New Revision: 21829
URL:
http://svn.reactos.ru/svn/reactos?rev=21829&view=rev
Log:
Replace some casts by CONTAINING_RECORD macro
Fix handle leak in SetupDiOpenClassRegKeyExW
Modified:
trunk/reactos/dll/win32/setupapi/devinst.c
Modified: trunk/reactos/dll/win32/setupapi/devinst.c
URL:
http://svn.reactos.ru/svn/reactos/trunk/reactos/dll/win32/setupapi/devinst.…
==============================================================================
--- trunk/reactos/dll/win32/setupapi/devinst.c (original)
+++ trunk/reactos/dll/win32/setupapi/devinst.c Sun May 7 17:35:44 2006
@@ -826,7 +826,7 @@
SetLastError(ERROR_NO_MORE_ITEMS);
else
{
- struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement
*)ItemList;
+ struct DeviceInfoElement *DevInfo = CONTAINING_RECORD(ItemList, struct
DeviceInfoElement, ListEntry);
memcpy(&DeviceInfoData->ClassGuid,
&DevInfo->ClassGuid,
sizeof(GUID));
@@ -1661,6 +1661,7 @@
hInterfaceKey = INVALID_HANDLE_VALUE;
hDeviceInstanceKey = INVALID_HANDLE_VALUE;
+ hReferenceKey = INVALID_HANDLE_VALUE;
/* Open registry key related to this interface */
hInterfaceKey = SetupDiOpenClassRegKeyExW(InterfaceGuid, KEY_ENUMERATE_SUB_KEYS,
DIOCR_INTERFACE, MachineName, NULL);
@@ -2290,7 +2291,7 @@
while (ItemList != &list->ListHead && !Found)
{
PLIST_ENTRY InterfaceListEntry;
- struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement
*)ItemList;
+ struct DeviceInfoElement *DevInfo = CONTAINING_RECORD(ItemList, struct
DeviceInfoElement, ListEntry);
if (DeviceInfoData && (struct DeviceInfoElement
*)DeviceInfoData->Reserved != DevInfo)
{
/* We are not searching for this element */
@@ -2300,7 +2301,7 @@
InterfaceListEntry = DevInfo->InterfaceListHead.Flink;
while (InterfaceListEntry != &DevInfo->InterfaceListHead
&& !Found)
{
- struct DeviceInterface *DevItf = (struct DeviceInterface
*)InterfaceListEntry;
+ struct DeviceInterface *DevItf =
CONTAINING_RECORD(InterfaceListEntry, struct DeviceInterface, ListEntry);
if (!IsEqualIID(&DevItf->InterfaceClassGuid,
InterfaceClassGuid))
{
InterfaceListEntry = InterfaceListEntry->Flink;
@@ -2394,7 +2395,7 @@
while (!IsListEmpty(&list->ListHead))
{
ListEntry = RemoveHeadList(&list->ListHead);
- deviceInfo = (struct DeviceInfoElement *)ListEntry;
+ deviceInfo = CONTAINING_RECORD(ListEntry, struct DeviceInfoElement, ListEntry);
if (!DestroyDeviceInfoElement(deviceInfo))
return FALSE;
}
@@ -3330,12 +3331,13 @@
PCWSTR MachineName OPTIONAL,
PVOID Reserved)
{
- LPWSTR lpGuidString;
- LPWSTR lpFullGuidString;
+ LPWSTR lpGuidString = NULL;
+ LPWSTR lpFullGuidString = NULL;
DWORD dwLength;
HKEY HKLM;
- HKEY hClassesKey;
- HKEY hClassKey;
+ HKEY hClassesKey = NULL;
+ HKEY hClassKey = NULL;
+ HKEY ret = INVALID_HANDLE_VALUE;
DWORD rc;
LPCWSTR lpKeyName;
@@ -3343,18 +3345,14 @@
Flags, debugstr_w(MachineName), Reserved);
if (Flags == DIOCR_INSTALLER)
- {
lpKeyName = REGSTR_PATH_CLASS_NT;
- }
else if (Flags == DIOCR_INTERFACE)
- {
lpKeyName = REGSTR_PATH_DEVICE_CLASSES;
- }
else
{
ERR("Invalid Flags parameter!\n");
SetLastError(ERROR_INVALID_FLAGS);
- return INVALID_HANDLE_VALUE;
+ goto cleanup;
}
if (MachineName != NULL)
@@ -3363,32 +3361,35 @@
if (rc != ERROR_SUCCESS)
{
SetLastError(rc);
- return INVALID_HANDLE_VALUE;
+ goto cleanup;
}
}
else
HKLM = HKEY_LOCAL_MACHINE;
rc = RegOpenKeyExW(HKLM,
- lpKeyName,
- 0,
- ClassGuid ? 0 : samDesired,
- &hClassesKey);
+ lpKeyName,
+ 0,
+ ClassGuid ? 0 : samDesired,
+ &hClassesKey);
if (MachineName != NULL) RegCloseKey(HKLM);
if (rc != ERROR_SUCCESS)
{
- SetLastError(rc);
- return INVALID_HANDLE_VALUE;
+ SetLastError(rc);
+ goto cleanup;
}
if (ClassGuid == NULL)
- return hClassesKey;
+ {
+ /* Stop here. We don't need to open a subkey */
+ ret = hClassesKey;
+ goto cleanup;
+ }
if (UuidToStringW((UUID*)ClassGuid, &lpGuidString) != RPC_S_OK)
{
- SetLastError(ERROR_GEN_FAILURE);
- RegCloseKey(hClassesKey);
- return INVALID_HANDLE_VALUE;
+ SetLastError(ERROR_GEN_FAILURE);
+ goto cleanup;
}
dwLength = lstrlenW(lpGuidString);
@@ -3396,32 +3397,36 @@
if (!lpFullGuidString)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
- RpcStringFreeW(&lpGuidString);
- return INVALID_HANDLE_VALUE;
+ goto cleanup;
}
lpFullGuidString[0] = '{';
memcpy(&lpFullGuidString[1], lpGuidString, dwLength * sizeof(WCHAR));
lpFullGuidString[dwLength + 1] = '}';
lpFullGuidString[dwLength + 2] = '\0';
- RpcStringFreeW(&lpGuidString);
rc = RegOpenKeyExW(hClassesKey,
- lpFullGuidString,
- 0,
- samDesired,
- &hClassKey);
+ lpFullGuidString,
+ 0,
+ samDesired,
+ &hClassKey);
if (rc != ERROR_SUCCESS)
{
- SetLastError(rc);
- HeapFree(GetProcessHeap(), 0, lpFullGuidString);
- RegCloseKey(hClassesKey);
- return INVALID_HANDLE_VALUE;
- }
-
- HeapFree(GetProcessHeap(), 0, lpFullGuidString);
- RegCloseKey(hClassesKey);
-
- return hClassKey;
+ SetLastError(rc);
+ goto cleanup;
+ }
+ ret = hClassKey;
+
+cleanup:
+ if (hClassKey != NULL && hClassKey != ret)
+ RegCloseKey(hClassesKey);
+ if (hClassesKey != NULL && hClassesKey != ret)
+ RegCloseKey(hClassesKey);
+ if (lpGuidString)
+ RpcStringFreeW(&lpGuidString);
+ if (lpFullGuidString)
+ HeapFree(GetProcessHeap(), 0, lpFullGuidString);
+
+ return ret;
}
/***********************************************************************
@@ -3942,7 +3947,8 @@
ListEntry = ClassCoInstallersListHead.Flink;
while (rc == NO_ERROR && ListEntry !=
&ClassCoInstallersListHead)
{
- struct CoInstallerElement *coinstaller = (struct CoInstallerElement
*)ListEntry;
+ struct CoInstallerElement *coinstaller;
+ coinstaller = CONTAINING_RECORD(ListEntry, struct CoInstallerElement,
ListEntry);
rc = (*coinstaller->Function)(InstallFunction, DeviceInfoSet,
DeviceInfoData, &Context);
coinstaller->PrivateData = Context.PrivateData;
if (rc == ERROR_DI_POSTPROCESSING_REQUIRED)
@@ -3957,7 +3963,8 @@
ListEntry = DeviceCoInstallersListHead.Flink;
while (rc == NO_ERROR && ListEntry !=
&DeviceCoInstallersListHead)
{
- struct CoInstallerElement *coinstaller = (struct CoInstallerElement
*)ListEntry;
+ struct CoInstallerElement *coinstaller;
+ coinstaller = CONTAINING_RECORD(ListEntry, struct CoInstallerElement,
ListEntry);
rc = (*coinstaller->Function)(InstallFunction, DeviceInfoSet,
DeviceInfoData, &Context);
coinstaller->PrivateData = Context.PrivateData;
if (rc == ERROR_DI_POSTPROCESSING_REQUIRED)
@@ -3996,7 +4003,8 @@
ListEntry = ClassCoInstallersListHead.Flink;
while (ListEntry != &ClassCoInstallersListHead)
{
- struct CoInstallerElement *coinstaller = (struct CoInstallerElement
*)ListEntry;
+ struct CoInstallerElement *coinstaller;
+ coinstaller = CONTAINING_RECORD(ListEntry, struct CoInstallerElement,
ListEntry);
if (coinstaller->DoPostProcessing)
{
Context.InstallResult = rc;
@@ -4011,7 +4019,8 @@
ListEntry = DeviceCoInstallersListHead.Flink;
while (ListEntry != &DeviceCoInstallersListHead)
{
- struct CoInstallerElement *coinstaller = (struct CoInstallerElement
*)ListEntry;
+ struct CoInstallerElement *coinstaller;
+ coinstaller = CONTAINING_RECORD(ListEntry, struct CoInstallerElement,
ListEntry);
if (coinstaller->DoPostProcessing)
{
Context.InstallResult = rc;
@@ -6023,7 +6032,7 @@
while (!IsListEmpty(&list->DriverListHead))
{
ListEntry = RemoveHeadList(&list->DriverListHead);
- driverInfo = (struct DriverInfoElement *)ListEntry;
+ driverInfo = CONTAINING_RECORD(ListEntry, struct DriverInfoElement,
ListEntry);
DestroyDriverInfoElement(driverInfo);
}
InstallParams.Reserved = 0;
@@ -6043,7 +6052,7 @@
while (!IsListEmpty(&deviceInfo->DriverListHead))
{
ListEntry = RemoveHeadList(&deviceInfo->DriverListHead);
- driverInfo = (struct DriverInfoElement *)ListEntry;
+ driverInfo = CONTAINING_RECORD(ListEntry, struct DriverInfoElement,
ListEntry);
if ((PVOID)InstallParamsSet.Reserved == driverInfo)
{
InstallParamsSet.Reserved = 0;
@@ -6321,7 +6330,7 @@
SetLastError(ERROR_NO_MORE_ITEMS);
else
{
- struct DriverInfoElement *DrvInfo = (struct DriverInfoElement *)ItemList;
+ struct DriverInfoElement *DrvInfo = CONTAINING_RECORD(ItemList, struct
DriverInfoElement, ListEntry);
memcpy(
&DriverInfoData->DriverType,
@@ -6653,11 +6662,11 @@
SetLastError(ERROR_INVALID_PARAMETER);
else
{
- *pDriverInfo = (struct DriverInfoElement *)ItemList;
+ *pDriverInfo = CONTAINING_RECORD(ItemList, struct DriverInfoElement,
ListEntry);
DriverInfoData->Reserved = (ULONG_PTR)ItemList;
ret = TRUE;
TRACE("Choosing driver whose rank is 0x%lx\n",
- ((struct DriverInfoElement *)ItemList)->DriverRank);
+ (*pDriverInfo)->DriverRank);
if (DeviceInfoData)
memcpy(&DeviceInfoData->ClassGuid,
&(*pDriverInfo)->ClassGuid, sizeof(GUID));
}