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

[GITHUB-1042] c.s.j.p.WindowUtils.W32WindowUtils.getProcessFilePath does not close process handle #1043

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Bug Fixes
* [#1036](https://github.com/java-native-access/jna/issues/1036): `Advapi32Util.registryValueExists` called on non existing key raises exception instead of returning `false` - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#384](https://github.com/java-native-access/jna/issues/384): Android only supports loading libraries through the JVM `System#loadLibrary` mechanism, defaulting `jna.nosys` to `true` disabled that code path - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1041](https://github.com/java-native-access/jna/pull/1041): Avoid IllegalArgumentException when reading xattrs with zero length - [@jrobhoward](https://github.com/jrobhoward).
* [#1042](https://github.com/java-native-access/jna/issues/1042): `com.sun.jna.platform.WindowUtils.W32WindowUtils.getProcessFilePath(HWND)` does not close process handle - [@matthiasblaesing](https://github.com/matthiasblaesing).

Release 5.1.0
=============
Expand Down
31 changes: 22 additions & 9 deletions contrib/platform/src/com/sun/jna/platform/WindowUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

@dblock dblock Dec 21, 2018

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.

Copy link
Member Author

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 not ERROR_ACCESS_DENIED, so any other error is raised as a Win32Exception. Before and after the change trying to access a process we don't have access to, will return the empty string.

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 "";
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my previous reply. What is important: calling Native.toString(filePath).trim() on an initialized char[] (all \0) will result in the empty string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks. Maybe I should question the original implementation?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 HWND in a common class is suspicious.

}
}
return Native.toString(filePath).trim();
} finally {
Kernel32.INSTANCE.CloseHandle(process);
}
return Native.toString(filePath).trim();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@

import com.sun.jna.platform.WindowUtils;
import com.sun.jna.platform.DesktopWindow;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.DWORDByReference;
import com.sun.jna.platform.win32.WinDef.HICON;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser;

public class WindowUtilsTest extends TestCase {

Expand Down