Improve handle enumeration error checking
* handle provider
*
* Copyright (C) 2010-2015 wj32
- * Copyright (C) 2017 dmex
+ * Copyright (C) 2017-2021 dmex
*
* This file is part of Process Hacker.
*
@@ -301,7 +301,7 @@ NTSTATUS PhEnumHandlesGeneric(
_Out_ PBOOLEAN FilterNeeded
)
{
- NTSTATUS status;
+ NTSTATUS status = STATUS_UNSUCCESSFUL;
// There are three ways of enumerating handles:
// * On Windows 8 and later, NtQueryInformationProcess with ProcessHandleInformation is the most efficient method.
@@ -317,9 +317,8 @@ NTSTATUS PhEnumHandlesGeneric(
// Enumerate handles using KProcessHacker. Unlike with NtQuerySystemInformation,
// this only enumerates handles for a single process and saves a lot of processing.
- if (!NT_SUCCESS(status = KphEnumerateProcessHandles2(ProcessHandle, &handles)))
- goto FAILED;
-
+ if (NT_SUCCESS(status = KphEnumerateProcessHandles2(ProcessHandle, &handles)))
+ {
convertedHandles = PhAllocate(
FIELD_OFFSET(SYSTEM_HANDLE_INFORMATION_EX, Handles) +
sizeof(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX) * handles->HandleCount
@@ -343,15 +342,16 @@ NTSTATUS PhEnumHandlesGeneric(
*Handles = convertedHandles;
*FilterNeeded = FALSE;
}
- else if (WindowsVersion >= WINDOWS_8 && PhGetIntegerSetting(L"EnableHandleSnapshot"))
+ }
+
+ if (!NT_SUCCESS(status) && WindowsVersion >= WINDOWS_8 && PhGetIntegerSetting(L"EnableHandleSnapshot"))
{
PPROCESS_HANDLE_SNAPSHOT_INFORMATION handles;
PSYSTEM_HANDLE_INFORMATION_EX convertedHandles;
ULONG i;
- if (!NT_SUCCESS(status = PhEnumHandlesEx2(ProcessHandle, &handles)))
- goto FAILED;
-
+ if (NT_SUCCESS(status = PhEnumHandlesEx2(ProcessHandle, &handles)))
+ {
convertedHandles = PhAllocate(
FIELD_OFFSET(SYSTEM_HANDLE_INFORMATION_EX, Handles) +
sizeof(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX) * handles->NumberOfHandles
@@ -375,10 +375,12 @@ NTSTATUS PhEnumHandlesGeneric(
*Handles = convertedHandles;
*FilterNeeded = FALSE;
}
- else
+ }
+
+ if (!NT_SUCCESS(status))
{
PSYSTEM_HANDLE_INFORMATION_EX handles;
-FAILED:
+
if (!NT_SUCCESS(status = PhEnumHandlesEx(&handles)))
return status;
