Skip to content

Apache Lucene open-source search software

LUCENE-4888: Never call DocIdSetIterator.advance with target <= docID().

Adrien Grand218588c9acb0
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index cfce7f17d34..2127a6c3c0e 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -197,6 +197,11 @@ Bug Fixes
* LUCENE-4893: Facet counts were multiplied as many times as
FacetsCollector.getFacetResults() is called. (Shai Erera)
+* LUCENE-4888: Fixed SloppyPhraseScorer, MultiDocs(AndPositions)Enum and
+ MultiSpansWrapper which happened to sometimes call DocIdSetIterator.advance
+ with target<=current (in this case the behavior of advance is undefined).
+ (Adrien Grand)
+
Documentation
* LUCENE-4841: Added example SimpleSortedSetFacetsExample to show how
diff --git a/lucene/core/src/java/org/apache/lucene/index/MultiDocsAndPositionsEnum.java b/lucene/core/src/java/org/apache/lucene/index/MultiDocsAndPositionsEnum.java
index 2514c23c152..8bd2b84432b 100644
--- a/lucene/core/src/java/org/apache/lucene/index/MultiDocsAndPositionsEnum.java
+++ b/lucene/core/src/java/org/apache/lucene/index/MultiDocsAndPositionsEnum.java
@@ -89,9 +89,16 @@ public final class MultiDocsAndPositionsEnum extends DocsAndPositionsEnum {
@Override
public int advance(int target) throws IOException {
+ assert target > doc;
while(true) {
if (current != null) {
- final int doc = current.advance(target-currentBase);
+ final int doc;
+ if (target < currentBase) {
+ // target was in the previous slice but there was no matching doc after it
+ doc = current.nextDoc();
+ } else {
+ doc = current.advance(target-currentBase);
+ }
if (doc == NO_MORE_DOCS) {
current = null;
} else {
diff --git a/lucene/core/src/java/org/apache/lucene/index/MultiDocsEnum.java b/lucene/core/src/java/org/apache/lucene/index/MultiDocsEnum.java
index e0e75e5f4ef..3d778b005f4 100644
--- a/lucene/core/src/java/org/apache/lucene/index/MultiDocsEnum.java
+++ b/lucene/core/src/java/org/apache/lucene/index/MultiDocsEnum.java
@@ -90,9 +90,16 @@ public final class MultiDocsEnum extends DocsEnum {
@Override
public int advance(int target) throws IOException {
+ assert target > doc;
while(true) {
if (current != null) {
- final int doc = current.advance(target-currentBase);
+ final int doc;
+ if (target < currentBase) {
+ // target was in the previous slice but there was no matching doc after it
+ doc = current.nextDoc();
+ } else {
+ doc = current.advance(target-currentBase);
+ }
if (doc == NO_MORE_DOCS) {
current = null;
} else {
diff --git a/lucene/core/src/java/org/apache/lucene/search/SloppyPhraseScorer.java b/lucene/core/src/java/org/apache/lucene/search/SloppyPhraseScorer.java
index a90adc60b5b..a4ad72a8673 100644
--- a/lucene/core/src/java/org/apache/lucene/search/SloppyPhraseScorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/SloppyPhraseScorer.java
@@ -561,7 +561,7 @@ final class SloppyPhraseScorer extends Scorer {
@Override
public int nextDoc() throws IOException {
- return advance(max.doc);
+ return advance(max.doc + 1); // advance to the next doc after #docID()
}
@Override
@@ -571,27 +571,25 @@ final class SloppyPhraseScorer extends Scorer {
@Override
public int advance(int target) throws IOException {
- sloppyFreq = 0.0f;
- if (!advanceMin(target)) {
- return NO_MORE_DOCS;
- }
- boolean restart=false;
- while (sloppyFreq == 0.0f) {
- while (min.doc < max.doc || restart) {
- restart = false;
+ assert target > docID();
+ do {
+ if (!advanceMin(target)) {
+ return NO_MORE_DOCS;
+ }
+ while (min.doc < max.doc) {
if (!advanceMin(max.doc)) {
return NO_MORE_DOCS;
- }
+ }
}
// found a doc with all of the terms
sloppyFreq = phraseFreq(); // check for phrase
- restart = true;
- }
+ target = min.doc + 1; // next target in case sloppyFreq is still 0
+ } while (sloppyFreq == 0f);
// found a match
return max.doc;
}
-
+
@Override
public long cost() {
return cost;
diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java b/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java
index 3322aee55e2..d177abbf359 100644
--- a/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java
+++ b/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java
@@ -29,7 +29,11 @@ public abstract class Spans {
public abstract boolean next() throws IOException;
/** Skips to the first match beyond the current, whose document number is
- * greater than or equal to <i>target</i>. <p>Returns true iff there is such
+ * greater than or equal to <i>target</i>.
+ * <p>The behavior of this method is <b>undefined</b> when called with
+ * <code> target &le; current</code>, or after the iterator has exhausted.
+ * Both cases may result in unpredicted behavior.
+ * <p>Returns true iff there is such
* a match. <p>Behaves as if written: <pre class="prettyprint">
* boolean skipTo(int target) {
* do {
diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java b/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java
index 484f453c657..d4974a517f5 100644
--- a/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java
+++ b/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java
@@ -71,6 +71,7 @@ public class TermSpans extends Spans {
@Override
public boolean skipTo(int target) throws IOException {
+ assert target > doc;
doc = postings.advance(target);
if (doc == DocIdSetIterator.NO_MORE_DOCS) {
return false;
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestSegmentTermDocs.java b/lucene/core/src/test/org/apache/lucene/index/TestSegmentTermDocs.java
index 5f52bf1aa42..7366a92caab 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestSegmentTermDocs.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestSegmentTermDocs.java
@@ -149,7 +149,7 @@ public class TestSegmentTermDocs extends LuceneTestCase {
assertTrue(tdocs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(1, tdocs.docID());
assertEquals(4, tdocs.freq());
- assertTrue(tdocs.advance(0) != DocIdSetIterator.NO_MORE_DOCS);
+ assertTrue(tdocs.advance(2) != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(2, tdocs.docID());
assertTrue(tdocs.advance(4) != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(4, tdocs.docID());
@@ -189,7 +189,7 @@ public class TestSegmentTermDocs extends LuceneTestCase {
assertTrue(tdocs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(11, tdocs.docID());
assertEquals(4, tdocs.freq());
- assertTrue(tdocs.advance(5) != DocIdSetIterator.NO_MORE_DOCS);
+ assertTrue(tdocs.advance(12) != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(12, tdocs.docID());
assertTrue(tdocs.advance(15) != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(15, tdocs.docID());
@@ -233,7 +233,7 @@ public class TestSegmentTermDocs extends LuceneTestCase {
assertTrue(tdocs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(27, tdocs.docID());
assertEquals(4, tdocs.freq());
- assertTrue(tdocs.advance(5) != DocIdSetIterator.NO_MORE_DOCS);
+ assertTrue(tdocs.advance(28) != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(28, tdocs.docID());
assertTrue(tdocs.advance(40) != DocIdSetIterator.NO_MORE_DOCS);
assertEquals(40, tdocs.docID());
diff --git a/lucene/core/src/test/org/apache/lucene/search/spans/MultiSpansWrapper.java b/lucene/core/src/test/org/apache/lucene/search/spans/MultiSpansWrapper.java
index 8308d348ac4..6d38820db9c 100644
--- a/lucene/core/src/test/org/apache/lucene/search/spans/MultiSpansWrapper.java
+++ b/lucene/core/src/test/org/apache/lucene/search/spans/MultiSpansWrapper.java
@@ -53,7 +53,6 @@ public class MultiSpansWrapper extends Spans { // can't be package private due t
this.leaves = leaves;
this.numLeaves = leaves.size();
this.termContexts = termContexts;
-
}
public static Spans wrap(IndexReaderContext topLevelReaderContext, SpanQuery query) throws IOException {
@@ -112,7 +111,12 @@ public class MultiSpansWrapper extends Spans { // can't be package private due t
current = query.getSpans(ctx, ctx.reader().getLiveDocs(), termContexts);
}
while (true) {
- if (current.skipTo(target - leaves.get(leafOrd).docBase)) {
+ if (target < leaves.get(leafOrd).docBase) {
+ // target was in the previous slice
+ if (current.next()) {
+ return true;
+ }
+ } else if (current.skipTo(target - leaves.get(leafOrd).docBase)) {
return true;
}
if (++leafOrd < numLeaves) {
diff --git a/lucene/core/src/test/org/apache/lucene/search/spans/TestBasics.java b/lucene/core/src/test/org/apache/lucene/search/spans/TestBasics.java
index c9db0de3eb8..82ec34e8d86 100644
--- a/lucene/core/src/test/org/apache/lucene/search/spans/TestBasics.java
+++ b/lucene/core/src/test/org/apache/lucene/search/spans/TestBasics.java
@@ -593,8 +593,8 @@ public class TestBasics extends LuceneTestCase {
boolean hasMore = true;
do {
- hasMore = skipToAccoringToJavaDocs(s1, s1.doc());
- assertEquals(hasMore, s2.skipTo(s2.doc()));
+ hasMore = skipToAccoringToJavaDocs(s1, s1.doc() + 1);
+ assertEquals(hasMore, s2.skipTo(s2.doc() + 1));
assertEquals(s1.doc(), s2.doc());
} while (hasMore);
}
diff --git a/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java b/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java
index bcae1f22e7a..1597c7032cb 100644
--- a/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java
+++ b/lucene/misc/src/test/org/apache/lucene/index/sorter/SorterTestBase.java
@@ -283,7 +283,7 @@ public abstract class SorterTestBase extends LuceneTestCase {
assertTrue(((SortingDocsAndPositionsEnum) sortedPositions).reused(reuse)); // make sure reuse worked
}
doc = 0;
- while ((doc = sortedPositions.advance(doc)) != DocIdSetIterator.NO_MORE_DOCS) {
+ while ((doc = sortedPositions.advance(doc + _TestUtil.nextInt(random(), 1, 5))) != DocIdSetIterator.NO_MORE_DOCS) {
int freq = sortedPositions.freq();
assertEquals("incorrect freq for doc=" + doc, sortedValues[doc].intValue() / 10 + 1, freq);
for (int i = 0; i < freq; i++) {
diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java
index 7bcd9f616dd..ec82de80558 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java
@@ -225,38 +225,42 @@ public class AssertingAtomicReader extends FilterAtomicReader {
static enum DocsEnumState { START, ITERATING, FINISHED };
static class AssertingDocsEnum extends FilterDocsEnum {
private DocsEnumState state = DocsEnumState.START;
+ private int doc;
public AssertingDocsEnum(DocsEnum in) {
super(in);
int docid = in.docID();
assert docid == -1 || docid == DocIdSetIterator.NO_MORE_DOCS : "invalid initial doc id: " + docid;
+ doc = -1;
}
@Override
public int nextDoc() throws IOException {
assert state != DocsEnumState.FINISHED : "nextDoc() called after NO_MORE_DOCS";
int nextDoc = super.nextDoc();
- assert nextDoc >= 0 : "invalid doc id: " + nextDoc;
+ assert nextDoc > doc : "backwards nextDoc from " + doc + " to " + nextDoc;
if (nextDoc == DocIdSetIterator.NO_MORE_DOCS) {
state = DocsEnumState.FINISHED;
} else {
state = DocsEnumState.ITERATING;
}
- return nextDoc;
+ assert docID() == nextDoc;
+ return doc = nextDoc;
}
@Override
public int advance(int target) throws IOException {
assert state != DocsEnumState.FINISHED : "advance() called after NO_MORE_DOCS";
+ assert target > doc : "target must be > docID(), got " + target + " <= " + doc;
int advanced = super.advance(target);
- assert advanced >= 0 : "invalid doc id: " + advanced;
assert advanced >= target : "backwards advance from: " + target + " to: " + advanced;
if (advanced == DocIdSetIterator.NO_MORE_DOCS) {
state = DocsEnumState.FINISHED;
} else {
state = DocsEnumState.ITERATING;
}
- return advanced;
+ assert docID() == advanced;
+ return doc = advanced;
}
// NOTE: We don't assert anything for docId(). Specifically DocsEnum javadocs
@@ -277,18 +281,20 @@ public class AssertingAtomicReader extends FilterAtomicReader {
private DocsEnumState state = DocsEnumState.START;
private int positionMax = 0;
private int positionCount = 0;
+ private int doc;
public AssertingDocsAndPositionsEnum(DocsAndPositionsEnum in) {
super(in);
int docid = in.docID();
assert docid == -1 || docid == DocIdSetIterator.NO_MORE_DOCS : "invalid initial doc id: " + docid;
+ doc = -1;
}
@Override
public int nextDoc() throws IOException {
assert state != DocsEnumState.FINISHED : "nextDoc() called after NO_MORE_DOCS";
int nextDoc = super.nextDoc();
- assert nextDoc >= 0 : "invalid doc id: " + nextDoc;
+ assert nextDoc > doc : "backwards nextDoc from " + doc + " to " + nextDoc;
positionCount = 0;
if (nextDoc == DocIdSetIterator.NO_MORE_DOCS) {
state = DocsEnumState.FINISHED;
@@ -297,14 +303,15 @@ public class AssertingAtomicReader extends FilterAtomicReader {
state = DocsEnumState.ITERATING;
positionMax = super.freq();
}
- return nextDoc;
+ assert docID() == nextDoc;
+ return doc = nextDoc;
}
@Override
public int advance(int target) throws IOException {
assert state != DocsEnumState.FINISHED : "advance() called after NO_MORE_DOCS";
+ assert target > doc : "target must be > docID(), got " + target + " <= " + doc;
int advanced = super.advance(target);
- assert advanced >= 0 : "invalid doc id: " + advanced;
assert advanced >= target : "backwards advance from: " + target + " to: " + advanced;
positionCount = 0;
if (advanced == DocIdSetIterator.NO_MORE_DOCS) {
@@ -314,7 +321,8 @@ public class AssertingAtomicReader extends FilterAtomicReader {
state = DocsEnumState.ITERATING;
positionMax = super.freq();
}
- return advanced;
+ assert docID() == advanced;
+ return doc = advanced;
}
@Override