# HG changeset patch # User smarks # Date 1511832649 28800 # Node ID 8877e857fdd715191ca73bb39a291e5c212a77a6 # Parent d9fcb7ba813335aebd1e5b3dc87b07a21a6b779c 8189284: More refactoring for deserialization cases Reviewed-by: rriggs, igerasim, rhalade, skoivu diff -r d9fcb7ba8133 -r 8877e857fdd7 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"); + } + } }