8076442: Cannot fully read BitSet.stream() if bit Integer.MAX_VALUE is set
authorchegar
Tue, 07 Apr 2015 10:33:08 +0100
changeset 29816 43ad6bf3975b
parent 29815 a50c9d80a80f
child 29817 f2832da6a04a
8076442: Cannot fully read BitSet.stream() if bit Integer.MAX_VALUE is set Reviewed-by: alanb, henryjen
jdk/src/java.base/share/classes/java/util/BitSet.java
jdk/test/java/util/BitSet/BitSetStreamTest.java
--- a/jdk/src/java.base/share/classes/java/util/BitSet.java	Mon Apr 06 13:41:10 2015 -0700
+++ b/jdk/src/java.base/share/classes/java/util/BitSet.java	Tue Apr 07 10:33:08 2015 +0100
@@ -1229,7 +1229,7 @@
             public int nextInt() {
                 if (next != -1) {
                     int ret = next;
-                    next = nextSetBit(next+1);
+                    next = (next == Integer.MAX_VALUE) ? -1 : nextSetBit(next+1);
                     return ret;
                 } else {
                     throw new NoSuchElementException();
--- a/jdk/test/java/util/BitSet/BitSetStreamTest.java	Mon Apr 06 13:41:10 2015 -0700
+++ b/jdk/test/java/util/BitSet/BitSetStreamTest.java	Tue Apr 07 10:33:08 2015 +0100
@@ -43,7 +43,7 @@
 /**
  * @test
  * @summary test BitSet stream
- * @bug 8012645
+ * @bug 8012645 8076442
  * @run testng BitSetStreamTest
  */
 public class BitSetStreamTest {
@@ -70,6 +70,7 @@
         { "step 5", IntStream.range(0, 255).map(f -> f * 5) },
         { "step 7", IntStream.range(0, 255).map(f -> f * 7) },
         { "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000) },
+        { "max int", IntStream.of(Integer.MAX_VALUE) },
         { "25 fibs", IntStream.generate(new Fibs()).limit(25) }
     };
 
@@ -106,6 +107,8 @@
         for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
             assertTrue(it.hasNext());
             assertEquals(it.nextInt(), i);
+            if (i == Integer.MAX_VALUE)
+                break; // or (i+1) would overflow
         }
         assertFalse(it.hasNext());
     }