LUCENE-4466: bounds check term vectors in segmentreader, not the codec

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1407605 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-11-09 19:52:20 +00:00
parent 18ed36adac
commit 43dc1dcdea
5 changed files with 32 additions and 34 deletions

View File

@ -97,12 +97,6 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader {
@Override
public Fields get(int doc) throws IOException {
// TestTV tests for this in testBadParams... but is this
// really guaranteed by the API?
if (doc < 0 || doc >= offsets.length) {
throw new IllegalArgumentException("doc id out of range");
}
SortedMap<String,SimpleTVTerms> fields = new TreeMap<String,SimpleTVTerms>();
in.seek(offsets[doc]);
readLine();

View File

@ -730,9 +730,6 @@ public class Lucene40TermVectorsReader extends TermVectorsReader implements Clos
@Override
public Fields get(int docID) throws IOException {
if (docID < 0 || docID >= numTotalDocs) {
throw new IllegalArgumentException("doID=" + docID + " is out of bounds [0.." + (numTotalDocs-1) + "]");
}
if (tvx != null) {
Fields fields = new TVFields(docID);
if (fields.size() == 0) {

View File

@ -136,9 +136,7 @@ public final class SegmentReader extends AtomicReader {
@Override
public void document(int docID, StoredFieldVisitor visitor) throws IOException {
if (docID < 0 || docID >= maxDoc()) {
throw new IllegalArgumentException("docID must be >= 0 and < maxDoc=" + maxDoc() + " (got docID=" + docID + ")");
}
checkBounds(docID);
getFieldsReader().visitDocument(docID, visitor);
}
@ -174,9 +172,16 @@ public final class SegmentReader extends AtomicReader {
if (termVectorsReader == null) {
return null;
}
checkBounds(docID);
return termVectorsReader.get(docID);
}
private void checkBounds(int docID) {
if (docID < 0 || docID >= maxDoc()) {
throw new IndexOutOfBoundsException("docID must be >= 0 and < maxDoc=" + maxDoc() + " (got docID=" + docID + ")");
}
}
@Override
public String toString() {
// SegmentInfo.toString takes dir and number of

View File

@ -204,4 +204,28 @@ public class TestSegmentReader extends LuceneTestCase {
assertTrue(results != null);
assertEquals("We do not have 3 term freq vectors", 3, results.size());
}
public void testOutOfBoundsAccess() throws IOException {
int numDocs = reader.maxDoc();
try {
reader.document(-1);
fail();
} catch (IndexOutOfBoundsException expected) {}
try {
reader.getTermVectors(-1);
fail();
} catch (IndexOutOfBoundsException expected) {}
try {
reader.document(numDocs);
fail();
} catch (IndexOutOfBoundsException expected) {}
try {
reader.getTermVectors(numDocs);
fail();
} catch (IndexOutOfBoundsException expected) {}
}
}

View File

@ -328,26 +328,4 @@ public class TestTermVectorsReader extends LuceneTestCase {
}
reader.close();
}
/**
* Make sure exceptions and bad params are handled appropriately
*/
public void testBadParams() throws IOException {
TermVectorsReader reader = null;
try {
reader = Codec.getDefault().termVectorsFormat().vectorsReader(dir, seg.info, fieldInfos, newIOContext(random()));
//Bad document number, good field number
reader.get(50);
fail();
} catch (IllegalArgumentException e) {
// expected exception
} finally {
reader.close();
}
reader = Codec.getDefault().termVectorsFormat().vectorsReader(dir, seg.info, fieldInfos, newIOContext(random()));
//good document number, bad field
Terms vector = reader.get(0).terms("f50");
assertNull(vector);
reader.close();
}
}