8135160: Endless Loop in RiffReader
authorserb
Sun, 27 Sep 2015 00:51:27 +0300
changeset 32873 d445b313f219
parent 32872 abca6234313d
child 32874 b563863620ec
8135160: Endless Loop in RiffReader Reviewed-by: prr, amenkov
jdk/src/java.desktop/share/classes/com/sun/media/sound/DLSSoundbank.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/RIFFReader.java
jdk/test/javax/sound/midi/Gervill/RiffReaderWriter/Skip.java
jdk/test/javax/sound/sampled/spi/AudioFileReader/EndlessLoopHugeLengthWave.java
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/DLSSoundbank.java	Sat Sep 26 22:38:36 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/DLSSoundbank.java	Sun Sep 27 00:51:27 2015 +0300
@@ -548,7 +548,7 @@
         long count = riff.readUnsignedInt();
 
         if (size - 8 != 0)
-            riff.skipBytes(size - 8);
+            riff.skip(size - 8);
 
         for (int i = 0; i < count; i++) {
             DLSModulator modulator = new DLSModulator();
@@ -568,7 +568,7 @@
         long count = riff.readUnsignedInt();
 
         if (size - 8 != 0)
-            riff.skipBytes(size - 8);
+            riff.skip(size - 8);
 
         for (int i = 0; i < count; i++) {
             DLSModulator modulator = new DLSModulator();
@@ -661,7 +661,7 @@
         long loops = riff.readInt();
 
         if (size > 20)
-            riff.skipBytes(size - 20);
+            riff.skip(size - 20);
 
         for (int i = 0; i < loops; i++) {
             DLSSampleLoop loop = new DLSSampleLoop();
@@ -671,7 +671,7 @@
             loop.length = riff.readUnsignedInt();
             sampleOptions.loops.add(loop);
             if (size2 > 16)
-                riff.skipBytes(size2 - 16);
+                riff.skip(size2 - 16);
         }
     }
 
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/RIFFReader.java	Sat Sep 26 22:38:36 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/RIFFReader.java	Sun Sep 27 00:51:27 2015 +0300
@@ -118,6 +118,7 @@
         return ckSize;
     }
 
+    @Override
     public int read() throws IOException {
         if (avail == 0) {
             return -1;
@@ -132,6 +133,7 @@
         return b;
     }
 
+    @Override
     public int read(byte[] b, int offset, int len) throws IOException {
         if (avail == 0) {
             return -1;
@@ -172,49 +174,45 @@
         }
     }
 
-    public long skipBytes(long n) throws IOException {
-        if (n < 0)
+    @Override
+    public long skip(final long n) throws IOException {
+        if (n <= 0 || avail == 0) {
             return 0;
-        long skipped = 0;
-        while (skipped != n) {
-            long s = skip(n - skipped);
-            if (s < 0)
+        }
+        // will not skip more than
+        long remaining = Math.min(n, avail);
+        while (remaining > 0) {
+            // Some input streams like FileInputStream can return more bytes,
+            // when EOF is reached.
+            long ret = Math.min(stream.skip(remaining), remaining);
+            if (ret == 0) {
+                // EOF or not? we need to check.
+                Thread.yield();
+                if (stream.read() == -1) {
+                    avail = 0;
+                    break;
+                }
+                ret = 1;
+            } else if (ret < 0) {
+                // the skip should not return negative value, but check it also
+                avail = 0;
                 break;
-            if (s == 0)
-                Thread.yield();
-            skipped += s;
+            }
+            remaining -= ret;
+            avail -= ret;
+            filepointer += ret;
         }
-        return skipped;
+        return n - remaining;
     }
 
-    public long skip(long n) throws IOException {
-        if (avail == 0)
-            return -1;
-        if (n > avail) {
-            long len = stream.skip(avail);
-            if (len != -1)
-                filepointer += len;
-            avail = 0;
-            return len;
-        } else {
-            long ret = stream.skip(n);
-            if (ret == -1) {
-                avail = 0;
-                return -1;
-            }
-            avail -= ret;
-            filepointer += ret;
-            return ret;
-        }
-    }
-
+    @Override
     public int available() {
         return (int)avail;
     }
 
     public void finish() throws IOException {
         if (avail != 0) {
-            skipBytes(avail);
+            skip(avail);
         }
     }
 
@@ -337,6 +335,7 @@
         return ch1 + (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
     }
 
+    @Override
     public void close() throws IOException {
         finish();
         if (this == root)
--- a/jdk/test/javax/sound/midi/Gervill/RiffReaderWriter/Skip.java	Sat Sep 26 22:38:36 2015 +0300
+++ b/jdk/test/javax/sound/midi/Gervill/RiffReaderWriter/Skip.java	Sun Sep 27 00:51:27 2015 +0300
@@ -29,9 +29,8 @@
 import java.io.File;
 import java.io.FileInputStream;
 
-import javax.sound.sampled.*;
-
-import com.sun.media.sound.*;
+import com.sun.media.sound.RIFFReader;
+import com.sun.media.sound.RIFFWriter;
 
 public class Skip {
 
@@ -42,6 +41,11 @@
     }
 
     public static void main(String[] args) throws Exception {
+        test(false);
+        test(true);
+    }
+
+    private static void test(boolean customStream) throws Exception {
         RIFFWriter writer = null;
         RIFFReader reader = null;
         File tempfile = File.createTempFile("test",".riff");
@@ -53,7 +57,17 @@
             chunk.write((byte)44);
             writer.close();
             writer = null;
-            FileInputStream fis = new FileInputStream(tempfile);
+            final FileInputStream fis;
+            if (customStream) {
+                fis = new FileInputStream(tempfile);
+            } else {
+                fis = new FileInputStream(tempfile) {
+                    @Override
+                    public long skip(long n) {
+                        return 0;
+                    }
+                };
+            }
             reader = new RIFFReader(fis);
             RIFFReader readchunk = reader.nextChunk();
             reader.skip(1);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/sound/sampled/spi/AudioFileReader/EndlessLoopHugeLengthWave.java	Sun Sep 27 00:51:27 2015 +0300
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2015, 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.io.ByteArrayInputStream;
+
+import javax.sound.sampled.AudioSystem;
+import javax.sound.sampled.UnsupportedAudioFileException;
+
+/**
+ * @test
+ * @bug 8135160
+ */
+public final class EndlessLoopHugeLengthWave {
+
+    // some data wich can cause an endless loop in RiffReader.java
+    private static byte[] headerWAV = {0x52, 0x49, 0x46, 0x46, // RIFF_MAGIC
+            0x7, 0xF, 0xF, 0xF, // fileLength
+            0x57, 0x41, 0x56, 0x45, //  waveMagic
+            0x66, 0x6d, 0x74, 0x20, // FMT_MAGIC
+            1, 2, 3, 4, // format
+            3, 0,// wav_type  WAVE_FORMAT_IEEE_FLOAT
+            1, 0, // channels
+            1, 1, // sampleRate
+            1, 0, 0, 0, // avgBytesPerSec
+            0, 1, // blockAlign
+            1, 0, // sampleSizeInBits
+            0x64, 0x61, 0x74, 0x61, // DATA_MAGIC
+    };
+
+    public static void main(final String[] args) throws Exception {
+        try {
+            AudioSystem.getAudioFileFormat(new ByteArrayInputStream(headerWAV));
+        } catch (final UnsupportedAudioFileException ignored) {
+            // Expected
+        }
+    }
+}
\ No newline at end of file