changeset 964:6b5e1edd95fc

Fix to iterator invalidation (from coverity scan)
author Chris Cannam
date Wed, 03 Sep 2014 10:49:38 +0100
parents 12e62865fd61
children 2d5a8219b4b0
files data/fileio/MIDIFileReader.cpp
diffstat 1 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/MIDIFileReader.cpp	Wed Sep 03 10:40:37 2014 +0100
+++ b/data/fileio/MIDIFileReader.cpp	Wed Sep 03 10:49:38 2014 +0100
@@ -661,7 +661,10 @@
     bool noteOffFound;
 
     for (MIDITrack::iterator i = m_midiComposition[track].begin();
-	 i != m_midiComposition[track].end(); i++) {
+	 i != m_midiComposition[track].end(); ) {
+
+        MIDITrack::iterator nexti = i;
+        ++nexti; 
 
         if ((*i)->getMessageType() == MIDI_NOTE_ON && (*i)->getVelocity() > 0) {
 
@@ -679,6 +682,13 @@
 
                     (*i)->setDuration((*j)->getTime() - (*i)->getTime());
 
+                    if (nexti == j) {
+                        // we're about to erase j, invalidating nexti
+                        // as well (but as this is a map, that is the
+                        // only iterator to be invalidated)
+                        ++nexti;
+                    }
+
                     delete *j;
                     m_midiComposition[track].erase(j);
 
@@ -696,6 +706,8 @@
                 (*i)->setDuration((*j)->getTime() - (*i)->getTime());
 	    }
         }
+
+        i = nexti;
     }
 
     return notesOnTrack;