From 458adcf3dde6839e512214a1feefdbf829ac9e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Mon, 4 Dec 2023 20:56:12 +0100 Subject: [PATCH 1/2] Make NativeLibrary#encoding final, #options and #callFlags private Reported by: @sebbASF Closes: #1560 --- src/com/sun/jna/Function.java | 2 +- src/com/sun/jna/NativeLibrary.java | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/com/sun/jna/Function.java b/src/com/sun/jna/Function.java index a4fd26ff8..c40c0427c 100644 --- a/src/com/sun/jna/Function.java +++ b/src/com/sun/jna/Function.java @@ -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(); this.encoding = encoding != null ? encoding : Native.getDefaultStringEncoding(); try { this.peer = library.getSymbolAddress(functionName); diff --git a/src/com/sun/jna/NativeLibrary.java b/src/com/sun/jna/NativeLibrary.java index ae274fda1..235e46e56 100644 --- a/src/com/sun/jna/NativeLibrary.java +++ b/src/com/sun/jna/NativeLibrary.java @@ -101,9 +101,9 @@ public long getSymbolAddress(long handle, String name, SymbolProvider parent) { private final String libraryPath; private final Map functions = new HashMap<>(); private final SymbolProvider symbolProvider; - final int callFlags; - private String encoding; - final Map options; + private final int callFlags; + private final String encoding; + private final Map options; private static final Map> libraries = new HashMap<>(); @@ -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 options) { this.libraryName = getLibraryName(libraryName); this.libraryPath = libraryPath; @@ -129,7 +130,6 @@ private NativeLibrary(String libraryName, String libraryPath, long handle, Map Date: Mon, 4 Dec 2023 21:48:55 +0100 Subject: [PATCH 2/2] Ensure propagation of handle invalidation of native library on manual unload Reported by: @sebbASF Closes: #1561 --- src/com/sun/jna/NativeLibrary.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/com/sun/jna/NativeLibrary.java b/src/com/sun/jna/NativeLibrary.java index 235e46e56..ea5278b9b 100644 --- a/src/com/sun/jna/NativeLibrary.java +++ b/src/com/sun/jna/NativeLibrary.java @@ -95,8 +95,8 @@ 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 functions = new HashMap<>(); @@ -701,8 +701,8 @@ public void close() { synchronized(this) { if (handle != 0) { - cleanable.clean(); handle = 0; + cleanable.clean(); } } }