-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[GITHUB-1042] c.s.j.p.WindowUtils.W32WindowUtils.getProcessFilePath does not close process handle #1043
[GITHUB-1042] c.s.j.p.WindowUtils.W32WindowUtils.getProcessFilePath does not close process handle #1043
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1285,17 +1285,30 @@ public String getProcessFilePath(final HWND hwnd) { | |
|
||
final HANDLE process = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION | WinNT.PROCESS_VM_READ, | ||
false, pid.getValue()); | ||
if (process == null | ||
&& Kernel32.INSTANCE.GetLastError() != WinNT.ERROR_ACCESS_DENIED) { | ||
throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); | ||
if (process == null) { | ||
if(Kernel32.INSTANCE.GetLastError() != WinNT.ERROR_ACCESS_DENIED) { | ||
throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); | ||
} else { | ||
// Ignore windows, that can't be accessed | ||
return ""; | ||
} | ||
} | ||
final int length = Psapi.INSTANCE.GetModuleFileNameExW(process, | ||
null, filePath, filePath.length); | ||
if (length == 0 | ||
&& Kernel32.INSTANCE.GetLastError() != WinNT.ERROR_INVALID_HANDLE) { | ||
throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); | ||
|
||
try { | ||
final int length = Psapi.INSTANCE.GetModuleFileNameExW(process, | ||
null, filePath, filePath.length); | ||
if (length == 0) { | ||
if(Kernel32.INSTANCE.GetLastError() != WinNT.ERROR_INVALID_HANDLE) { | ||
throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); | ||
} else { | ||
// ignore invalid handles | ||
return ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. The exception should be thrown on any other error, the previous code was correct IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my previous reply. What is important: calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, thanks. Maybe I should question the original implementation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean, that the function(s) should not return the empty string in case of an error? Yeah, I thought about "fixing" that, but I decided against it, as it basically became API by convention. There are other thinks I would change looking at it - seeing a |
||
} | ||
} | ||
return Native.toString(filePath).trim(); | ||
} finally { | ||
Kernel32.INSTANCE.CloseHandle(process); | ||
} | ||
return Native.toString(filePath).trim(); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to swallow any other unexpected error and return an incorrect result! Also I don't understand the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it does not. Line 1290 is entered if
GetLastError
is notERROR_ACCESS_DENIED
, so any other error is raised as aWin32Exception
. Before and after the change trying to access a process we don't have access to, will return the empty string.