From 46fe671e8ce975b88fdc69c8879ce08bb9a0e0ef Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Fri, 2 Sep 2011 16:43:31 +0000 Subject: [PATCH] don't throw scary EOFE if you pass oob docID to IR.document git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1164620 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/index/IndexReader.java | 3 +++ .../org/apache/lucene/index/SegmentReader.java | 3 +++ .../apache/lucene/index/TestIndexReader.java | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/lucene/src/java/org/apache/lucene/index/IndexReader.java b/lucene/src/java/org/apache/lucene/index/IndexReader.java index e99ec6922b6..a25b9d82b00 100644 --- a/lucene/src/java/org/apache/lucene/index/IndexReader.java +++ b/lucene/src/java/org/apache/lucene/index/IndexReader.java @@ -974,6 +974,9 @@ public abstract class IndexReader implements Cloneable,Closeable { // IndexableField public Document document(int docID) throws CorruptIndexException, IOException { ensureOpen(); + if (docID < 0 || docID >= maxDoc()) { + throw new IllegalArgumentException("docID must be >= 0 and < maxDoc=" + maxDoc() + " (got docID=" + docID + ")"); + } final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(); document(docID, visitor); return visitor.getDocument(); diff --git a/lucene/src/java/org/apache/lucene/index/SegmentReader.java b/lucene/src/java/org/apache/lucene/index/SegmentReader.java index 36679fd2a2d..622e45e2cd8 100644 --- a/lucene/src/java/org/apache/lucene/index/SegmentReader.java +++ b/lucene/src/java/org/apache/lucene/index/SegmentReader.java @@ -455,6 +455,9 @@ public class SegmentReader extends IndexReader implements Cloneable { public void document(int docID, StoredFieldVisitor visitor) throws CorruptIndexException, IOException { ensureOpen(); + if (docID < 0 || docID >= maxDoc()) { + throw new IllegalArgumentException("docID must be >= 0 and < maxDoc=" + maxDoc() + " (got docID=" + docID + ")"); + } getFieldsReader().visitDocument(docID, visitor); } diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexReader.java b/lucene/src/test/org/apache/lucene/index/TestIndexReader.java index 2384681093a..c45569ef5f7 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexReader.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexReader.java @@ -1388,4 +1388,21 @@ public class TestIndexReader extends LuceneTestCase assertEquals(3, closeCount[0]); dir.close(); } + + public void testOOBDocID() throws Exception { + Directory dir = newDirectory(); + IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))); + writer.addDocument(new Document()); + IndexReader r = writer.getReader(); + writer.close(); + r.document(0); + try { + r.document(1); + fail("did not hit exception"); + } catch (IllegalArgumentException iae) { + // expected + } + r.close(); + dir.close(); + } }