From c333527620bb6a74c70b3d7c0c61c3e2719d76cf Mon Sep 17 00:00:00 2001 From: pavel-shelentsov Date: Mon, 5 Feb 2018 21:17:41 +0300 Subject: [PATCH] Fix for resource leak on exception (#917) * Using try-with-resources to avoid resource leak * Reader closing in finnaly block --- src/com/sun/jna/NativeLibrary.java | 33 ++++++++++++++++++------------ 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/com/sun/jna/NativeLibrary.java b/src/com/sun/jna/NativeLibrary.java index 22b9f84083..a9734e2648 100644 --- a/src/com/sun/jna/NativeLibrary.java +++ b/src/com/sun/jna/NativeLibrary.java @@ -959,22 +959,29 @@ else if (Platform.ARCH.equals("mips64el")) { */ private static ArrayList getLinuxLdPaths() { ArrayList ldPaths = new ArrayList(); + BufferedReader reader = null; try { - Process process = Runtime.getRuntime().exec("/sbin/ldconfig -p"); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - String buffer = ""; - while ((buffer = reader.readLine()) != null) { - int startPath = buffer.indexOf(" => "); - int endPath = buffer.lastIndexOf('/'); - if (startPath != -1 && endPath != -1 && startPath < endPath) { - String path = buffer.substring(startPath+4, endPath); - if (ldPaths.contains(path) == false) { - ldPaths.add(path); - } - } + Process process = Runtime.getRuntime().exec("/sbin/ldconfig -p"); + reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String buffer; + while ((buffer = reader.readLine()) != null) { + int startPath = buffer.indexOf(" => "); + int endPath = buffer.lastIndexOf('/'); + if (startPath != -1 && endPath != -1 && startPath < endPath) { + String path = buffer.substring(startPath + 4, endPath); + if (!ldPaths.contains(path)) { + ldPaths.add(path); + } } - reader.close(); + } } catch (Exception e) { + } finally { + if(reader != null) { + try { + reader.close(); + } catch (IOException e) { + } + } } return ldPaths; }