Skip to content

Commit 5de99da

Browse files
committed
8237495: Java MIDI fails with a dereferenced memory error when asked to send a raw 0xF7
Reviewed-by: kizune
1 parent be63525 commit 5de99da

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

src/java.desktop/share/native/libjsound/MidiOutDevice.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -133,7 +133,7 @@ Java_com_sun_media_sound_MidiOutDevice_nSendLongMessage(JNIEnv* e, jobject thisO
133133
}
134134
/* "continuation" sysex messages start with F7 (instead of F0), but
135135
are sent without the F7. */
136-
if (data[0] == 0xF7) {
136+
if (data[0] == 0xF7 && size > 1) {
137137
data++;
138138
size--;
139139
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import javax.sound.midi.MidiDevice;
25+
import javax.sound.midi.MidiMessage;
26+
import javax.sound.midi.MidiSystem;
27+
import javax.sound.midi.MidiUnavailableException;
28+
import javax.sound.midi.Receiver;
29+
import javax.sound.midi.ShortMessage;
30+
import javax.sound.midi.SysexMessage;
31+
32+
import static javax.sound.midi.SysexMessage.SPECIAL_SYSTEM_EXCLUSIVE;
33+
import static javax.sound.midi.SysexMessage.SYSTEM_EXCLUSIVE;
34+
35+
/**
36+
* @test
37+
* @bug 8237495
38+
* @summary fail with a dereferenced memory error when asked to send a raw 0xF7
39+
*/
40+
public final class SendRawSysexMessage {
41+
42+
private static final class RawMidiMessage extends MidiMessage {
43+
@Override
44+
public RawMidiMessage clone() {
45+
return new RawMidiMessage(getMessage());
46+
}
47+
@Override
48+
public int getStatus() {
49+
return SYSTEM_EXCLUSIVE; // not that this really matters
50+
}
51+
52+
RawMidiMessage(byte[] data) {
53+
super(data.clone());
54+
}
55+
}
56+
57+
public static void main(String[] args) throws Exception {
58+
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
59+
System.err.println("List of devices to test:");
60+
for (int i = 0; i < infos.length; i++) {
61+
System.err.printf("\t%d.\t%s%n", i, infos[i]);
62+
}
63+
for (int i = 0; i < infos.length; i++) {
64+
System.err.printf("%d.\t%s%n", i, infos[i]);
65+
try {
66+
test(infos[i]);
67+
} catch (MidiUnavailableException ignored){
68+
// try next
69+
}
70+
}
71+
}
72+
73+
private static void test(MidiDevice.Info info) throws Exception {
74+
try (MidiDevice device = MidiSystem.getMidiDevice(info)) {
75+
System.err.println("Sending to " + device + " (" + info + ")");
76+
if (!device.isOpen())
77+
device.open();
78+
try (Receiver r = device.getReceiver()) {
79+
System.err.println("note on");
80+
r.send(new ShortMessage(ShortMessage.NOTE_ON, 5, 5), -1);
81+
System.err.println("sysex");
82+
r.send(new SysexMessage(new byte[]{
83+
(byte) SYSTEM_EXCLUSIVE, 0x0, 0x03, 0x04,
84+
(byte) SPECIAL_SYSTEM_EXCLUSIVE}, 5), -1);
85+
System.err.println("raw 1");
86+
r.send(new RawMidiMessage(new byte[]{
87+
(byte) 0x02, 0x02, 0x03, 0x04}), -1);
88+
System.err.println("raw 2");
89+
r.send(new RawMidiMessage(new byte[]{
90+
(byte) 0x09, 0x02, 0x03, 0x04}), -1);
91+
System.err.println("raw 3");
92+
r.send(new RawMidiMessage(new byte[]{
93+
(byte) SYSTEM_EXCLUSIVE, 0x02, 0x03, 0x04}), -1);
94+
System.err.println("raw 4");
95+
r.send(new RawMidiMessage(new byte[]{
96+
(byte) 0x02, 0x02, 0x03, 0x04}), -1);
97+
System.err.println("raw 5");
98+
r.send(new RawMidiMessage(new byte[]{
99+
(byte) 0x02, 0x02, 0x03,
100+
(byte) SPECIAL_SYSTEM_EXCLUSIVE}), -1);
101+
System.err.println("raw 6");
102+
r.send(new RawMidiMessage(new byte[]{
103+
(byte) SYSTEM_EXCLUSIVE, 0x02, 0x03, 0x04}), -1);
104+
System.err.println("sleep");
105+
Thread.sleep(1000);
106+
System.err.println("raw 7");
107+
r.send(new RawMidiMessage(new byte[]{
108+
(byte) 0x02, 0x02, 0x03, 0x04}), -1);
109+
System.err.println("sleep");
110+
Thread.sleep(1000);
111+
System.err.println("raw 8");
112+
r.send(new RawMidiMessage(new byte[]{
113+
(byte) SPECIAL_SYSTEM_EXCLUSIVE}), -1);
114+
System.err.println("note off");
115+
r.send(new ShortMessage(ShortMessage.NOTE_OFF, 5, 5), -1);
116+
System.err.println("done, should quit");
117+
System.err.println();
118+
}
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)