From e162ebbf4e90e2c2f00b76270a17d343dfe83959 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 21 Feb 2003 16:56:52 +0000 Subject: [PATCH] Darrell Walisser - Fri, 14 Feb 2003 20:56:08 -0500 * Fixed crash in native midi code with files with more than 32 tracks --- CHANGES | 2 ++ native_midi/native_midi_common.c | 36 +++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/CHANGES b/CHANGES index 400c6750..5f7a7262 100644 --- a/CHANGES +++ b/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 diff --git a/native_midi/native_midi_common.c b/native_midi/native_midi_common.c index ad97ec2c..ae347612 100644 --- a/native_midi/native_midi_common.c +++ b/native_midi/native_midi_common.c @@ -30,8 +30,8 @@ #include -/* 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 */ @@ -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; @@ -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. */ @@ -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)) { @@ -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; } @@ -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); @@ -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; }