src/java.base/share/classes/java/util/BitSet.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 50610 9b85066e259b
child 58679 9c3209ff7550
--- a/src/java.base/share/classes/java/util/BitSet.java	Thu Oct 17 20:27:44 2019 +0100
+++ b/src/java.base/share/classes/java/util/BitSet.java	Thu Oct 17 20:53:35 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -83,6 +83,7 @@
      * bit position i % 64 (where bit position 0 refers to the least
      * significant bit and 63 refers to the most significant bit).
      */
+    @java.io.Serial
     private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("bits", long[].class),
     };
@@ -104,6 +105,7 @@
     private transient boolean sizeIsSticky = false;
 
     /* use serialVersionUID from JDK 1.0.2 for interoperability */
+    @java.io.Serial
     private static final long serialVersionUID = 7997698588986878753L;
 
     /**
@@ -1124,6 +1126,7 @@
      * Save the state of the {@code BitSet} instance to a stream (i.e.,
      * serialize it).
      */
+    @java.io.Serial
     private void writeObject(ObjectOutputStream s)
         throws IOException {
 
@@ -1141,6 +1144,7 @@
      * Reconstitute the {@code BitSet} instance from a stream (i.e.,
      * deserialize it).
      */
+    @java.io.Serial
     private void readObject(ObjectInputStream s)
         throws IOException, ClassNotFoundException {
 
@@ -1182,9 +1186,13 @@
     public String toString() {
         checkInvariants();
 
+        final int MAX_INITIAL_CAPACITY = Integer.MAX_VALUE - 8;
         int numBits = (wordsInUse > 128) ?
             cardinality() : wordsInUse * BITS_PER_WORD;
-        StringBuilder b = new StringBuilder(6*numBits + 2);
+        // Avoid overflow in the case of a humongous numBits
+        int initialCapacity = (numBits <= (MAX_INITIAL_CAPACITY - 2) / 6) ?
+            6 * numBits + 2 : MAX_INITIAL_CAPACITY;
+        StringBuilder b = new StringBuilder(initialCapacity);
         b.append('{');
 
         int i = nextSetBit(0);