United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6222207 BitSet internal invariants may be violated
6222207 : BitSet internal invariants may be violated

Details
Type:
Bug
Submit Date:
2005-01-27
Status:
Closed
Updated Date:
2010-04-02
Project Name:
JDK
Resolved Date:
2005-02-19
Component:
core-libs
OS:
generic
Sub-Component:
java.util
CPU:
generic
Priority:
P2
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:
6

Related Reports
Relates:
Relates:

Sub Tasks

Description
BitSet.java has some invariants, which can be understood best using
this java code:

    private void checkInvariants() {
	assert(wordsInUse == 0 || words[wordsInUse - 1] != 0);
	assert(wordsInUse >= 0 && wordsInUse <= words.length);
	assert(wordsInUse == words.length || words[wordsInUse] == 0);
    }

But these invariants do not always hold, leading to corrupt internal
data corruption and incorrect results.

One manifestation is this program:
-------------------------------------------------------------------
import java.util.*;

public class Bug {
    public static void main(String[] args) throws Exception {
	BitSet s = new BitSet();
	s.set(127,127);
	System.out.printf("length=%d isEmpty=%b%n", s.length(), s.isEmpty());
    }
}
-------------------------------------------------------------------
Giving:
length=64 isEmpty=false
when correct is:
length=0 isEmpty=true

###@###.### 2005-1-27 06:57:58 GMT
###@###.### 2005-1-27 07:01:50 GMT

                                    

Comments
EVALUATION

The code is not good about checking its invariants, so they are occasionally
violated.  Calls to the submitter's checkInvariants should be placed throughout
the source code to catch violations.  The code has a nasty habit of overshooting
a range when the end of a range is near a 64-bit boundary.  One must code
very carefully to avoid off-by-one error in bit-twiddling code such as this.
###@###.### 2005-1-27 06:57:58 GMT
                                     
2005-01-27



Hardware and Software, Engineered to Work Together