8221445: FastSysexMessage constructor crashes MIDI receiption thread
authorserb
Tue, 09 Apr 2019 13:57:58 -0700
changeset 54506 8b3b89320d03
parent 54505 7f53d59593e2
child 54507 a67b9214cfab
8221445: FastSysexMessage constructor crashes MIDI receiption thread Reviewed-by: prr
src/java.desktop/share/classes/com/sun/media/sound/FastSysexMessage.java
src/java.desktop/share/classes/com/sun/media/sound/MidiUtils.java
src/java.desktop/share/classes/javax/sound/midi/SysexMessage.java
test/jdk/javax/sound/midi/SysexMessage/Basic.java
test/jdk/javax/sound/midi/SysexMessage/Exceptions.java
--- a/src/java.desktop/share/classes/com/sun/media/sound/FastSysexMessage.java	Tue Apr 09 08:50:08 2019 +0100
+++ b/src/java.desktop/share/classes/com/sun/media/sound/FastSysexMessage.java	Tue Apr 09 13:57:58 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 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
@@ -37,9 +37,7 @@
 
     FastSysexMessage(byte[] data) throws InvalidMidiDataException {
         super(data);
-        if (data.length==0 || (((data[0] & 0xFF) != 0xF0) && ((data[0] & 0xFF) != 0xF7))) {
-            super.setMessage(data, data.length); // will throw Exception
-        }
+        MidiUtils.checkSysexStatus(data, data.length);
     }
 
     /**
@@ -54,9 +52,7 @@
     // which is shared among all transmitters, cannot be modified
     @Override
     public void setMessage(byte[] data, int length) throws InvalidMidiDataException {
-        if ((data.length == 0) || (((data[0] & 0xFF) != 0xF0) && ((data[0] & 0xFF) != 0xF7))) {
-            super.setMessage(data, data.length); // will throw Exception
-        }
+        MidiUtils.checkSysexStatus(data, length);
         this.length = length;
         this.data = new byte[this.length];
         System.arraycopy(data, 0, this.data, 0, length);
--- a/src/java.desktop/share/classes/com/sun/media/sound/MidiUtils.java	Tue Apr 09 08:50:08 2019 +0100
+++ b/src/java.desktop/share/classes/com/sun/media/sound/MidiUtils.java	Tue Apr 09 13:57:58 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 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
@@ -27,6 +27,7 @@
 
 import java.util.ArrayList;
 
+import javax.sound.midi.InvalidMidiDataException;
 import javax.sound.midi.MetaMessage;
 import javax.sound.midi.MidiDevice;
 import javax.sound.midi.MidiEvent;
@@ -34,6 +35,9 @@
 import javax.sound.midi.Sequence;
 import javax.sound.midi.Track;
 
+import static javax.sound.midi.SysexMessage.SPECIAL_SYSTEM_EXCLUSIVE;
+import static javax.sound.midi.SysexMessage.SYSTEM_EXCLUSIVE;
+
 // TODO:
 // - define and use a global symbolic constant for 60000000 (see convertTempo)
 
@@ -65,6 +69,37 @@
                 "MidiDevice %s not supported by this provider", info));
     }
 
+    /**
+     * Checks the status byte for the system exclusive message.
+     *
+     * @param  data the system exclusive message data
+     * @param  length the length of the valid message data in the array
+     * @throws InvalidMidiDataException if the status byte is invalid for a
+     *         system exclusive message
+     */
+    public static void checkSysexStatus(final byte[] data, final int length)
+            throws InvalidMidiDataException {
+        if (data.length == 0 || length == 0) {
+            throw new InvalidMidiDataException("Status byte is missing");
+        }
+        checkSysexStatus(data[0] & 0xFF);
+    }
+
+    /**
+     * Checks the status byte for the system exclusive message.
+     *
+     * @param  status the status byte for the message (0xF0 or 0xF7)
+     * @throws InvalidMidiDataException if the status byte is invalid for a
+     *         system exclusive message
+     */
+    public static void checkSysexStatus(final int status)
+            throws InvalidMidiDataException {
+        if (status != SYSTEM_EXCLUSIVE && status != SPECIAL_SYSTEM_EXCLUSIVE) {
+            throw new InvalidMidiDataException(String.format(
+                    "Invalid status byte for sysex message: 0x%X", status));
+        }
+    }
+
     /** return true if the passed message is Meta End Of Track */
     public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
         // first check if it is a META message at all
--- a/src/java.desktop/share/classes/javax/sound/midi/SysexMessage.java	Tue Apr 09 08:50:08 2019 +0100
+++ b/src/java.desktop/share/classes/javax/sound/midi/SysexMessage.java	Tue Apr 09 13:57:58 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 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
@@ -25,6 +25,8 @@
 
 package javax.sound.midi;
 
+import com.sun.media.sound.MidiUtils;
+
 /**
  * A {@code SysexMessage} object represents a MIDI system exclusive message.
  * <p>
@@ -183,10 +185,7 @@
      */
     @Override
     public void setMessage(byte[] data, int length) throws InvalidMidiDataException {
-        int status = (data[0] & 0xFF);
-        if ((status != 0xF0) && (status != 0xF7)) {
-            throw new InvalidMidiDataException("Invalid status byte for sysex message: 0x" + Integer.toHexString(status));
-        }
+        MidiUtils.checkSysexStatus(data, length);
         super.setMessage(data, length);
     }
 
@@ -200,9 +199,7 @@
      *         system exclusive message
      */
     public void setMessage(int status, byte[] data, int length) throws InvalidMidiDataException {
-        if ( (status != 0xF0) && (status != 0xF7) ) {
-            throw new InvalidMidiDataException("Invalid status byte for sysex message: 0x" + Integer.toHexString(status));
-        }
+        MidiUtils.checkSysexStatus(status);
         if (length < 0 || length > data.length) {
             throw new IndexOutOfBoundsException("length out of bounds: "+length);
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/sound/midi/SysexMessage/Basic.java	Tue Apr 09 13:57:58 2019 -0700
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.util.Arrays;
+
+import javax.sound.midi.SysexMessage;
+
+import static javax.sound.midi.SysexMessage.SPECIAL_SYSTEM_EXCLUSIVE;
+import static javax.sound.midi.SysexMessage.SYSTEM_EXCLUSIVE;
+
+/**
+ * @test
+ * @bug 8221445
+ * @summary Checks basic functionality of javax.sound.midi.SysexMessage class
+ */
+public class Basic {
+
+    public static void main(final String[] args) throws Exception {
+        byte[] dataExclusive = {(byte) (SYSTEM_EXCLUSIVE)};
+        byte[] dataSpecialExclusive = {(byte) (SPECIAL_SYSTEM_EXCLUSIVE)};
+        byte[] empty = {};
+
+        ////////////////////////////
+        // Constructors
+        ////////////////////////////
+        SysexMessage msg = new SysexMessage(dataExclusive, 1);
+        test(msg, SYSTEM_EXCLUSIVE, empty, 1);
+        msg = new SysexMessage(dataSpecialExclusive, 1);
+        test(msg, SPECIAL_SYSTEM_EXCLUSIVE, empty, 1);
+        msg = new SysexMessage(SYSTEM_EXCLUSIVE, empty, 0);
+        test(msg, SYSTEM_EXCLUSIVE, empty, 1);
+        msg = new SysexMessage(SPECIAL_SYSTEM_EXCLUSIVE, empty, 0);
+        test(msg, SPECIAL_SYSTEM_EXCLUSIVE, empty, 1);
+        msg = new SysexMessage(SYSTEM_EXCLUSIVE, dataSpecialExclusive, 1);
+        test(msg, SYSTEM_EXCLUSIVE, dataSpecialExclusive, 2);
+        msg = new SysexMessage(SPECIAL_SYSTEM_EXCLUSIVE, dataExclusive, 1);
+        test(msg, SPECIAL_SYSTEM_EXCLUSIVE, dataExclusive, 2);
+
+        ////////////////////////////
+        // SysexMessage.setMessage()
+        ////////////////////////////
+        msg = new SysexMessage();
+        msg.setMessage(dataExclusive, 1);
+        test(msg, SYSTEM_EXCLUSIVE, empty, 1);
+        msg = new SysexMessage();
+        msg.setMessage(dataSpecialExclusive, 1);
+        test(msg, SPECIAL_SYSTEM_EXCLUSIVE, empty, 1);
+        msg = new SysexMessage();
+        msg.setMessage(SYSTEM_EXCLUSIVE, empty, 0);
+        test(msg, SYSTEM_EXCLUSIVE, empty, 1);
+        msg = new SysexMessage();
+        msg.setMessage(SPECIAL_SYSTEM_EXCLUSIVE, empty, 0);
+        test(msg, SPECIAL_SYSTEM_EXCLUSIVE, empty, 1);
+        msg = new SysexMessage();
+        msg.setMessage(SYSTEM_EXCLUSIVE, dataSpecialExclusive, 1);
+        test(msg, SYSTEM_EXCLUSIVE, dataSpecialExclusive, 2);
+        msg = new SysexMessage();
+        msg.setMessage(SPECIAL_SYSTEM_EXCLUSIVE, dataExclusive, 1);
+        test(msg, SPECIAL_SYSTEM_EXCLUSIVE, dataExclusive, 2);
+    }
+
+    static void test(SysexMessage msg, int status, byte[] data, int length) {
+        if (msg.getStatus() != status) {
+            System.err.println("Expected status: " + status);
+            System.err.println("Actual status: " + msg.getStatus());
+            throw new RuntimeException();
+        }
+        if (msg.getLength() != length) {
+            System.err.println("Expected length: " + length);
+            System.err.println("Actual length: " + msg.getLength());
+            throw new RuntimeException();
+        }
+        if (!Arrays.equals(msg.getData(), data)) {
+            System.err.println("Expected data: " + Arrays.toString(data));
+            System.err.println("Actual data: " + Arrays.toString(msg.getData()));
+            throw new RuntimeException();
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/sound/midi/SysexMessage/Exceptions.java	Tue Apr 09 13:57:58 2019 -0700
@@ -0,0 +1,179 @@
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import javax.sound.midi.InvalidMidiDataException;
+import javax.sound.midi.SysexMessage;
+
+import static javax.sound.midi.SysexMessage.SYSTEM_EXCLUSIVE;
+
+/**
+ * @test
+ * @bug 8221445
+ * @summary Checks exceptions thrown by javax.sound.midi.SysexMessage class
+ */
+public final class Exceptions {
+
+    public static void main(final String[] args) throws Exception {
+        testInvalidMidiDataException();
+        testIndexOutOfBoundsException();
+        testNullPointerException();
+    }
+
+    private static void testInvalidMidiDataException() {
+        try {
+            // data should conatins a status byte
+            new SysexMessage(new byte[0], 0);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final InvalidMidiDataException ignored) {
+            // ok
+        }
+        try {
+            // length is zero, no space for the status byte
+            new SysexMessage(new byte[]{(byte) (SYSTEM_EXCLUSIVE)}, 0);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final InvalidMidiDataException ignored) {
+            // ok
+        }
+        try {
+            // status should conatins a status byte (0xF0 or 0xF7)
+            new SysexMessage(0, new byte[0], 2);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final InvalidMidiDataException ignored) {
+            // ok
+        }
+        SysexMessage sysexMessage = new SysexMessage();
+        try {
+            // data should conatins a status byte
+            sysexMessage.setMessage(new byte[0], 0);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final InvalidMidiDataException ignored) {
+            // ok
+        }
+        try {
+            // length is zero, no space for the status byte
+            sysexMessage.setMessage(new byte[]{(byte) (SYSTEM_EXCLUSIVE)}, 0);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final InvalidMidiDataException ignored) {
+            // ok
+        }
+        try {
+            // data should conatins a status byte (0xF0 or 0xF7)
+            sysexMessage.setMessage(new byte[]{0}, 0);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final InvalidMidiDataException ignored) {
+            // ok
+        }
+        try {
+            // status should conatins a status byte (0xF0 or 0xF7)
+            sysexMessage.setMessage(0, new byte[0], 0);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final InvalidMidiDataException ignored) {
+            // ok
+        }
+    }
+
+    private static void testIndexOutOfBoundsException() throws Exception {
+        // length is bigger than data
+        try {
+            new SysexMessage(new byte[]{(byte) (0xF0 & 0xFF)}, 2);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final IndexOutOfBoundsException ignored) {
+            // ok
+        }
+        try {
+            new SysexMessage(0xF0, new byte[0], 2);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final IndexOutOfBoundsException ignored) {
+            // ok
+        }
+        SysexMessage sysexMessage = new SysexMessage();
+        try {
+            sysexMessage.setMessage(new byte[]{(byte) (0xF0 & 0xFF)}, 2);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final IndexOutOfBoundsException ignored) {
+            // ok
+        }
+        try {
+            sysexMessage.setMessage(0xF0, new byte[0], 2);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final IndexOutOfBoundsException ignored) {
+            // ok
+        }
+
+        // length is negative
+        try {
+            new SysexMessage(new byte[]{(byte) (0xF0 & 0xFF)}, -1);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final IndexOutOfBoundsException ignored) {
+            // ok
+        }
+        try {
+            new SysexMessage(0xF0, new byte[0], -1);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final IndexOutOfBoundsException ignored) {
+            // ok
+        }
+        sysexMessage = new SysexMessage();
+        try {
+            sysexMessage.setMessage(new byte[]{(byte) (0xF0 & 0xFF)}, -1);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final IndexOutOfBoundsException ignored) {
+            // ok
+        }
+        try {
+            sysexMessage.setMessage(0xF0, new byte[0], -1);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final IndexOutOfBoundsException ignored) {
+            // ok
+        }
+    }
+
+    private static void testNullPointerException() throws Exception {
+        try {
+            new SysexMessage(null, 0);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final NullPointerException ignored) {
+            // ok
+        }
+        try {
+            new SysexMessage(SYSTEM_EXCLUSIVE, null, 2);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final NullPointerException ignored) {
+            // ok
+        }
+        SysexMessage sysexMessage = new SysexMessage();
+        try {
+            sysexMessage.setMessage(null, 0);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final NullPointerException ignored) {
+            // ok
+        }
+        sysexMessage = new SysexMessage();
+        try {
+            sysexMessage.setMessage(SYSTEM_EXCLUSIVE, null, 2);
+            throw new RuntimeException("Expected exception is not thrown");
+        } catch (final NullPointerException ignored) {
+            // ok
+        }
+    }
+}