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

Integrate reported quality issues #1570

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/com/sun/jna/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public static Function getFunction(Pointer p, int callFlags, String encoding) {
this.library = library;
this.functionName = functionName;
this.callFlags = callFlags;
this.options = library.options;
this.options = library.getOptions();
Copy link
Contributor

Choose a reason for hiding this comment

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

This map is still mutable; if someone modified the library map (e.g. getOptions.clear() that would echo here.

Now that we are on JDK8 I'd suggest making this version of it an unmodifiable copy. (I'd suggest making the getter return unmodifiable but that's probably a breaking change.)

Suggested change
this.options = library.getOptions();
this.options = Map.copyOf(library.getOptions());

this.encoding = encoding != null ? encoding : Native.getDefaultStringEncoding();
try {
this.peer = library.getSymbolAddress(functionName);
Expand Down
20 changes: 11 additions & 9 deletions src/com/sun/jna/NativeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ public long getSymbolAddress(long handle, String name, SymbolProvider parent) {
}
};

private Cleaner.Cleanable cleanable;
private long handle;
private final Cleaner.Cleanable cleanable;
private volatile long handle;
private final String libraryName;
private final String libraryPath;
private final Map<String, Function> functions = new HashMap<>();
private final SymbolProvider symbolProvider;
final int callFlags;
private String encoding;
final Map<String, ?> options;
private final int callFlags;
private final String encoding;
private final Map<String, ?> options;

private static final Map<String, Reference<NativeLibrary>> libraries = new HashMap<>();

Expand All @@ -120,6 +120,7 @@ private static String functionKey(String name, int flags, String encoding) {
return name + "|" + flags + "|" + encoding;
}

@SuppressWarnings("LeakingThisInConstructor")
private NativeLibrary(String libraryName, String libraryPath, long handle, Map<String, ?> options) {
this.libraryName = getLibraryName(libraryName);
this.libraryPath = libraryPath;
Expand All @@ -129,17 +130,18 @@ private NativeLibrary(String libraryName, String libraryPath, long handle, Map<S
int callingConvention = option instanceof Number ? ((Number)option).intValue() : Function.C_CONVENTION;
this.callFlags = callingConvention;
this.options = options;
this.encoding = (String)options.get(Library.OPTION_STRING_ENCODING);
SymbolProvider optionSymbolProvider = (SymbolProvider)options.get(Library.OPTION_SYMBOL_PROVIDER);
if (optionSymbolProvider == null) {
this.symbolProvider = NATIVE_SYMBOL_PROVIDER;
} else {
this.symbolProvider = optionSymbolProvider;
}

if (this.encoding == null) {
this.encoding = Native.getDefaultStringEncoding();
String encodingValue = (String) options.get(Library.OPTION_STRING_ENCODING);
if (encodingValue == null) {
encodingValue = Native.getDefaultStringEncoding();
}
this.encoding = encodingValue;

// Special workaround for w32 kernel32.GetLastError
// Short-circuit the function to use built-in GetLastError access
Expand Down Expand Up @@ -699,8 +701,8 @@ public void close() {

synchronized(this) {
if (handle != 0) {
cleanable.clean();
handle = 0;
cleanable.clean();
}
}
}
Expand Down