Skip to content

Ghidra is a software reverse engineering (SRE) framework

changes from review. Fixed issue where getLines(InputStream) was closing the stream, Should be the caller's responsibility

ghidravore6639d19e3bcb
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/totd/TipOfTheDayPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/totd/TipOfTheDayPlugin.java
index 768a11375e..ba8169e98b 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/totd/TipOfTheDayPlugin.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/totd/TipOfTheDayPlugin.java
@@ -17,8 +17,7 @@ package ghidra.app.plugin.core.totd;
import java.io.IOException;
import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
import docking.ActionContext;
import docking.action.DockingAction;
@@ -83,7 +82,7 @@ public class TipOfTheDayPlugin extends Plugin implements FrontEndOnly {
private List<String> loadTips() throws IOException {
try (InputStream in = getClass().getResourceAsStream("tips.txt")) {
- return FileUtilities.getLines(in);
+ return in == null ? Collections.emptyList() : FileUtilities.getLines(in);
}
}
diff --git a/Ghidra/Features/Base/src/test.slow/java/docking/widgets/table/model/DirData.java b/Ghidra/Features/Base/src/test.slow/java/docking/widgets/table/model/DirData.java
index 1e7d3074b9..d6e4da06fb 100644
--- a/Ghidra/Features/Base/src/test.slow/java/docking/widgets/table/model/DirData.java
+++ b/Ghidra/Features/Base/src/test.slow/java/docking/widgets/table/model/DirData.java
@@ -30,13 +30,14 @@ public class DirData {
private Integer size;
public static List<DirData> loadTestData(String file) throws IOException {
- InputStream is = ResourceManager.getResourceAsStream(file);
- List<String> lines = FileUtilities.getLines(is);
- List<DirData> data = new ArrayList<>();
- for (String line : lines) {
- data.add(new DirData(line));
+ try (InputStream is = ResourceManager.getResourceAsStream(file)) {
+ List<String> lines = FileUtilities.getLines(is);
+ List<DirData> data = new ArrayList<>();
+ for (String line : lines) {
+ data.add(new DirData(line));
+ }
+ return data;
}
- return data;
}
public String getDate() {
diff --git a/Ghidra/Framework/Utility/src/main/java/utilities/util/FileUtilities.java b/Ghidra/Framework/Utility/src/main/java/utilities/util/FileUtilities.java
index f66b34ad46..aacae8606c 100644
--- a/Ghidra/Framework/Utility/src/main/java/utilities/util/FileUtilities.java
+++ b/Ghidra/Framework/Utility/src/main/java/utilities/util/FileUtilities.java
@@ -463,19 +463,19 @@ public final class FileUtilities {
return dir.delete();
}
- for (int i = 0; i < files.length; i++) {
+ for (File file : files) {
monitor.checkCanceled();
- if (files[i].isDirectory()) {
+ if (file.isDirectory()) {
// use a dummy monitor as not to ruin our progress
- if (!doDeleteDir(files[i], monitor)) {
- printDebug("Unable to delete directory: " + files[i]);
+ if (!doDeleteDir(file, monitor)) {
+ printDebug("Unable to delete directory: " + file);
return false;
}
}
else {
- monitor.setMessage("Deleting file: " + files[i]);
- if (!files[i].delete()) {
- printDebug("Unable to delete file: " + files[i]);
+ monitor.setMessage("Deleting file: " + file);
+ if (!file.delete()) {
+ printDebug("Unable to delete file: " + file);
return false;
}
}
@@ -681,13 +681,11 @@ public final class FileUtilities {
* <p>
* @param file The text file to read in
* @return a list of file lines
- * @throws IOException
+ * @throws IOException if an error occurs trying to read the file.
*/
public static List<String> getLines(ResourceFile file) throws IOException {
- try {
- BufferedReader in = new BufferedReader(
- new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
- return getLines(in);
+ try (InputStream is = file.getInputStream()) {
+ return getLines(is);
}
catch (FileNotFoundException exc) {
return new ArrayList<>();
@@ -738,7 +736,7 @@ public final class FileUtilities {
* @throws IOException if there are any issues reading the file
*/
public static List<String> getLines(InputStream is) throws IOException {
- return getLines(new BufferedReader(new InputStreamReader(is)));
+ return getLines(new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)));
}
/**
@@ -790,22 +788,12 @@ public final class FileUtilities {
* @throws IOException if there are any issues reading the file
*/
public static List<String> getLines(BufferedReader in) throws IOException {
- try {
- List<String> fileLines = new ArrayList<>();
- String line;
- while ((line = in.readLine()) != null) {
- fileLines.add(line);
- }
- return fileLines;
- }
- finally {
- try {
- in.close();
- }
- catch (IOException e) {
- // don't care; we tried
- }
+ List<String> fileLines = new ArrayList<>();
+ String line;
+ while ((line = in.readLine()) != null) {
+ fileLines.add(line);
}
+ return fileLines;
}
/**