Apache Lucene open-source search software
LUCENE-2972: add asserts that access to OBS is within bounds
Michael McCandless5d0b7d21ae97
diff --git a/lucene/src/java/org/apache/lucene/util/OpenBitSet.java b/lucene/src/java/org/apache/lucene/util/OpenBitSet.javaindex 8e093b70f4f..121f8d8b9e8 100644--- a/lucene/src/java/org/apache/lucene/util/OpenBitSet.java+++ b/lucene/src/java/org/apache/lucene/util/OpenBitSet.java@@ -78,11 +78,15 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {protected long[] bits;protected int wlen; // number of words (elements) used in the array+ // Used only for assert:+ private long numBits;+/** Constructs an OpenBitSet large enough to hold numBits.** @param numBits*/public OpenBitSet(long numBits) {+ this.numBits = numBits;bits = new long[bits2words(numBits)];wlen = bits.length;}@@ -107,6 +111,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {public OpenBitSet(long[] bits, int numWords) {this.bits = bits;this.wlen = numWords;+ this.numBits = wlen * 64;}@Override@@ -170,6 +175,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size*/public boolean fastGet(int index) {+ assert index >= 0 && index < numBits;int i = index >> 6; // div 64// signed shift will keep a negative index and force an// array-index-out-of-bounds-exception, removing the need for an explicit check.@@ -194,6 +200,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public boolean fastGet(long index) {+ assert index >= 0 && index < numBits;int i = (int)(index >> 6); // div 64int bit = (int)index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -217,6 +224,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size*/public int getBit(int index) {+ assert index >= 0 && index < numBits;int i = index >> 6; // div 64int bit = index & 0x3f; // mod 64return ((int)(bits[i]>>>bit)) & 0x01;@@ -245,6 +253,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public void fastSet(int index) {+ assert index >= 0 && index < numBits;int wordNum = index >> 6; // div 64int bit = index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -255,6 +264,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public void fastSet(long index) {+ assert index >= 0 && index < numBits;int wordNum = (int)(index >> 6);int bit = (int)index & 0x3f;long bitmask = 1L << bit;@@ -296,6 +306,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {ensureCapacity(index+1);wlen = wordNum+1;}+ numBits = Math.max(numBits, index+1);return wordNum;}@@ -304,6 +315,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public void fastClear(int index) {+ assert index >= 0 && index < numBits;int wordNum = index >> 6;int bit = index & 0x03f;long bitmask = 1L << bit;@@ -321,6 +333,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public void fastClear(long index) {+ assert index >= 0 && index < numBits;int wordNum = (int)(index >> 6); // div 64int bit = (int)index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -415,6 +428,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public boolean getAndSet(int index) {+ assert index >= 0 && index < numBits;int wordNum = index >> 6; // div 64int bit = index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -427,6 +441,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public boolean getAndSet(long index) {+ assert index >= 0 && index < numBits;int wordNum = (int)(index >> 6); // div 64int bit = (int)index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -439,6 +454,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public void fastFlip(int index) {+ assert index >= 0 && index < numBits;int wordNum = index >> 6; // div 64int bit = index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -449,6 +465,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public void fastFlip(long index) {+ assert index >= 0 && index < numBits;int wordNum = (int)(index >> 6); // div 64int bit = (int)index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -467,6 +484,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public boolean flipAndGet(int index) {+ assert index >= 0 && index < numBits;int wordNum = index >> 6; // div 64int bit = index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -478,6 +496,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {* The index should be less than the OpenBitSet size.*/public boolean flipAndGet(long index) {+ assert index >= 0 && index < numBits;int wordNum = (int)(index >> 6); // div 64int bit = (int)index & 0x3f; // mod 64long bitmask = 1L << bit;@@ -674,6 +693,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {public void union(OpenBitSet other) {int newLen = Math.max(wlen,other.wlen);ensureCapacityWords(newLen);+ numBits = Math.max(other.numBits, numBits);long[] thisArr = this.bits;long[] otherArr = other.bits;@@ -702,6 +722,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable {public void xor(OpenBitSet other) {int newLen = Math.max(wlen,other.wlen);ensureCapacityWords(newLen);+ numBits = Math.max(other.numBits, numBits);long[] thisArr = this.bits;long[] otherArr = other.bits;