From 496dffb97978367f2a06aa4e1942c6c955df3918 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Wed, 16 Jul 2014 09:01:28 +0000 Subject: [PATCH] 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 --- lucene/CHANGES.txt | 3 ++ .../apache/lucene/store/NIOFSDirectory.java | 3 ++ .../lucene/store/SimpleFSDirectory.java | 3 ++ .../lucene/store/BaseDirectoryTestCase.java | 31 +++++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 9edd5680d14..f5568d3b11f 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -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). 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 8f567c4f901..54202c591e0 100644 --- a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java +++ b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java @@ -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()); } 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 46f10454eea..0f6e5444646 100644 --- a/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java +++ b/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java @@ -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()); } diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java index 8cfead4f38c..2889002dd79 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java @@ -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");