8189284: More refactoring for deserialization cases
authorsmarks
Mon, 27 Nov 2017 17:30:49 -0800
changeset 48586 8877e857fdd7
parent 48585 d9fcb7ba8133
child 48587 f2e87b6383af
8189284: More refactoring for deserialization cases Reviewed-by: rriggs, igerasim, rhalade, skoivu
src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java
--- a/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java	Tue Nov 28 01:08:26 2017 +0300
+++ b/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java	Mon Nov 27 17:30:49 2017 -0800
@@ -1608,4 +1608,30 @@
         }
     }
 
+    /**
+     * Deserializes this queue and then checks some invariants.
+     *
+     * @param s the input stream
+     * @throws ClassNotFoundException if the class of a serialized object
+     *         could not be found
+     * @throws java.io.InvalidObjectException if invariants are violated
+     * @throws java.io.IOException if an I/O error occurs
+     */
+    private void readObject(java.io.ObjectInputStream s)
+        throws java.io.IOException, ClassNotFoundException {
+
+        // Read in items array and various fields
+        s.defaultReadObject();
+
+        // Check invariants over count and index fields. Note that
+        // if putIndex==takeIndex, count can be either 0 or items.length.
+        if (items.length == 0 ||
+            takeIndex < 0 || takeIndex >= items.length ||
+            putIndex  < 0 || putIndex  >= items.length ||
+            count < 0     || count     >  items.length ||
+            Math.floorMod(putIndex - takeIndex, items.length) !=
+            Math.floorMod(count, items.length)) {
+            throw new java.io.InvalidObjectException("invariants violated");
+        }
+    }
 }