more error checking

This commit is contained in:
zeffy
2017-06-10 11:30:25 -07:00
parent 4b85cb18a6
commit 0b86b8e9ab

View File

@@ -68,8 +68,7 @@ BOOL get_svcgname(SC_HANDLE hSCManager, LPCTSTR lpServiceName, LPTSTR lpGroupNam
if (!_tcsicmp(fname, _T("svchost"))) { if (!_tcsicmp(fname, _T("svchost"))) {
LPWSTR *p = argv; LPWSTR *p = argv;
for (int i = 1; i < numArgs; i++) { for (int i = 1; i < numArgs; i++) {
if (!_tcsicmp(*(p++), _T("-k"))) { if (!_tcsicmp(*(p++), _T("-k")) && !_tcscpy_s(lpGroupName, dwSize, *p)) {
_tcscpy_s(lpGroupName, dwSize, *p);
result = TRUE; result = TRUE;
_tdbgprintf(_T("Got group name of service %s: %s."), lpServiceName, lpGroupName); _tdbgprintf(_T("Got group name of service %s: %s."), lpServiceName, lpGroupName);
break; break;
@@ -84,16 +83,16 @@ BOOL get_svcpath(SC_HANDLE hSCManager, LPCTSTR lpServiceName, LPTSTR lpBinaryPat
if (!hService) { if (!hService) {
return FALSE; return FALSE;
} }
DWORD cbBytesNeeded; DWORD cbBytesNeeded;
QueryServiceConfig(hService, NULL, 0, &cbBytesNeeded); BOOL result = FALSE;
LPQUERY_SERVICE_CONFIG sc = malloc(cbBytesNeeded); if (!QueryServiceConfig(hService, NULL, 0, &cbBytesNeeded) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
BOOL result = QueryServiceConfig(hService, sc, cbBytesNeeded, &cbBytesNeeded); LPQUERY_SERVICE_CONFIG sc = malloc(cbBytesNeeded);
CloseServiceHandle(hService); if (QueryServiceConfig(hService, sc, cbBytesNeeded, &cbBytesNeeded) && !_tcscpy_s(lpBinaryPathName, dwSize, sc->lpBinaryPathName)) {
if (result) { result = TRUE;
_tcscpy_s(lpBinaryPathName, dwSize, sc->lpBinaryPathName); }
free(sc);
} }
free(sc); CloseServiceHandle(hService);
return result; return result;
} }