Skip to content

Apache Lucene open-source search software

LUCENE-5160: check for -1 return conditions in file reads

Grant Ingersoll8177498c8680
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 903e57e40dd..489f79fa77c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -46,6 +46,12 @@ New Features
* SOLR-3359: Added analyzer attribute/property to SynonymFilterFactory.
(Ryo Onodera via Koji Sekiguchi)
+Bugs
+
+* LUCENE-5160: Handle the case where reading from a file or FileChannel returns -1, which
+ could happen in rare cases where something happens to the file between the time we start the
+ read loop (where we check the length) and when we actually do the read. (gsingers, yonik, Robert Muir, Uwe Schindler)
+
Optimizations
* LUCENE-4848: Use Java 7 NIO2-FileChannel instead of RandomAccessFile
diff --git a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
index ad5b1297fb6..b6741189837 100644
--- a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
@@ -200,6 +200,9 @@ public class NIOFSDirectory extends FSDirectory {
}
bb.limit(limit);
int i = channel.read(bb, pos);
+ if (i < 0){//be defensive here, even though we checked before hand, something could have changed
+ throw new EOFException("read past EOF: " + this + " off: " + offset + " len: " + len + " pos: " + pos + " limit: " + limit + " end: " + end);
+ }
pos += i;
readOffset += i;
readLength -= i;
diff --git a/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java
index 786e0774fd2..ecb446efc4d 100644
--- a/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java
@@ -155,6 +155,9 @@ public class SimpleFSDirectory extends FSDirectory {
readLength = chunkSize;
}
final int i = file.read(b, offset + total, readLength);
+ if (i < 0){//be defensive here, even though we checked before hand, something could have changed
+ throw new EOFException("read past EOF: " + this + " off: " + offset + " len: " + len + " total: " + total + " readLen: " + readLength + " end: " + end);
+ }
total += i;
} while (total < len);
} catch (OutOfMemoryError e) {