Skip to content

Commit

Permalink
Darrell Walisser - Fri, 14 Feb 2003 20:56:08 -0500
Browse files Browse the repository at this point in the history
 * Fixed crash in native midi code with files with more than 32 tracks
  • Loading branch information
slouken committed Feb 21, 2003
1 parent cf851f2 commit e162ebb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES
@@ -1,4 +1,6 @@
1.2.5:
Darrell Walisser - Fri, 14 Feb 2003 20:56:08 -0500
* Fixed crash in native midi code with files with more than 32 tracks
Marc Le Douarain - Sat, 15 Feb 2003 14:46:41 +0100
* Added 8SVX format support to the AIFF loader
Sam Lantinga Wed Feb 12 21:03:57 PST 2003
Expand Down
36 changes: 26 additions & 10 deletions native_midi/native_midi_common.c
Expand Up @@ -30,8 +30,8 @@
#include <limits.h>


/* The maximum number of midi tracks that we can handle */
#define MIDI_TRACKS 32
/* The maximum number of midi tracks that we can handle
#define MIDI_TRACKS 32 */


/* A single midi track as read from the midi file */
Expand All @@ -45,7 +45,8 @@ typedef struct
typedef struct
{
int division; /* number of pulses per quarter note (ppqn) */
MIDITrack track[MIDI_TRACKS];
int nTracks; /* number of tracks */
MIDITrack *track; /* tracks */
} MIDIFile;


Expand Down Expand Up @@ -221,16 +222,20 @@ static MIDIEvent *MIDITracktoStream(MIDITrack *track)
*/
static MIDIEvent *MIDItoStream(MIDIFile *mididata)
{
MIDIEvent *track[MIDI_TRACKS];
MIDIEvent **track;
MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */
MIDIEvent *currentEvent = head;
int trackID;

if (NULL == head)
if (NULL == head)
return NULL;

track = (MIDIEvent**) calloc(1, sizeof(MIDIEvent*) * mididata->nTracks);
if (NULL == head)
return NULL;

/* First, convert all tracks to MIDIEvent lists */
for (trackID = 0; trackID < MIDI_TRACKS; trackID++)
for (trackID = 0; trackID < mididata->nTracks; trackID++)
track[trackID] = MIDITracktoStream(&mididata->track[trackID]);

/* Now, merge the lists. */
Expand All @@ -241,7 +246,7 @@ static MIDIEvent *MIDItoStream(MIDIFile *mididata)
int currentTrackID = -1;

/* Find the next event */
for (trackID = 0; trackID < MIDI_TRACKS; trackID++)
for (trackID = 0; trackID < mididata->nTracks; trackID++)
{
if (track[trackID] && (track[trackID]->time < lowestTime))
{
Expand All @@ -267,6 +272,7 @@ static MIDIEvent *MIDItoStream(MIDIFile *mididata)
currentEvent->next = 0;

currentEvent = head->next;
free(track);
free(head); /* release the dummy head event */
return currentEvent;
}
Expand Down Expand Up @@ -304,7 +310,16 @@ static int ReadMIDIFile(MIDIFile *mididata, FILE *fp)

fread(&tracks, 1, 2, fp);
tracks = BE_SHORT(tracks);

mididata->nTracks = tracks;

/* Allocate tracks */
mididata->track = (MIDITrack*) calloc(1, sizeof(MIDITrack) * mididata->nTracks);
if (NULL == mididata->track)
{
Mix_SetError("Out of memory");
goto bail;
}

/* Retrieve the PPQN value, needed for playback */
fread(&division, 1, 2, fp);
mididata->division = BE_SHORT(division);
Expand Down Expand Up @@ -371,12 +386,13 @@ MIDIEvent *CreateMIDIEventList(char *midifile, Uint16 *division)

eventList = MIDItoStream(mididata);

for(trackID = 0; trackID < MIDI_TRACKS; trackID++)
for(trackID = 0; trackID < mididata->nTracks; trackID++)
{
if (mididata->track[trackID].data)
free(mididata->track[trackID].data);
}
free(mididata);
free(mididata->track);
free(mididata);

return eventList;
}
Expand Down

0 comments on commit e162ebb

Please sign in to comment.