From 89bc1eb083918014a5d9971ff546646175253f2f Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Thu, 3 Sep 2015 11:05:21 -0400 Subject: [PATCH] PrologCompiler: Fix NPEs when temp dir can't be listed Coverity-CID: 47579 Coverity-CID: 47580 Coverity-CID: 47581 Change-Id: I3681909b3ac1af8849f615055cf14b63a34ffbc2 --- .../gerrit/pgm/rules/PrologCompiler.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/rules/PrologCompiler.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/rules/PrologCompiler.java index 31c3be10e8..064cc19680 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/rules/PrologCompiler.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/rules/PrologCompiler.java @@ -262,14 +262,16 @@ public class PrologCompiler implements Callable { } } - private List getAllFiles(File dir, String extension) { + private List getAllFiles(File dir, String extension) + throws IOException { ArrayList fileList = new ArrayList<>(); getAllFiles(dir, extension, fileList); return fileList; } - private void getAllFiles(File dir, String extension, List fileList) { - for (File f : dir.listFiles()) { + private void getAllFiles(File dir, String extension, List fileList) + throws IOException { + for (File f : listFiles(dir)) { if (f.getName().endsWith(extension)) { fileList.add(f); } @@ -279,14 +281,16 @@ public class PrologCompiler implements Callable { } } - private List getRelativePaths(File dir, String extension) { + private List getRelativePaths(File dir, String extension) + throws IOException { ArrayList pathList = new ArrayList<>(); getRelativePaths(dir, extension, "", pathList); return pathList; } - private void getRelativePaths(File dir, String extension, String path, List pathList) { - for (File f : dir.listFiles()) { + private static void getRelativePaths(File dir, String extension, String path, + List pathList) throws IOException { + for (File f : listFiles(dir)) { if (f.getName().endsWith(extension)) { pathList.add(path + f.getName()); } @@ -296,8 +300,8 @@ public class PrologCompiler implements Callable { } } - private void deleteAllFiles(File dir) { - for (File f : dir.listFiles()) { + private static void deleteAllFiles(File dir) throws IOException { + for (File f : listFiles(dir)) { if (f.isDirectory()) { deleteAllFiles(f); } else { @@ -306,4 +310,12 @@ public class PrologCompiler implements Callable { } dir.delete(); } + + private static File[] listFiles(File dir) throws IOException { + File[] files = dir.listFiles(); + if (files == null) { + throw new IOException("Failed to list directory: " + dir); + } + return files; + } }