Skip to content

Commit 19e818b

Browse files
author
Alexander Zuev
committed
8350813: Rendering of bulky sound bank from MIDI sequence can cause OutOfMemoryError
Reviewed-by: honkar, prr
1 parent 361f8c0 commit 19e818b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2025, 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
@@ -90,6 +90,13 @@ public Soundbank getSoundbank(AudioInputStream ais)
9090
"Can not allocate enough memory to read audio data.");
9191
}
9292

93+
long maximumHeapSize = (long) ((Runtime.getRuntime().maxMemory() -
94+
(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())) * 0.9);
95+
if (totalSize > maximumHeapSize) {
96+
throw new InvalidMidiDataException(
97+
"Insufficient heap size to render audio data.");
98+
}
99+
93100
if (ais.getFrameLength() == -1 || totalSize > MEGABYTE) {
94101
ByteArrayOutputStream baos = new ByteArrayOutputStream();
95102
byte[] buff = new byte[DEFAULT_BUFFER_SIZE - (DEFAULT_BUFFER_SIZE % frameSize)];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2025, 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.InvalidMidiDataException;
25+
import javax.sound.midi.MidiSystem;
26+
import java.io.ByteArrayInputStream;
27+
import java.io.IOException;
28+
29+
/**
30+
* @test
31+
* @bug 8350813
32+
* @summary Rendering of bulky sound bank from MIDI sequence can cause OutOfMemoryError.
33+
* @run main/othervm -Xmx1g BulkSoundBank
34+
*/
35+
36+
public class BulkSoundBank {
37+
static final byte[] midi = {77, 84, 104, 100, 0, 0, 0, 6, 0, 0, 0, 1, 1,
38+
-32, 77, 84, 114, 107, 0, 0, 0, 50, 0, -1, 88, 4, 4, 2, 24, 8, 0, -1,
39+
81, 3, 7, -95, 32, 0, -112, 60, 64, -125, 96, -128, 60, 64, -125, -44,
40+
-51, 32, -112, 48, 64, 1, -128, 48, 64, -127, -64, -45, 127, -112, 60,
41+
64, 1, -128, 60, 64, 0, -1, 47, 0};
42+
43+
public static void main(String[] args) throws IOException {
44+
try (ByteArrayInputStream bis = new ByteArrayInputStream(midi)) {
45+
MidiSystem.getSoundbank(bis);
46+
throw new RuntimeException("Test should throw InvalidMidiDataException"
47+
+ " but it did not.");
48+
} catch (InvalidMidiDataException imda) {
49+
System.out.println("Caught InvalidMidiDataException as expected");
50+
}
51+
}
52+
}
53+

0 commit comments

Comments
 (0)