Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-825: detect null returned from Directory.list() and throw IOException not NPE

Michael McCandless07a632c0e995
diff --git a/src/java/org/apache/lucene/index/IndexFileDeleter.java b/src/java/org/apache/lucene/index/IndexFileDeleter.java
index 00b04985c15..7cb2fe42377 100644
--- a/src/java/org/apache/lucene/index/IndexFileDeleter.java
+++ b/src/java/org/apache/lucene/index/IndexFileDeleter.java
@@ -84,6 +84,8 @@ final class IndexFileDeleter {
IndexFileNameFilter filter = IndexFileNameFilter.getFilter();
String[] files = directory.list();
+ if (files == null)
+ throw new IOException("cannot read directory " + directory + ": list() returned null");
for (int i = 0; i < files.length; i++) {
diff --git a/src/java/org/apache/lucene/index/SegmentInfo.java b/src/java/org/apache/lucene/index/SegmentInfo.java
index c521999e7ea..22f9d077686 100644
--- a/src/java/org/apache/lucene/index/SegmentInfo.java
+++ b/src/java/org/apache/lucene/index/SegmentInfo.java
@@ -236,6 +236,9 @@ final class SegmentInfo {
// code. So we must fallback to the original
// directory list check:
String[] result = dir.list();
+ if (result == null)
+ throw new IOException("cannot read directory " + dir + ": list() returned null");
+
String pattern;
pattern = name + ".s";
int patternLength = pattern.length();
diff --git a/src/java/org/apache/lucene/index/SegmentInfos.java b/src/java/org/apache/lucene/index/SegmentInfos.java
index a4ffe899516..41a8799c3b7 100644
--- a/src/java/org/apache/lucene/index/SegmentInfos.java
+++ b/src/java/org/apache/lucene/index/SegmentInfos.java
@@ -113,7 +113,7 @@ final class SegmentInfos extends Vector {
public static long getCurrentSegmentGeneration(Directory directory) throws IOException {
String[] files = directory.list();
if (files == null)
- throw new IOException("Cannot read directory " + directory);
+ throw new IOException("cannot read directory " + directory + ": list() returned null");
return getCurrentSegmentGeneration(files);
}
@@ -477,12 +477,12 @@ final class SegmentInfos extends Vector {
if (0 == method) {
if (directory != null) {
files = directory.list();
+ if (files == null)
+ throw new FileNotFoundException("cannot read directory " + directory + ": list() returned null");
} else {
files = fileDirectory.list();
- }
-
- if (files == null) {
- throw new FileNotFoundException("no segments* file found in directory " + directory + ": list() returned null");
+ if (files == null)
+ throw new FileNotFoundException("cannot read directory " + fileDirectory + ": list() returned null");
}
gen = getCurrentSegmentGeneration(files);
diff --git a/src/java/org/apache/lucene/store/Directory.java b/src/java/org/apache/lucene/store/Directory.java
index 736d6bf5797..3cc441387dc 100644
--- a/src/java/org/apache/lucene/store/Directory.java
+++ b/src/java/org/apache/lucene/store/Directory.java
@@ -42,7 +42,11 @@ public abstract class Directory {
* this Directory instance). */
protected LockFactory lockFactory;
- /** Returns an array of strings, one for each file in the directory. */
+ /** Returns an array of strings, one for each file in the
+ * directory. This method may return null (for example for
+ * {@link FSDirectory} if the underlying directory doesn't
+ * exist in the filesystem or there are permissions
+ * problems).*/
public abstract String[] list()
throws IOException;
@@ -154,6 +158,10 @@ public abstract class Directory {
*/
public static void copy(Directory src, Directory dest, boolean closeDirSrc) throws IOException {
final String[] files = src.list();
+
+ if (files == null)
+ throw new IOException("cannot read directory " + src + ": list() returned null");
+
byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
for (int i = 0; i < files.length; i++) {
IndexOutput os = null;
diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.java
index 3bd6393c65e..881450de437 100644
--- a/src/java/org/apache/lucene/store/FSDirectory.java
+++ b/src/java/org/apache/lucene/store/FSDirectory.java
@@ -239,7 +239,7 @@ public class FSDirectory extends Directory {
if (directory.exists()) {
String[] files = directory.list(IndexFileNameFilter.getFilter()); // clear old files
if (files == null)
- throw new IOException("Cannot read directory " + directory.getAbsolutePath());
+ throw new IOException("cannot read directory " + directory.getAbsolutePath() + ": list() returned null");
for (int i = 0; i < files.length; i++) {
File file = new File(directory, files[i]);
if (!file.delete())
diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.java
index e2796810903..118e89dc584 100644
--- a/src/test/org/apache/lucene/index/TestIndexReader.java
+++ b/src/test/org/apache/lucene/index/TestIndexReader.java
@@ -980,7 +980,7 @@ public class TestIndexReader extends TestCase
Directory dir = FSDirectory.getDirectory(dirFile);
try {
IndexReader reader = IndexReader.open(dir);
- fail("expected CorruptIndexException");
+ fail("expected FileNotFoundException");
} catch (FileNotFoundException e) {
// expected
}
@@ -990,7 +990,7 @@ public class TestIndexReader extends TestCase
// Make sure we still get a CorruptIndexException (not NPE):
try {
IndexReader reader = IndexReader.open(dir);
- fail("expected CorruptIndexException");
+ fail("expected FileNotFoundException");
} catch (FileNotFoundException e) {
// expected
}