Apache Solr open-source search software
fix OpenBitSet.flip bug when indicies outside current size
Yonik Seeley7b4db83305a8
diff --git a/src/java/org/apache/solr/util/OpenBitSet.java b/src/java/org/apache/solr/util/OpenBitSet.javaindex ea2452134ba..0bca6f1ce2c 100644--- a/src/java/org/apache/solr/util/OpenBitSet.java+++ b/src/java/org/apache/solr/util/OpenBitSet.java@@ -330,6 +330,7 @@ public class OpenBitSet implements Cloneable, Serializable {if (endIndex <= startIndex) return;int startWord = (int)(startIndex>>6);+ if (startWord >= wlen) return;// since endIndex is one past the end, this is index of the last// word to be changed.@@ -439,7 +440,6 @@ public class OpenBitSet implements Cloneable, Serializable {*/public void flip(long startIndex, long endIndex) {if (endIndex <= startIndex) return;-int oldlen = wlen;int startWord = (int)(startIndex>>6);@@ -455,7 +455,7 @@ public class OpenBitSet implements Cloneable, Serializable {***/long startmask = -1L << startIndex;- long endmask = -1L >>> -endIndex; // 64-endIndex is the same as -endIndex due to wrap+ long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrapif (startWord == endWord) {bits[startWord] ^= (startmask & endmask);@@ -464,15 +464,10 @@ public class OpenBitSet implements Cloneable, Serializable {bits[startWord] ^= startmask;- int middle = Math.min(oldlen, endWord);- for (int i=startWord+1; i<middle; i++) {+ for (int i=startWord+1; i<endWord; i++) {bits[i] = ~bits[i];}- if (endWord>middle) {- Arrays.fill(bits,middle,endWord,-1L);- }-bits[endWord] ^= endmask;}diff --git a/src/test/org/apache/solr/util/TestOpenBitSet.java b/src/test/org/apache/solr/util/TestOpenBitSet.javaindex 91e2d705a23..d5a64102179 100644--- a/src/test/org/apache/solr/util/TestOpenBitSet.java+++ b/src/test/org/apache/solr/util/TestOpenBitSet.java@@ -76,26 +76,6 @@ public class TestOpenBitSet extends TestCase {int nOper = rand.nextInt(sz);for (int j=0; j<nOper; j++) {int idx;- int idx1,idx2;-- idx1 = rand.nextInt(sz);- idx2 = rand.nextInt(sz);- if (idx1>idx2) { idx=idx1; idx1=idx2; idx2=idx; }- a.set(idx1,idx2);- b.set(idx1,idx2);-- idx1 = rand.nextInt(sz);- idx2 = rand.nextInt(sz);- if (idx1>idx2) { idx=idx1; idx1=idx2; idx2=idx; }- a.clear(idx1,idx2);- b.clear(idx1,idx2);-- idx1 = rand.nextInt(sz);- idx2 = rand.nextInt(sz);- if (idx1>idx2) { idx=idx1; idx1=idx2; idx2=idx; }- a.flip(idx1,idx2);- b.flip(idx1,idx2);-idx = rand.nextInt(sz);a.set(idx);@@ -122,15 +102,30 @@ public class TestOpenBitSet extends TestCase {// test that the various ways of accessing the bits are equivalentdoGet(a,b);- doNextSetBit(a,b);- doIterate(a,b);-- // test negation- int fromIndex = rand.nextInt(sz+80);- int toIndex = fromIndex + rand.nextInt((sz>>1)+1);- BitSet a_not = (BitSet)a.clone(); a_not.flip(fromIndex,toIndex);- OpenBitSet b_not = (OpenBitSet)b.clone(); b_not.flip(fromIndex,toIndex);- doIterate(a,b);++ // test ranges, including possible extension+ int fromIndex, toIndex;+ fromIndex = rand.nextInt(sz+80);+ toIndex = fromIndex + rand.nextInt((sz>>1)+1);+ BitSet aa = (BitSet)a.clone(); aa.flip(fromIndex,toIndex);+ OpenBitSet bb = (OpenBitSet)b.clone(); bb.flip(fromIndex,toIndex);++ doIterate(aa,bb); // a problem here is from flip or doIterate++ fromIndex = rand.nextInt(sz+80);+ toIndex = fromIndex + rand.nextInt((sz>>1)+1);+ aa = (BitSet)a.clone(); aa.clear(fromIndex,toIndex);+ bb = (OpenBitSet)b.clone(); bb.clear(fromIndex,toIndex);++ doNextSetBit(aa,bb); // a problem here is from clear() or nextSetBit++ fromIndex = rand.nextInt(sz+80);+ toIndex = fromIndex + rand.nextInt((sz>>1)+1);+ aa = (BitSet)a.clone(); aa.set(fromIndex,toIndex);+ bb = (OpenBitSet)b.clone(); bb.set(fromIndex,toIndex);++ doNextSetBit(aa,bb); // a problem here is from set() or nextSetBit+if (a0 != null) {assertEquals( a.equals(a0), b.equals(b0));