You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2014/07/16 11:01:28 UTC

svn commit: r1610943 - in /lucene/dev/trunk/lucene: ./ core/src/java/org/apache/lucene/store/ test-framework/src/java/org/apache/lucene/store/

Author: uschindler
Date: Wed Jul 16 09:01:28 2014
New Revision: 1610943

URL: http://svn.apache.org/r1610943
Log:
LUCENE-5827: Make all Directory implementations correctly fail with IllegalArgumentException if slices are out of bounds

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1610943&r1=1610942&r2=1610943&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Wed Jul 16 09:01:28 2014
@@ -172,6 +172,9 @@ Bug Fixes
 
 * LUCENE-5824: Fix hunspell 'long' flag handling. (Robert Muir)
 
+* LUCENE-5827: Make all Directory implementations correctly fail with
+  IllegalArgumentException if slices are out of bounds.  (Uwe SChindler)
+
 Test Framework
 
 * LUCENE-5786: Unflushed/ truncated events file (hung testing subprocess).

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java?rev=1610943&r1=1610942&r2=1610943&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java Wed Jul 16 09:01:28 2014
@@ -133,6 +133,9 @@ public class NIOFSDirectory extends FSDi
     
     @Override
     public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
+      if (offset < 0 || length < 0 || offset + length > this.length()) {
+        throw new IllegalArgumentException("slice() " + sliceDescription + " out of bounds: "  + this);
+      }
       return new NIOFSIndexInput(sliceDescription, channel, off + offset, length, getBufferSize());
     }
 

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java?rev=1610943&r1=1610942&r2=1610943&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java Wed Jul 16 09:01:28 2014
@@ -110,6 +110,9 @@ public class SimpleFSDirectory extends F
     
     @Override
     public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
+      if (offset < 0 || length < 0 || offset + length > this.length()) {
+        throw new IllegalArgumentException("slice() " + sliceDescription + " out of bounds: "  + this);
+      }
       return new SimpleFSIndexInput(sliceDescription, file, off + offset, length, getBufferSize());
     }
 

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java?rev=1610943&r1=1610942&r2=1610943&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java Wed Jul 16 09:01:28 2014
@@ -459,6 +459,37 @@ public abstract class BaseDirectoryTestC
     dir.close();
   }
 
+  public void testSliceOutOfBounds() throws Exception {
+    Directory dir = getDirectory(createTempDir("testSliceOutOfBounds"));
+    IndexOutput o = dir.createOutput("out", newIOContext(random()));
+    final int len = random().nextInt(2040) + 8;
+    byte[] b = new byte[len];
+    o.writeBytes(b, 0, len);
+    o.close();
+    IndexInput i = dir.openInput("out", newIOContext(random()));
+    try {
+      i.slice("slice1", 0, len + 1);
+      fail("Did not get IllegalArgumentException");
+    } catch (IllegalArgumentException iae) {
+      // pass
+    }
+    try {
+      i.slice("slice2", -1, len);
+      fail("Did not get IllegalArgumentException");
+    } catch (IllegalArgumentException iae) {
+      // pass
+    }
+    IndexInput slice = i.slice("slice3", 4, len / 2);
+    try {
+      slice.slice("slice3sub", 1, len / 2);
+      fail("Did not get IllegalArgumentException");
+    } catch (IllegalArgumentException iae) {
+      // pass
+    }
+    i.close();
+    dir.close();    
+  }
+  
   // LUCENE-3382 -- make sure we get exception if the directory really does not exist.
   public void testNoDir() throws Throwable {
     File tempDir = createTempDir("doesnotexist");