mirror of https://github.com/apache/lucene.git
LUCENE-825: detect null returned from Directory.list() and throw IOException not NPE
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@516120 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
57cf17d188
commit
07a632c0e9
|
@ -84,6 +84,8 @@ final class IndexFileDeleter {
|
||||||
IndexFileNameFilter filter = IndexFileNameFilter.getFilter();
|
IndexFileNameFilter filter = IndexFileNameFilter.getFilter();
|
||||||
|
|
||||||
String[] files = directory.list();
|
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++) {
|
for (int i = 0; i < files.length; i++) {
|
||||||
|
|
||||||
|
|
|
@ -236,6 +236,9 @@ final class SegmentInfo {
|
||||||
// code. So we must fallback to the original
|
// code. So we must fallback to the original
|
||||||
// directory list check:
|
// directory list check:
|
||||||
String[] result = dir.list();
|
String[] result = dir.list();
|
||||||
|
if (result == null)
|
||||||
|
throw new IOException("cannot read directory " + dir + ": list() returned null");
|
||||||
|
|
||||||
String pattern;
|
String pattern;
|
||||||
pattern = name + ".s";
|
pattern = name + ".s";
|
||||||
int patternLength = pattern.length();
|
int patternLength = pattern.length();
|
||||||
|
|
|
@ -113,7 +113,7 @@ final class SegmentInfos extends Vector {
|
||||||
public static long getCurrentSegmentGeneration(Directory directory) throws IOException {
|
public static long getCurrentSegmentGeneration(Directory directory) throws IOException {
|
||||||
String[] files = directory.list();
|
String[] files = directory.list();
|
||||||
if (files == null)
|
if (files == null)
|
||||||
throw new IOException("Cannot read directory " + directory);
|
throw new IOException("cannot read directory " + directory + ": list() returned null");
|
||||||
return getCurrentSegmentGeneration(files);
|
return getCurrentSegmentGeneration(files);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -477,12 +477,12 @@ final class SegmentInfos extends Vector {
|
||||||
if (0 == method) {
|
if (0 == method) {
|
||||||
if (directory != null) {
|
if (directory != null) {
|
||||||
files = directory.list();
|
files = directory.list();
|
||||||
|
if (files == null)
|
||||||
|
throw new FileNotFoundException("cannot read directory " + directory + ": list() returned null");
|
||||||
} else {
|
} else {
|
||||||
files = fileDirectory.list();
|
files = fileDirectory.list();
|
||||||
}
|
if (files == null)
|
||||||
|
throw new FileNotFoundException("cannot read directory " + fileDirectory + ": list() returned null");
|
||||||
if (files == null) {
|
|
||||||
throw new FileNotFoundException("no segments* file found in directory " + directory + ": list() returned null");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gen = getCurrentSegmentGeneration(files);
|
gen = getCurrentSegmentGeneration(files);
|
||||||
|
|
|
@ -42,7 +42,11 @@ public abstract class Directory {
|
||||||
* this Directory instance). */
|
* this Directory instance). */
|
||||||
protected LockFactory lockFactory;
|
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()
|
public abstract String[] list()
|
||||||
throws IOException;
|
throws IOException;
|
||||||
|
|
||||||
|
@ -154,6 +158,10 @@ public abstract class Directory {
|
||||||
*/
|
*/
|
||||||
public static void copy(Directory src, Directory dest, boolean closeDirSrc) throws IOException {
|
public static void copy(Directory src, Directory dest, boolean closeDirSrc) throws IOException {
|
||||||
final String[] files = src.list();
|
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];
|
byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
|
||||||
for (int i = 0; i < files.length; i++) {
|
for (int i = 0; i < files.length; i++) {
|
||||||
IndexOutput os = null;
|
IndexOutput os = null;
|
||||||
|
|
|
@ -239,7 +239,7 @@ public class FSDirectory extends Directory {
|
||||||
if (directory.exists()) {
|
if (directory.exists()) {
|
||||||
String[] files = directory.list(IndexFileNameFilter.getFilter()); // clear old files
|
String[] files = directory.list(IndexFileNameFilter.getFilter()); // clear old files
|
||||||
if (files == null)
|
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++) {
|
for (int i = 0; i < files.length; i++) {
|
||||||
File file = new File(directory, files[i]);
|
File file = new File(directory, files[i]);
|
||||||
if (!file.delete())
|
if (!file.delete())
|
||||||
|
|
|
@ -980,7 +980,7 @@ public class TestIndexReader extends TestCase
|
||||||
Directory dir = FSDirectory.getDirectory(dirFile);
|
Directory dir = FSDirectory.getDirectory(dirFile);
|
||||||
try {
|
try {
|
||||||
IndexReader reader = IndexReader.open(dir);
|
IndexReader reader = IndexReader.open(dir);
|
||||||
fail("expected CorruptIndexException");
|
fail("expected FileNotFoundException");
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
@ -990,7 +990,7 @@ public class TestIndexReader extends TestCase
|
||||||
// Make sure we still get a CorruptIndexException (not NPE):
|
// Make sure we still get a CorruptIndexException (not NPE):
|
||||||
try {
|
try {
|
||||||
IndexReader reader = IndexReader.open(dir);
|
IndexReader reader = IndexReader.open(dir);
|
||||||
fail("expected CorruptIndexException");
|
fail("expected FileNotFoundException");
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue