From f895117bcabda9fed6ef09f0c29201d804665aef Mon Sep 17 00:00:00 2001 From: Thomas Faber Date: Fri, 17 Jan 2014 16:16:04 +0000 Subject: [PATCH] [MSCONFIG] - Fix memory/handle leak in failure case of GetServices. Patch by Christoph von Wittich - Fix service handle failure checks - Remove unnecessary casts svn path=/trunk/; revision=61653 --- reactos/base/applications/msconfig/srvpage.c | 17 +++++++++++------ .../base/applications/msconfig/startuppage.c | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/reactos/base/applications/msconfig/srvpage.c b/reactos/base/applications/msconfig/srvpage.c index eb159b420d9..88b1d74be2c 100644 --- a/reactos/base/applications/msconfig/srvpage.c +++ b/reactos/base/applications/msconfig/srvpage.c @@ -99,7 +99,7 @@ GetServices ( void ) ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL; ScHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE); - if (ScHandle != INVALID_HANDLE_VALUE) + if (ScHandle != NULL) { if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, 0, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0) { @@ -107,7 +107,7 @@ GetServices ( void ) if (GetLastError() == ERROR_MORE_DATA) { /* reserve memory for service info array */ - pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(GetProcessHeap(), 0, BytesNeeded); + pServiceStatus = HeapAlloc(GetProcessHeap(), 0, BytesNeeded); if (!pServiceStatus) return; @@ -145,16 +145,21 @@ GetServices ( void ) BytesNeeded = 0; hService = OpenService(ScHandle, pServiceStatus[Index].lpServiceName, SC_MANAGER_CONNECT); - if (hService != INVALID_HANDLE_VALUE) + if (hService != NULL) { /* check if service is required by the system*/ if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)NULL, 0, &BytesNeeded)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - pServiceFailureActions = (LPSERVICE_FAILURE_ACTIONS) HeapAlloc(GetProcessHeap(), 0, BytesNeeded); + pServiceFailureActions = HeapAlloc(GetProcessHeap(), 0, BytesNeeded); if (pServiceFailureActions == NULL) + { + HeapFree(GetProcessHeap(), 0, pServiceStatus); + CloseServiceHandle(hService); + CloseServiceHandle(ScHandle); return; + } if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)pServiceFailureActions, BytesNeeded, &BytesNeeded)) { @@ -196,7 +201,7 @@ GetServices ( void ) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - pServiceConfig = (LPQUERY_SERVICE_CONFIG) HeapAlloc(GetProcessHeap(), 0, BytesNeeded); + pServiceConfig = HeapAlloc(GetProcessHeap(), 0, BytesNeeded); if (pServiceConfig == NULL) { HeapFree(GetProcessHeap(), 0, pServiceStatus); @@ -238,7 +243,7 @@ GetServices ( void ) dwLen = GetFileVersionInfoSize(FileName, &dwHandle); if (dwLen) { - lpData = (TCHAR*) HeapAlloc(GetProcessHeap(), 0, dwLen); + lpData = HeapAlloc(GetProcessHeap(), 0, dwLen); if (lpData == NULL) { HeapFree(GetProcessHeap(), 0, pServiceStatus); diff --git a/reactos/base/applications/msconfig/startuppage.c b/reactos/base/applications/msconfig/startuppage.c index 557bd86c35e..e14cb9da870 100644 --- a/reactos/base/applications/msconfig/startuppage.c +++ b/reactos/base/applications/msconfig/startuppage.c @@ -93,7 +93,7 @@ GetDisabledAutostartEntriesFromRegistry (TCHAR * szBasePath) { dwValueLength = MAX_KEY_LENGTH; dwDataLength = MAX_VALUE_NAME; - Data = (TCHAR*) HeapAlloc(GetProcessHeap(), 0, MAX_VALUE_NAME * sizeof(TCHAR)); + Data = HeapAlloc(GetProcessHeap(), 0, MAX_VALUE_NAME * sizeof(TCHAR)); if (Data == NULL) break; @@ -160,7 +160,7 @@ GetAutostartEntriesFromRegistry ( HKEY hRootKey, TCHAR* KeyName ) { dwValueLength = MAX_KEY_LENGTH; dwDataLength = MAX_VALUE_NAME; - Data = (TCHAR*) HeapAlloc(GetProcessHeap(), 0, MAX_VALUE_NAME * sizeof(TCHAR)); + Data = HeapAlloc(GetProcessHeap(), 0, MAX_VALUE_NAME * sizeof(TCHAR)); if (Data == NULL) break; retVal = RegEnumValue(hKey, Index, lpValueName, &dwValueLength, NULL, &dwType, (LPBYTE)Data, &dwDataLength);