7058697: Unexpected exceptions in MID parser code
authorserb
Wed, 30 Jul 2014 16:11:47 +0400
changeset 26018 2a797c982509
parent 26017 4d9ac57cd697
child 26019 10a56d28f48d
7058697: Unexpected exceptions in MID parser code Reviewed-by: prr, pchelko
jdk/src/share/classes/com/sun/media/sound/SoftMidiAudioFileReader.java
jdk/src/share/classes/com/sun/media/sound/StandardMidiFileReader.java
--- a/jdk/src/share/classes/com/sun/media/sound/SoftMidiAudioFileReader.java	Wed Jul 30 14:29:36 2014 +0400
+++ b/jdk/src/share/classes/com/sun/media/sound/SoftMidiAudioFileReader.java	Wed Jul 30 16:11:47 2014 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2014, 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
@@ -22,6 +22,7 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
+
 package com.sun.media.sound;
 
 import java.io.File;
@@ -39,14 +40,14 @@
 import javax.sound.midi.Sequence;
 import javax.sound.midi.Track;
 import javax.sound.sampled.AudioFileFormat;
+import javax.sound.sampled.AudioFileFormat.Type;
 import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioInputStream;
 import javax.sound.sampled.UnsupportedAudioFileException;
-import javax.sound.sampled.AudioFileFormat.Type;
 import javax.sound.sampled.spi.AudioFileReader;
 
 /**
- * MIDI File Audio Renderer/Reader
+ * MIDI File Audio Renderer/Reader.
  *
  * @author Karl Helgason
  */
@@ -109,6 +110,9 @@
                 if (divtype == Sequence.PPQ) {
                     if (((MetaMessage) msg).getType() == 0x51) {
                         byte[] data = ((MetaMessage) msg).getData();
+                        if (data.length < 3) {
+                            throw new UnsupportedAudioFileException();
+                        }
                         mpq = ((data[0] & 0xff) << 16)
                                 | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
                     }
--- a/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileReader.java	Wed Jul 30 14:29:36 2014 +0400
+++ b/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileReader.java	Wed Jul 30 16:11:47 2014 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, 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
@@ -25,27 +25,25 @@
 
 package com.sun.media.sound;
 
+import java.io.BufferedInputStream;
 import java.io.DataInputStream;
+import java.io.EOFException;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
-import java.io.IOException;
-import java.io.EOFException;
-import java.io.BufferedInputStream;
 import java.net.URL;
 
-import javax.sound.midi.MidiFileFormat;
 import javax.sound.midi.InvalidMidiDataException;
 import javax.sound.midi.MetaMessage;
 import javax.sound.midi.MidiEvent;
+import javax.sound.midi.MidiFileFormat;
 import javax.sound.midi.MidiMessage;
 import javax.sound.midi.Sequence;
 import javax.sound.midi.SysexMessage;
 import javax.sound.midi.Track;
 import javax.sound.midi.spi.MidiFileReader;
 
-
-
 /**
  * MIDI file reader.
  *
@@ -53,19 +51,23 @@
  * @author Jan Borgersen
  * @author Florian Bomers
  */
-
 public final class StandardMidiFileReader extends MidiFileReader {
 
     private static final int MThd_MAGIC = 0x4d546864;  // 'MThd'
 
     private static final int bisBufferSize = 1024; // buffer size in buffered input streams
 
-    public MidiFileFormat getMidiFileFormat(InputStream stream) throws InvalidMidiDataException, IOException {
+    public MidiFileFormat getMidiFileFormat(InputStream stream)
+            throws InvalidMidiDataException, IOException {
         return getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, null);
     }
 
-    // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
-    private MidiFileFormat getMidiFileFormatFromStream(InputStream stream, int fileLength, SMFParser smfParser) throws InvalidMidiDataException, IOException {
+    // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat()
+    // returns format having invalid length
+    private MidiFileFormat getMidiFileFormatFromStream(InputStream stream,
+                                                       int fileLength,
+                                                       SMFParser smfParser)
+            throws InvalidMidiDataException, IOException{
         int maxReadLength = 16;
         int duration = MidiFileFormat.UNKNOWN_LENGTH;
         DataInputStream dis;
@@ -230,7 +232,7 @@
 //=============================================================================================================
 
 /**
- * State variables during parsing of a MIDI file
+ * State variables during parsing of a MIDI file.
  */
 final class SMFParser {
     private static final int MTrk_MAGIC = 0x4d54726b;  // 'MTrk'
@@ -297,7 +299,11 @@
             }
         }
         // now read track in a byte array
-        trackData = new byte[trackLength];
+        try {
+            trackData = new byte[trackLength];
+        } catch (final OutOfMemoryError oom) {
+            throw new IOException("Track length too big", oom);
+        }
         try {
             // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection
             stream.readFully(trackData);
@@ -386,8 +392,13 @@
                         // meta
                         int metaType = readUnsigned();
                         int metaLength = (int) readVarInt();
+                        final byte[] metaData;
+                        try {
+                            metaData = new byte[metaLength];
+                        } catch (final OutOfMemoryError oom) {
+                            throw new IOException("Meta length too big", oom);
+                        }
 
-                        byte[] metaData = new byte[metaLength];
                         read(metaData);
 
                         MetaMessage metaMessage = new MetaMessage();
@@ -413,5 +424,4 @@
             throw new EOFException("invalid MIDI file");
         }
     }
-
 }