Skip to content

Fix ArrayIndexOutOfBoundsException in mutable.BitSet.<<= #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,42 @@ class MutableBitSetDecorator(protected val bs: mutable.BitSet) {

private def shiftLeftInPlace(shiftBy: Int): Unit = {

val bitOffset = shiftBy & WordMask
val wordOffset = shiftBy >>> LogWL

var significantWordCount = bs.nwords
while (significantWordCount > 0 && bs.word(significantWordCount - 1) == 0) {
significantWordCount -= 1
}

if (bitOffset == 0) {
val newSize = significantWordCount + wordOffset
require(newSize <= MaxSize)
ensureCapacity(newSize)
System.arraycopy(bs.elems, 0, bs.elems, wordOffset, significantWordCount)
} else {
val revBitOffset = WordLength - bitOffset
val extraBits = bs.elems(significantWordCount - 1) >>> revBitOffset
val extraWordCount = if (extraBits == 0) 0 else 1
val newSize = significantWordCount + wordOffset + extraWordCount
require(newSize <= MaxSize)
ensureCapacity(newSize)
var i = significantWordCount - 1
var previous = bs.elems(i)
while (i > 0) {
val current = bs.elems(i - 1)
bs.elems(i + wordOffset) = (current >>> revBitOffset) | (previous << bitOffset)
previous = current
i -= 1
if (significantWordCount > 0) {

val bitOffset = shiftBy & WordMask
val wordOffset = shiftBy >>> LogWL

if (bitOffset == 0) {
val newSize = significantWordCount + wordOffset
require(newSize <= MaxSize)
ensureCapacity(newSize)
System.arraycopy(bs.elems, 0, bs.elems, wordOffset, significantWordCount)
} else {
val revBitOffset = WordLength - bitOffset
val extraBits = bs.elems(significantWordCount - 1) >>> revBitOffset
val extraWordCount = if (extraBits == 0) 0 else 1
val newSize = significantWordCount + wordOffset + extraWordCount
require(newSize <= MaxSize)
ensureCapacity(newSize)
var i = significantWordCount - 1
var previous = bs.elems(i)
while (i > 0) {
val current = bs.elems(i - 1)
bs.elems(i + wordOffset) = (current >>> revBitOffset) | (previous << bitOffset)
previous = current
i -= 1
}
bs.elems(wordOffset) = previous << bitOffset
if (extraWordCount != 0) bs.elems(newSize - 1) = extraBits
}
bs.elems(wordOffset) = previous << bitOffset
if (extraWordCount != 0) bs.elems(newSize - 1) = extraBits

java.util.Arrays.fill(bs.elems, 0, wordOffset, 0)
}
java.util.Arrays.fill(bs.elems, 0, wordOffset, 0)
}

private def shiftRightInPlace(shiftBy: Int): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package scala.collection.decorators

import org.junit.{Assert, Test}

import scala.collection.BitSetOps
import scala.collection.mutable.BitSet

class MutableBitSetDecoratorTest {

import Assert.{assertEquals, assertSame}
import Assert.{assertEquals, assertSame, assertTrue}
import BitSet.empty

@Test
Expand Down Expand Up @@ -104,4 +105,12 @@ class MutableBitSetDecoratorTest {
}
}

@Test
def shiftLeftTwoEmptyWords(): Unit = {
val twoWords = BitSet(BitSetOps.WordLength + 1)
twoWords ^= twoWords
twoWords <<= 1
assertTrue(twoWords.isEmpty)
}

}