Skip to content

Commit c333527

Browse files
pavel-shelentsovmatthiasblaesing
authored andcommitted
Fix for resource leak on exception (#917)
* Using try-with-resources to avoid resource leak * Reader closing in finnaly block
1 parent 1ad4606 commit c333527

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/com/sun/jna/NativeLibrary.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -959,22 +959,29 @@ else if (Platform.ARCH.equals("mips64el")) {
959959
*/
960960
private static ArrayList<String> getLinuxLdPaths() {
961961
ArrayList<String> ldPaths = new ArrayList<String>();
962+
BufferedReader reader = null;
962963
try {
963-
Process process = Runtime.getRuntime().exec("/sbin/ldconfig -p");
964-
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
965-
String buffer = "";
966-
while ((buffer = reader.readLine()) != null) {
967-
int startPath = buffer.indexOf(" => ");
968-
int endPath = buffer.lastIndexOf('/');
969-
if (startPath != -1 && endPath != -1 && startPath < endPath) {
970-
String path = buffer.substring(startPath+4, endPath);
971-
if (ldPaths.contains(path) == false) {
972-
ldPaths.add(path);
973-
}
974-
}
964+
Process process = Runtime.getRuntime().exec("/sbin/ldconfig -p");
965+
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
966+
String buffer;
967+
while ((buffer = reader.readLine()) != null) {
968+
int startPath = buffer.indexOf(" => ");
969+
int endPath = buffer.lastIndexOf('/');
970+
if (startPath != -1 && endPath != -1 && startPath < endPath) {
971+
String path = buffer.substring(startPath + 4, endPath);
972+
if (!ldPaths.contains(path)) {
973+
ldPaths.add(path);
974+
}
975975
}
976-
reader.close();
976+
}
977977
} catch (Exception e) {
978+
} finally {
979+
if(reader != null) {
980+
try {
981+
reader.close();
982+
} catch (IOException e) {
983+
}
984+
}
978985
}
979986
return ldPaths;
980987
}

0 commit comments

Comments
 (0)