Fix crash on out of range MIDI events (though this shouldn't be possible at all....
[ardour.git] / libs / ardour / smf_reader.cc
index 21ca370e6cd5e69b7789f27cc009f1fb73d025da..c5e715c2ce830f8beca4a73c1ba03b97fd620785 100644 (file)
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include <cstring>
 #include <cstdio>
 #include <cassert>
 #include <iostream>
 #include <glibmm/miscutils.h>
+#include <midi++/events.h>
+
 #include <ardour/smf_reader.h>
-#include <ardour/midi_events.h>
 #include <ardour/midi_util.h>
 
 using namespace std;
@@ -194,7 +196,7 @@ SMFReader::read_event(size_t    buf_len,
        static uint8_t  last_status = 0;
        static uint32_t last_size   = 0;
 
-       *delta_time = read_var_len();
+       *delta_time = read_var_len(_fd);
        int status = fgetc(_fd);
        if (status == EOF)
                throw PrematureEOF();
@@ -222,7 +224,7 @@ SMFReader::read_event(size_t    buf_len,
                if (feof(_fd))
                        throw PrematureEOF();
                uint8_t type = fgetc(_fd);
-               const uint32_t size = read_var_len();
+               const uint32_t size = read_var_len(_fd);
                /*cerr.flags(ios::hex);
                cerr << "SMF - meta 0x" << (int)type << ", size = ";
                cerr.flags(ios::dec);
@@ -269,20 +271,20 @@ SMFReader::close()
 
 
 uint32_t
-SMFReader::read_var_len() const throw(PrematureEOF)
+SMFReader::read_var_len(FILE* fd) throw (PrematureEOF)
 {
-       if (feof(_fd))
+       if (feof(fd))
                throw PrematureEOF();
 
        uint32_t value;
        uint8_t  c;
 
-       if ( (value = getc(_fd)) & 0x80 ) {
+       if ( (value = getc(fd)) & 0x80 ) {
                value &= 0x7F;
                do {
-                       if (feof(_fd))
+                       if (feof(fd))
                                throw PrematureEOF();
-                       value = (value << 7) + ((c = getc(_fd)) & 0x7F);
+                       value = (value << 7) + ((c = getc(fd)) & 0x7F);
                } while (c & 0x80);
        }