8004261: Improve input validation
authorserb
Fri, 15 Feb 2013 13:49:38 +0400
changeset 16876 17f574546dc8
parent 16875 a29830a7e5b3
child 16877 d12c06e6e2ba
8004261: Improve input validation Reviewed-by: art, mschoene, amenkov
jdk/src/share/classes/com/sun/media/sound/AbstractMidiDevice.java
jdk/src/share/classes/com/sun/media/sound/FastShortMessage.java
jdk/src/share/classes/com/sun/media/sound/FastSysexMessage.java
jdk/src/share/classes/com/sun/media/sound/MidiOutDevice.java
jdk/src/share/classes/com/sun/media/sound/RealTimeSequencer.java
--- a/jdk/src/share/classes/com/sun/media/sound/AbstractMidiDevice.java	Thu Dec 20 13:40:27 2012 +0000
+++ b/jdk/src/share/classes/com/sun/media/sound/AbstractMidiDevice.java	Fri Feb 15 13:49:38 2013 +0400
@@ -56,7 +56,7 @@
     // from simultaneous creation and destruction
     // reduces possibility of deadlock, compared to
     // synchronizing to the class instance
-    private Object traRecLock = new Object();
+    private final Object traRecLock = new Object();
 
     // DEVICE ATTRIBUTES
 
@@ -474,7 +474,7 @@
         This is necessary for Receivers retrieved via MidiSystem.getReceiver()
         (which opens the device implicitely).
      */
-    protected abstract class AbstractReceiver implements MidiDeviceReceiver {
+    abstract class AbstractReceiver implements MidiDeviceReceiver {
         private boolean open = true;
 
 
@@ -483,24 +483,24 @@
             Receiver. Therefore, subclasses should not override this method.
             Instead, they should implement implSend().
         */
-        public synchronized void send(MidiMessage message, long timeStamp) {
-            if (open) {
-                implSend(message, timeStamp);
-            } else {
+        @Override
+        public final synchronized void send(final MidiMessage message,
+                                            final long timeStamp) {
+            if (!open) {
                 throw new IllegalStateException("Receiver is not open");
             }
+            implSend(message, timeStamp);
         }
 
-
-        protected abstract void implSend(MidiMessage message, long timeStamp);
-
+        abstract void implSend(MidiMessage message, long timeStamp);
 
         /** Close the Receiver.
          * Here, the call to the magic method closeInternal() takes place.
          * Therefore, subclasses that override this method must call
          * 'super.close()'.
          */
-        public void close() {
+        @Override
+        public final void close() {
             open = false;
             synchronized (AbstractMidiDevice.this.traRecLock) {
                 AbstractMidiDevice.this.getReceiverList().remove(this);
@@ -508,11 +508,12 @@
             AbstractMidiDevice.this.closeInternal(this);
         }
 
-        public MidiDevice getMidiDevice() {
+        @Override
+        public final MidiDevice getMidiDevice() {
             return AbstractMidiDevice.this;
         }
 
-        protected boolean isOpen() {
+        final boolean isOpen() {
             return open;
         }
 
--- a/jdk/src/share/classes/com/sun/media/sound/FastShortMessage.java	Thu Dec 20 13:40:27 2012 +0000
+++ b/jdk/src/share/classes/com/sun/media/sound/FastShortMessage.java	Fri Feb 15 13:49:38 2013 +0400
@@ -32,7 +32,7 @@
  *
  * @author Florian Bomers
  */
-class FastShortMessage extends ShortMessage {
+final class FastShortMessage extends ShortMessage {
     private int packedMsg;
 
     public FastShortMessage(int packedMsg) throws InvalidMidiDataException {
--- a/jdk/src/share/classes/com/sun/media/sound/FastSysexMessage.java	Thu Dec 20 13:40:27 2012 +0000
+++ b/jdk/src/share/classes/com/sun/media/sound/FastSysexMessage.java	Fri Feb 15 13:49:38 2013 +0400
@@ -32,7 +32,7 @@
  *
  * @author Florian Bomers
  */
-class FastSysexMessage extends SysexMessage {
+final class FastSysexMessage extends SysexMessage {
 
     FastSysexMessage(byte[] data) throws InvalidMidiDataException {
         super(data);
--- a/jdk/src/share/classes/com/sun/media/sound/MidiOutDevice.java	Thu Dec 20 13:40:27 2012 +0000
+++ b/jdk/src/share/classes/com/sun/media/sound/MidiOutDevice.java	Fri Feb 15 13:49:38 2013 +0400
@@ -103,9 +103,9 @@
 
     class MidiOutReceiver extends AbstractReceiver {
 
-        protected void implSend(MidiMessage message, long timeStamp) {
-            int length = message.getLength();
-            int status = message.getStatus();
+        void implSend(final MidiMessage message, final long timeStamp) {
+            final int length = message.getLength();
+            final int status = message.getStatus();
             if (length <= 3 && status != 0xF0 && status != 0xF7) {
                 int packedMsg;
                 if (message instanceof ShortMessage) {
@@ -140,11 +140,15 @@
                 }
                 nSendShortMessage(id, packedMsg, timeStamp);
             } else {
+                final byte[] data;
                 if (message instanceof FastSysexMessage) {
-                    nSendLongMessage(id, ((FastSysexMessage) message).getReadOnlyMessage(),
-                                     length, timeStamp);
+                    data = ((FastSysexMessage) message).getReadOnlyMessage();
                 } else {
-                    nSendLongMessage(id, message.getMessage(), length, timeStamp);
+                    data = message.getMessage();
+                }
+                final int dataLength = Math.min(length, data.length);
+                if (dataLength > 0) {
+                    nSendLongMessage(id, data, dataLength, timeStamp);
                 }
             }
         }
--- a/jdk/src/share/classes/com/sun/media/sound/RealTimeSequencer.java	Thu Dec 20 13:40:27 2012 +0000
+++ b/jdk/src/share/classes/com/sun/media/sound/RealTimeSequencer.java	Fri Feb 15 13:49:38 2013 +0400
@@ -1026,7 +1026,7 @@
 
     class SequencerReceiver extends AbstractReceiver {
 
-        protected void implSend(MidiMessage message, long timeStamp) {
+        void implSend(MidiMessage message, long timeStamp) {
             if (recording) {
                 long tickPos = 0;