Add missing checks against null.

The program can dereference a null pointer because it does not check the return value of a function that might return
null.
This commit is contained in:
Daniel Gredler
2018-08-30 15:24:41 -04:00
parent 418fda3c8f
commit d4050793b4
2 changed files with 19 additions and 10 deletions
@@ -30,8 +30,11 @@ public class GarbageCollector
} }
File folder = file.getParentFile(); File folder = file.getParentFile();
if (folder.list().length == 0) { if (folder != null) {
FileUtil.deleteHarder(folder); String[] children = folder.list();
if (children != null && children.length == 0) {
FileUtil.deleteHarder(folder);
}
} }
} }
}); });
@@ -187,14 +187,20 @@ public class FileUtil
*/ */
public static void walkTree (File root, Visitor visitor) public static void walkTree (File root, Visitor visitor)
{ {
Deque<File> stack = new ArrayDeque<>(Arrays.asList(root.listFiles())); File[] children = root.listFiles();
while (!stack.isEmpty()) { if (children != null) {
File currentFile = stack.pop(); Deque<File> stack = new ArrayDeque<>(Arrays.asList(children));
if (currentFile.exists()) { while (!stack.isEmpty()) {
visitor.visit(currentFile); File currentFile = stack.pop();
if (currentFile.isDirectory()) { if (currentFile.exists()) {
for (File file: currentFile.listFiles()) { visitor.visit(currentFile);
stack.push(file); if (currentFile.isDirectory()) {
File[] currentChildren = currentFile.listFiles();
if (currentChildren != null) {
for (File file : currentChildren) {
stack.push(file);
}
}
} }
} }
} }