Skip to content

Commit

Permalink
Fix for resource leak on exception (#917)
Browse files Browse the repository at this point in the history
* Using try-with-resources to avoid resource leak

* Reader closing in finnaly block
  • Loading branch information
pavel-shelentsov authored and matthiasblaesing committed Feb 5, 2018
1 parent 1ad4606 commit c333527
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/com/sun/jna/NativeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -959,22 +959,29 @@ else if (Platform.ARCH.equals("mips64el")) {
*/
private static ArrayList<String> getLinuxLdPaths() {
ArrayList<String> ldPaths = new ArrayList<String>();
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;
}
Expand Down

0 comments on commit c333527

Please sign in to comment.