diff --git a/jdk/src/share/classes/com/sun/media/sound/DirectAudioDevice.java b/jdk/src/share/classes/com/sun/media/sound/DirectAudioDevice.java index c9d57c6b65c..7b5e5339e36 100644 --- a/jdk/src/share/classes/com/sun/media/sound/DirectAudioDevice.java +++ b/jdk/src/share/classes/com/sun/media/sound/DirectAudioDevice.java @@ -736,7 +736,7 @@ class DirectAudioDevice extends AbstractMixer { if (off < 0) { throw new ArrayIndexOutOfBoundsException(off); } - if (off + len > b.length) { + if ((long)off + (long)len > (long)b.length) { throw new ArrayIndexOutOfBoundsException(b.length); } @@ -964,7 +964,7 @@ class DirectAudioDevice extends AbstractMixer { if (off < 0) { throw new ArrayIndexOutOfBoundsException(off); } - if (off + len > b.length) { + if ((long)off + (long)len > (long)b.length) { throw new ArrayIndexOutOfBoundsException(b.length); } if (!isActive() && doIO) { diff --git a/jdk/src/share/classes/com/sun/media/sound/SoftMixingSourceDataLine.java b/jdk/src/share/classes/com/sun/media/sound/SoftMixingSourceDataLine.java index 9912e08d975..2f7cdbaa389 100644 --- a/jdk/src/share/classes/com/sun/media/sound/SoftMixingSourceDataLine.java +++ b/jdk/src/share/classes/com/sun/media/sound/SoftMixingSourceDataLine.java @@ -130,6 +130,12 @@ public class SoftMixingSourceDataLine extends SoftMixingDataLine implements if (len % framesize != 0) throw new IllegalArgumentException( "Number of bytes does not represent an integral number of sample frames."); + if (off < 0) { + throw new ArrayIndexOutOfBoundsException(off); + } + if ((long)off + (long)len > (long)b.length) { + throw new ArrayIndexOutOfBoundsException(b.length); + } byte[] buff = cycling_buffer; int buff_len = cycling_buffer.length; diff --git a/jdk/test/javax/sound/sampled/DataLine/DataLine_ArrayIndexOutOfBounds.java b/jdk/test/javax/sound/sampled/DataLine/DataLine_ArrayIndexOutOfBounds.java new file mode 100644 index 00000000000..e8dac311ee1 --- /dev/null +++ b/jdk/test/javax/sound/sampled/DataLine/DataLine_ArrayIndexOutOfBounds.java @@ -0,0 +1,226 @@ +/** + * @test + * @bug 7088367 + * @summary SourceDataLine.write and TargetDataLine.read don't throw ArrayIndexOutOfBoundsException + * @author Alex Menkov + */ + +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.Line; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.Mixer; +import javax.sound.sampled.SourceDataLine; +import javax.sound.sampled.TargetDataLine; + +public class DataLine_ArrayIndexOutOfBounds { + + static int total = 0; + static int failed = 0; + + // shared buffer for all tests + static final byte[] buffer = new byte[5000000]; + + // the class describes different test scenarios (buffer properties) + static abstract class Scenario { + abstract int getBufferOffset(DataLine line); + abstract int getBufferLength(DataLine line); + } + + // scenarios to tests + static Scenario[] scenarios = new Scenario[]{ + new Scenario() { + public String toString() { + return "offset is near Integer.MAX_VALUE"; + } + public int getBufferOffset(DataLine line) { + return Integer.MAX_VALUE - 4096; + } + public int getBufferLength(DataLine line) { + return 65536; + } + }, + new Scenario() { + public String toString() { + return "offset is less than buffer.length, length is large"; + } + int getBufferOffset(DataLine line) { + return buffer.length / 10; + } + int getBufferLength(DataLine line) { + return Integer.MAX_VALUE - getBufferOffset(line) + 4096; + } + } + }; + + public static void main(String[] args) throws Exception { + Mixer.Info[] infos = AudioSystem.getMixerInfo(); + log("" + infos.length + " mixers detected"); + for (int i=0; i