You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2021/03/10 10:03:47 UTC

[lucene] 10/18: LUCENE-6932: RAMDirectory's IndexInput should always throw EOFE if you seek beyond the end of the file and then try to read

This is an automated email from the ASF dual-hosted git repository.

dweiss pushed a commit to branch branch_5_4
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit ad2c18cd72751cd80c11b4916980fb510eaf8f9f
Author: Michael McCandless <mi...@apache.org>
AuthorDate: Sun Jan 17 19:28:10 2016 +0000

    LUCENE-6932: RAMDirectory's IndexInput should always throw EOFE if you seek beyond the end of the file and then try to read
    
    git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/branch_5x@1725112 13f79535-47bb-0310-9956-ffa450edef68
    
    Conflicts:
    	lucene/CHANGES.txt
---
 lucene/CHANGES.txt                                 |  4 +++
 .../org/apache/lucene/store/RAMInputStream.java    |  7 +++--
 .../org/apache/lucene/store/TestRAMDirectory.java  | 30 +++++++++++++++++++++-
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index d3fa220..fa48106 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -14,6 +14,10 @@ Bug Fixes
 * LUCENE-7019: add two-phase iteration to GeoPointTermQueryConstantScoreWrapper.
   (Robert Muir via Nick Knize)
 
+* 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)
+
 ======================= Lucene 5.4.1 =======================
 
 Bug Fixes
diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java
index 6cd2e16..46d5dc1 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 position
         currentBufferIndex--;
         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);
+    }
   }
 
   @Override
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java
index fa3f71e..30ceba5 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;
@@ -160,4 +162,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!
+        }
+      }
+    }
+  }}