Apache Solr open-source search software
LUCENE-6932: RAMDirectory's IndexInput should always throw EOFE if you seek beyond the end of the file and then try to read
Michael McCandless6961bb1fc433
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 4f516fe591c..4c206621b79 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -185,6 +185,10 @@ Bug Fixes* LUCENE-6976: BytesRefTermAttributeImpl.copyTo NPE'ed if BytesRef was null.Added equals & hashCode, and a new test for these things. (David Smiley)+* LUCENE-6932: RAMDirectory's IndexInput should always throw+ EOFException if you seek past the end of the file and then try to+ read (Stéphane Campinas via Mike McCandless)+Other* LUCENE-6924: Upgrade randomizedtesting to 2.3.2. (Dawid Weiss)diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.javaindex 6cd2e16339f..46d5dc18736 100644--- a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java+++ b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java@@ -97,7 +97,7 @@ public class RAMInputStream extends IndexInput implements Cloneable {if (enforceEOF) {throw new EOFException("read past EOF: " + this);} else {- // Force EOF if a read takes place at this position+ // Force EOF if a read later takes place at this positioncurrentBufferIndex--;bufferPosition = BUFFER_SIZE;}@@ -120,7 +120,10 @@ public class RAMInputStream extends IndexInput implements Cloneable {currentBufferIndex = (int) (pos / BUFFER_SIZE);switchCurrentBuffer(false);}- bufferPosition = (int) (pos % BUFFER_SIZE);+ if (pos < BUFFER_SIZE * (long) file.numBuffers()) {+ // do not overwrite bufferPosition if EOF should be thrown on the next read+ bufferPosition = (int) (pos % BUFFER_SIZE);+ }}@Overridediff --git a/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.javaindex c54eb575afb..655609b984d 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java@@ -17,12 +17,14 @@ package org.apache.lucene.store;* limitations under the License.*/+import java.io.EOFException;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.util.ArrayList;import java.util.Arrays;import java.util.List;+import java.util.Random;import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.document.Document;@@ -161,4 +163,30 @@ public class TestRAMDirectory extends BaseDirectoryTestCase {writer.close();}-}++ public void testShouldThrowEOFException() throws Exception {+ final Random random = random();++ try (Directory dir = newDirectory()) {+ final int len = 16 + random().nextInt(2048) / 16 * 16;+ final byte[] bytes = new byte[len];++ try (IndexOutput os = dir.createOutput("foo", newIOContext(random))) {+ os.writeBytes(bytes, bytes.length);+ }++ try (IndexInput is = dir.openInput("foo", newIOContext(random))) {+ try {+ is.seek(0);+ // Here, I go past EOF.+ is.seek(len + random().nextInt(2048));+ // since EOF is not enforced by the previous call in RAMInputStream+ // this call to readBytes should throw the exception.+ is.readBytes(bytes, 0, 16);+ fail("Did not get EOFException");+ } catch (EOFException eof) {+ // expected!+ }+ }+ }+ }}diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.javaindex f1dbe87c137..4ef33b0db74 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java@@ -312,7 +312,7 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {FieldInfo proto = oneDocReader.getFieldInfos().fieldInfo("field");FieldInfo field = new FieldInfo(proto.name, proto.number, proto.hasVectors(), proto.omitsNorms(), proto.hasPayloads(),proto.getIndexOptions(), proto.getDocValuesType(), proto.getDocValuesGen(), new HashMap<>(),- 0, 0);+ proto.getDimensionCount(), proto.getDimensionNumBytes());FieldInfos fieldInfos = new FieldInfos(new FieldInfo[] { field } );