[MSCONFIG]
authorThomas Faber <thomas.faber@reactos.org>
Fri, 17 Jan 2014 16:16:04 +0000 (16:16 +0000)
committerThomas Faber <thomas.faber@reactos.org>
Fri, 17 Jan 2014 16:16:04 +0000 (16:16 +0000)
- 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
reactos/base/applications/msconfig/startuppage.c

index eb159b4..88b1d74 100644 (file)
@@ -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);
index 557bd86..e14cb9d 100644 (file)
@@ -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);