[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
This commit is contained in:
Thomas Faber 2014-01-17 16:16:04 +00:00
parent 20fe3bef9e
commit f895117bca
2 changed files with 13 additions and 8 deletions

View file

@ -99,7 +99,7 @@ GetServices ( void )
ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL; ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL;
ScHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE); 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) 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) if (GetLastError() == ERROR_MORE_DATA)
{ {
/* reserve memory for service info array */ /* reserve memory for service info array */
pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(GetProcessHeap(), 0, BytesNeeded); pServiceStatus = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
if (!pServiceStatus) if (!pServiceStatus)
return; return;
@ -145,16 +145,21 @@ GetServices ( void )
BytesNeeded = 0; BytesNeeded = 0;
hService = OpenService(ScHandle, pServiceStatus[Index].lpServiceName, SC_MANAGER_CONNECT); 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*/ /* check if service is required by the system*/
if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)NULL, 0, &BytesNeeded)) if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)NULL, 0, &BytesNeeded))
{ {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{ {
pServiceFailureActions = (LPSERVICE_FAILURE_ACTIONS) HeapAlloc(GetProcessHeap(), 0, BytesNeeded); pServiceFailureActions = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
if (pServiceFailureActions == NULL) if (pServiceFailureActions == NULL)
{
HeapFree(GetProcessHeap(), 0, pServiceStatus);
CloseServiceHandle(hService);
CloseServiceHandle(ScHandle);
return; return;
}
if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)pServiceFailureActions, BytesNeeded, &BytesNeeded)) if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)pServiceFailureActions, BytesNeeded, &BytesNeeded))
{ {
@ -196,7 +201,7 @@ GetServices ( void )
{ {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{ {
pServiceConfig = (LPQUERY_SERVICE_CONFIG) HeapAlloc(GetProcessHeap(), 0, BytesNeeded); pServiceConfig = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
if (pServiceConfig == NULL) if (pServiceConfig == NULL)
{ {
HeapFree(GetProcessHeap(), 0, pServiceStatus); HeapFree(GetProcessHeap(), 0, pServiceStatus);
@ -238,7 +243,7 @@ GetServices ( void )
dwLen = GetFileVersionInfoSize(FileName, &dwHandle); dwLen = GetFileVersionInfoSize(FileName, &dwHandle);
if (dwLen) if (dwLen)
{ {
lpData = (TCHAR*) HeapAlloc(GetProcessHeap(), 0, dwLen); lpData = HeapAlloc(GetProcessHeap(), 0, dwLen);
if (lpData == NULL) if (lpData == NULL)
{ {
HeapFree(GetProcessHeap(), 0, pServiceStatus); HeapFree(GetProcessHeap(), 0, pServiceStatus);

View file

@ -93,7 +93,7 @@ GetDisabledAutostartEntriesFromRegistry (TCHAR * szBasePath)
{ {
dwValueLength = MAX_KEY_LENGTH; dwValueLength = MAX_KEY_LENGTH;
dwDataLength = MAX_VALUE_NAME; 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) if (Data == NULL)
break; break;
@ -160,7 +160,7 @@ GetAutostartEntriesFromRegistry ( HKEY hRootKey, TCHAR* KeyName )
{ {
dwValueLength = MAX_KEY_LENGTH; dwValueLength = MAX_KEY_LENGTH;
dwDataLength = MAX_VALUE_NAME; 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) if (Data == NULL)
break; break;
retVal = RegEnumValue(hKey, Index, lpValueName, &dwValueLength, NULL, &dwType, (LPBYTE)Data, &dwDataLength); retVal = RegEnumValue(hKey, Index, lpValueName, &dwValueLength, NULL, &dwType, (LPBYTE)Data, &dwDataLength);