Skip to content

Commit f94b2d6

Browse files
author
Satyen Subramaniam
committed
8319598: SMFParser misinterprets interrupted running status
Backport-of: 62d5d1e99c118b6ed26e79a2f7247308f8c23310
1 parent faa45e8 commit f94b2d6

File tree

2 files changed

+154
-3
lines changed

2 files changed

+154
-3
lines changed

src/java.desktop/share/classes/com/sun/media/sound/StandardMidiFileReader.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,10 @@ void readTrack(Track track) throws IOException, InvalidMidiDataException {
313313
// reset current tick to 0
314314
long tick = 0;
315315

316-
// reset current status byte to 0 (invalid value).
316+
// reset current running status byte to 0 (invalid value).
317317
// this should cause us to throw an InvalidMidiDataException if we don't
318318
// get a valid status byte from the beginning of the track.
319-
int status = 0;
319+
int runningStatus = 0;
320320
boolean endOfTrackFound = false;
321321

322322
while (!trackFinished() && !endOfTrackFound) {
@@ -333,10 +333,17 @@ void readTrack(Track track) throws IOException, InvalidMidiDataException {
333333
// check for new status
334334
int byteValue = readUnsigned();
335335

336+
int status;
336337
if (byteValue >= 0x80) {
337338
status = byteValue;
339+
340+
// update running status (only for channel messages)
341+
if ((status & 0xF0) != 0xF0) {
342+
runningStatus = status;
343+
}
338344
} else {
339-
data1 = byteValue;
345+
status = runningStatus;
346+
data1 = byteValue;
340347
}
341348

342349
switch (status & 0xF0) {
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2023, 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 java.io.ByteArrayInputStream;
25+
26+
import javax.sound.midi.MidiSystem;
27+
import javax.sound.midi.Sequence;
28+
import javax.sound.midi.Track;
29+
30+
/**
31+
* @test
32+
* @bug 8319598
33+
* @summary SMFParser bug with running status, interrupted by Meta or SysEx messages
34+
*/
35+
public class SMFInterruptedRunningStatus {
36+
37+
public static void main(String[] args) throws Exception {
38+
39+
byte[][] files = new byte[][] {SMF_1, SMF_2, SMF_3};
40+
for (int i = 0; i < files.length; i++) {
41+
Sequence seq = MidiSystem.getSequence(
42+
new ByteArrayInputStream(files[i]));
43+
testSequence(seq, i + 1);
44+
}
45+
46+
// no exception thrown, all files have been parsed correctly
47+
System.out.println("Test passed");
48+
}
49+
50+
private static void testSequence(Sequence seq, int fileNumber) {
51+
52+
// check number of tracks and number of events
53+
Track[] tracks = seq.getTracks();
54+
if (1 != tracks.length) {
55+
throw new RuntimeException("file number "
56+
+ fileNumber + " fails (incorrect number of tracks: "
57+
+ tracks.length + ")");
58+
}
59+
Track track = tracks[0];
60+
if (7 != track.size()) {
61+
throw new RuntimeException("file number " + fileNumber
62+
+ " fails (incorrect number of events: "
63+
+ track.size() + ")");
64+
}
65+
66+
// check status byte of each message
67+
int[] expectedStatusBytes = new int[] {
68+
0x90, 0xFF, 0x90, 0x90, 0x90, 0xFF, 0xFF};
69+
for (int i = 0; i < expectedStatusBytes.length; i++) {
70+
int expected = expectedStatusBytes[i];
71+
if (expected != track.get(i).getMessage().getStatus()) {
72+
throw new RuntimeException("file number " + fileNumber
73+
+ " fails (wrong status byte in event " + i + ")");
74+
}
75+
}
76+
}
77+
78+
// MIDI file without running status - should work equally before
79+
// and after the bugfix
80+
private static final byte[] SMF_1 = {
81+
0x4D, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06, // file header (start)
82+
0x00, 0x01, 0x00, 0x01, 0x00, (byte) 0x80, // file header (end)
83+
0x4D, 0x54, 0x72, 0x6B, 0x00, 0x00, 0x00, 0x24, // track header
84+
0x00, // delta time
85+
(byte) 0x90, 0x3C, 0x7F, // Note-ON (C)
86+
0x40, // delta time
87+
(byte) 0xFF, 0x01, 0x04, 0x54, 0x65, 0x73, 0x74, // META (text)
88+
0x20, // delta time
89+
(byte) 0x90, 0x3C, 0x00, // Note-OFF (C)
90+
0x20, // delta time
91+
(byte) 0x90, 0x3E, 0x7F, // Note-ON (D)
92+
0x60, // delta time
93+
(byte) 0x90, 0x3E, 0x00, // Note-OFF (D)
94+
0x20, // delta time
95+
(byte) 0xFF, 0x01, 0x04, 0x54, 0x65, 0x73, 0x74, // META (text)
96+
0x00, // delta time
97+
(byte) 0xFF, 0x2F, 0x00 // META (end of track)
98+
};
99+
100+
// MIDI file with running status, interrupted by a META message
101+
// - failed before the bugfix
102+
private static final byte[] SMF_2 = {
103+
0x4D, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06, // file header (start)
104+
0x00, 0x01, 0x00, 0x01, 0x00, (byte) 0x80, // file header (end)
105+
0x4D, 0x54, 0x72, 0x6B, 0x00, 0x00, 0x00, 0x21, // track header
106+
0x00, // delta time
107+
(byte) 0x90, 0x3C, 0x7F, // Note-ON (C)
108+
0x40, // delta time
109+
(byte) 0xFF, 0x01, 0x04, 0x54, 0x65, 0x73, 0x74, // META (interruptor)
110+
0x20, // delta time
111+
0x3C, 0x00, // Note-OFF (C) - running status
112+
0x20, // delta time
113+
0x3E, 0x7F, // Note-ON (D) - running status
114+
0x60, // delta time
115+
0x3E, 0x00, // Note-OFF (D) - running status
116+
0x20, // delta time
117+
(byte) 0xFF, 0x01, 0x04, 0x54, 0x65, 0x73, 0x74, // META (text)
118+
0x00, // delta time
119+
(byte) 0xFF, 0x2F, 0x00 // META (end of track)
120+
};
121+
122+
// MIDI file with running status, interrupted by a META message
123+
// - succeeded before the bugfix but with wrong interpretation of the data
124+
private static final byte[] SMF_3 = {
125+
0x4D, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06, // file header (start)
126+
0x00, 0x01, 0x00, 0x01, 0x00, (byte) 0x80, // file header (end)
127+
0x4D, 0x54, 0x72, 0x6B, 0x00, 0x00, 0x00, 0x21, // track header
128+
0x00, // delta time
129+
(byte) 0x90, 0x3C, 0x7F, // Note-ON (C)
130+
0x40, // delta time
131+
(byte) 0xFF, 0x01, 0x04, 0x54, 0x65, 0x73, 0x74, // META (interruptor)
132+
0x20, // delta time
133+
0x3C, 0x00, // Note-OFF (C) - running status
134+
0x0D, // delta time
135+
0x3E, 0x7F, // Note-ON (D) - running status
136+
0x60, // delta time
137+
0x3E, 0x00, // Note-OFF (D) - running status
138+
0x20, // delta time
139+
(byte) 0xFF, 0x01, 0x04, 0x54, 0x65, 0x73, 0x74, // META (text)
140+
0x00, // delta time
141+
(byte) 0xFF, 0x2F, 0x00 // META (end of track)
142+
};
143+
}
144+

0 commit comments

Comments
 (0)