Skip to content

Commit

Permalink
Support an edge case where XP sets dwLength to 0
Browse files Browse the repository at this point in the history
There's an edge case on Windows XP where an infinite loop is possible.

A call to NtQueryObject with a NULL buffer and a buffer size of 0 will
return STATUS_INFO_LENGTH_MISMATCH but dwLength will be 0.

This change will pre-allocate a buffer of size (MAX_PATH + 1) *
sizeof(WCHAR) and skips the handle if the required dwLenght is 0.
  • Loading branch information
mrjefftang committed Apr 13, 2015
1 parent 42a935b commit 43b118b
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions psutil/arch/windows/process_handles.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ psutil_get_open_files(long dwPid, HANDLE hProcess)
DUPLICATE_SAME_ACCESS))
goto loop_cleanup;

// Guess buffer size is MAX_PATH + 1
g_dwLength = (MAX_PATH+1) * sizeof(WCHAR);

do
{
// Release any previously allocated buffer
Expand All @@ -145,6 +148,10 @@ psutil_get_open_files(long dwPid, HANDLE hProcess)
}

// NtQueryObject puts the required buffer size in g_dwLength
// WinXP edge case puts g_dwLength == 0, just skip this handle
if (g_dwLength == 0)
goto loop_cleanup;

g_dwSize = g_dwLength;
if (g_dwSize > 0)
{
Expand All @@ -154,6 +161,7 @@ psutil_get_open_files(long dwPid, HANDLE hProcess)

if (g_pNameBuffer == NULL)
goto loop_cleanup;

}

dwWait = psutil_NtQueryObject();
Expand Down

0 comments on commit 43b118b

Please sign in to comment.