LUCENE-5827: Make all Directory implementations correctly fail with IllegalArgumentException if slices are out of bounds

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1610943 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2014-07-16 09:01:28 +00:00
parent b270cdde57
commit 496dffb979
4 changed files with 40 additions and 0 deletions

View File

@ -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).

View File

@ -133,6 +133,9 @@ public class NIOFSDirectory extends FSDirectory {
@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());
}

View File

@ -110,6 +110,9 @@ public class SimpleFSDirectory extends FSDirectory {
@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());
}

View File

@ -459,6 +459,37 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {
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");