Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some correctness issues and handle leaks #5

Open
JohnLaTwC opened this issue Nov 20, 2020 · 0 comments
Open

Some correctness issues and handle leaks #5

JohnLaTwC opened this issue Nov 20, 2020 · 0 comments

Comments

@JohnLaTwC
Copy link

Issue 1

GetProcId does not close process snapshot in early return

DWORD GetProcId(const wchar_t* ProcName)
{
	PROCESSENTRY32   pe32;
	HANDLE         hSnapshot = NULL;

	pe32.dwSize = sizeof(PROCESSENTRY32);
	hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	if (Process32First(hSnapshot, &pe32))
	{
		do {
			if (wcscmp(pe32.szExeFile, ProcName) == 0) {
+				CloseHandle(hSnapshot);
				return pe32.th32ProcessID;
				break;
			}

		} while (Process32Next(hSnapshot, &pe32));
	}
	if (hSnapshot != INVALID_HANDLE_VALUE)
		CloseHandle(hSnapshot);

	return NULL;
}

return pe32.th32ProcessID;

Issue 2

Incorrect check on CreateFile API

		HANDLE hFile = CreateFileA("Andrew.dmp", GENERIC_ALL, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); //Create the dmp file

		if (!hFile)
! This check is incorrect. If CreateFileA fails, hFile will be INVALID_HANDLE_VALUE which is -1 not 0.
		{
			printf("Failed to write dump: Invalid dump file\n");
		}

if (!hFile)

Issue 3

AndrewSpecial should call CloseHandle on hProc to avoid a handle leak. There are a couple of code paths that exit the function without closing the handle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant