LUCENE-3874: don't let bogus positions corrumpt the index

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1301155 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-03-15 19:12:41 +00:00
parent 35dad942bb
commit faca588e93
3 changed files with 38 additions and 3 deletions

View File

@ -937,6 +937,12 @@ Bug fixes
* LUCENE-3872: IndexWriter.close() now throws IllegalStateException if
you call it after calling prepareCommit() without calling commit()
first. (Tim Bogaert via Mike McCandless)
* LUCENE-3874: Throw IllegalArgumentException from IndexWriter (rather
than producing a corrupt index), if a positionIncrement would cause
integer overflow. This can happen, for example when using a buggy
TokenStream that forgets to call clearAttributes() in combination
with a StopFilter. (Robert Muir)
Optimizations

View File

@ -109,10 +109,16 @@ final class DocInverterPerField extends DocFieldConsumerPerField {
if (!hasMoreTokens) break;
final int posIncr = posIncrAttribute.getPositionIncrement();
fieldState.position += posIncr;
if (fieldState.position > 0) {
fieldState.position--;
int position = fieldState.position + posIncr;
if (position > 0) {
position--;
} else if (position < 0) {
throw new IllegalArgumentException("position overflow for field '" + field.name() + "'");
}
// position is legal, we can safely place it in fieldState now.
// not sure if anything will use fieldState after non-aborting exc...
fieldState.position = position;
if (posIncr == 0)
fieldState.numOverlap++;

View File

@ -1494,4 +1494,27 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
uoe.doFail = false;
d.close();
}
public void testIllegalPositions() throws Exception {
Directory dir = newDirectory();
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, null));
Document doc = new Document();
Token t1 = new Token("foo", 0, 3);
t1.setPositionIncrement(Integer.MAX_VALUE);
Token t2 = new Token("bar", 4, 7);
t2.setPositionIncrement(200);
TokenStream overflowingTokenStream = new CannedTokenStream(
new Token[] { t1, t2 }
);
Field field = new TextField("foo", overflowingTokenStream);
doc.add(field);
try {
iw.addDocument(doc);
fail();
} catch (IllegalArgumentException expected) {
// expected exception
}
iw.close();
dir.close();
}
}