< prev index next >

test/java/util/EnumSet/BogusEnumSet.java

Print this page
rev 10985 : [mq]: 8061549


  65             0x43, 0x4b, 0x45, 0x44, 0x7e, 0x71, 0x0, 0x7e, 0x0, 0x5, 0x74, 0x0,
  66             0x7, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x7e, 0x71, 0x0,
  67             0x7e, 0x0, 0x5, 0x74, 0x0, 0xd, 0x54, 0x49, 0x4d, 0x45, 0x44,
  68             0x5f, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x7e, 0x71, 0x0,
  69             0x7e, 0x0, 0x5, 0x74, 0x0, 0xa, 0x54, 0x45, 0x52, 0x4d, 0x49,
  70             0x4e, 0x41, 0x54, 0x45, 0x44, (byte)0xff, (byte)0xff, (byte)0xff,
  71             (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff
  72         };
  73 
  74         try {
  75           // Should fail, but instead creates corrupt EnumSet
  76           @SuppressWarnings("unchecked")
  77           EnumSet<Thread.State> es = (EnumSet<Thread.State>)
  78               deserialize(serializedForm);
  79 
  80           // Demonstrates corruption
  81           System.out.println("Enum size: " + Thread.State.values().length); // 6
  82           System.out.println("Set size: " + es.size()); // 64
  83           System.out.println("Set: " + es); // Throws IndexOutOfBoundsException
  84           throw new AssertionError("Expected exception InvalidObjectException not thrown");
  85         } catch (java.io.InvalidObjectException _) { /* OK */ }
  86     }
  87 
  88     private static Object deserialize(byte[] sf) throws Throwable {
  89       return new ObjectInputStream(
  90           new ByteArrayInputStream(sf))
  91           .readObject();
  92     }
  93 }


  65             0x43, 0x4b, 0x45, 0x44, 0x7e, 0x71, 0x0, 0x7e, 0x0, 0x5, 0x74, 0x0,
  66             0x7, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x7e, 0x71, 0x0,
  67             0x7e, 0x0, 0x5, 0x74, 0x0, 0xd, 0x54, 0x49, 0x4d, 0x45, 0x44,
  68             0x5f, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x7e, 0x71, 0x0,
  69             0x7e, 0x0, 0x5, 0x74, 0x0, 0xa, 0x54, 0x45, 0x52, 0x4d, 0x49,
  70             0x4e, 0x41, 0x54, 0x45, 0x44, (byte)0xff, (byte)0xff, (byte)0xff,
  71             (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff
  72         };
  73 
  74         try {
  75           // Should fail, but instead creates corrupt EnumSet
  76           @SuppressWarnings("unchecked")
  77           EnumSet<Thread.State> es = (EnumSet<Thread.State>)
  78               deserialize(serializedForm);
  79 
  80           // Demonstrates corruption
  81           System.out.println("Enum size: " + Thread.State.values().length); // 6
  82           System.out.println("Set size: " + es.size()); // 64
  83           System.out.println("Set: " + es); // Throws IndexOutOfBoundsException
  84           throw new AssertionError("Expected exception InvalidObjectException not thrown");
  85         } catch (java.io.InvalidObjectException ignore) { /* OK */ }
  86     }
  87 
  88     private static Object deserialize(byte[] sf) throws Throwable {
  89       return new ObjectInputStream(
  90           new ByteArrayInputStream(sf))
  91           .readObject();
  92     }
  93 }
< prev index next >