Darrell Walisser - Fri, 14 Feb 2003 20:56:08 -0500
authorSam Lantinga <slouken@libsdl.org>
Fri, 21 Feb 2003 16:56:52 +0000
changeset 21774d2e4341a39
parent 216 b813303a6119
child 218 a21e382190a8
Darrell Walisser - Fri, 14 Feb 2003 20:56:08 -0500
* Fixed crash in native midi code with files with more than 32 tracks
CHANGES
native_midi/native_midi_common.c
     1.1 --- a/CHANGES	Fri Feb 21 16:54:24 2003 +0000
     1.2 +++ b/CHANGES	Fri Feb 21 16:56:52 2003 +0000
     1.3 @@ -1,4 +1,6 @@
     1.4  1.2.5:
     1.5 +Darrell Walisser - Fri, 14 Feb 2003 20:56:08 -0500
     1.6 + * Fixed crash in native midi code with files with more than 32 tracks
     1.7  Marc Le Douarain - Sat, 15 Feb 2003 14:46:41 +0100
     1.8   * Added 8SVX format support to the AIFF loader
     1.9  Sam Lantinga Wed Feb 12 21:03:57 PST 2003
     2.1 --- a/native_midi/native_midi_common.c	Fri Feb 21 16:54:24 2003 +0000
     2.2 +++ b/native_midi/native_midi_common.c	Fri Feb 21 16:56:52 2003 +0000
     2.3 @@ -30,8 +30,8 @@
     2.4  #include <limits.h>
     2.5  
     2.6  
     2.7 -/* The maximum number of midi tracks that we can handle */
     2.8 -#define MIDI_TRACKS 32
     2.9 +/* The maximum number of midi tracks that we can handle 
    2.10 +#define MIDI_TRACKS 32 */
    2.11  
    2.12  
    2.13  /* A single midi track as read from the midi file */
    2.14 @@ -45,7 +45,8 @@
    2.15  typedef struct
    2.16  {
    2.17  	int division;					/* number of pulses per quarter note (ppqn) */
    2.18 -	MIDITrack track[MIDI_TRACKS];
    2.19 +    int nTracks;                    /* number of tracks */
    2.20 +	MIDITrack *track;               /* tracks */
    2.21  } MIDIFile;
    2.22  
    2.23  
    2.24 @@ -221,16 +222,20 @@
    2.25   */
    2.26  static MIDIEvent *MIDItoStream(MIDIFile *mididata)
    2.27  {
    2.28 -	MIDIEvent *track[MIDI_TRACKS];
    2.29 +	MIDIEvent **track;
    2.30  	MIDIEvent *head = CreateEvent(0,0,0,0);	/* dummy event to make handling the list easier */
    2.31  	MIDIEvent *currentEvent = head;
    2.32  	int trackID;
    2.33  
    2.34 +    if (NULL == head)
    2.35 +		return NULL;
    2.36 +        
    2.37 +    track = (MIDIEvent**) calloc(1, sizeof(MIDIEvent*) * mididata->nTracks);
    2.38  	if (NULL == head)
    2.39 -		return NULL;
    2.40 +        return NULL;
    2.41  	
    2.42  	/* First, convert all tracks to MIDIEvent lists */
    2.43 -	for (trackID = 0; trackID < MIDI_TRACKS; trackID++)
    2.44 +	for (trackID = 0; trackID < mididata->nTracks; trackID++)
    2.45  		track[trackID] = MIDITracktoStream(&mididata->track[trackID]);
    2.46  
    2.47  	/* Now, merge the lists. */
    2.48 @@ -241,7 +246,7 @@
    2.49  		int currentTrackID = -1;
    2.50  		
    2.51  		/* Find the next event */
    2.52 -		for (trackID = 0; trackID < MIDI_TRACKS; trackID++)
    2.53 +		for (trackID = 0; trackID < mididata->nTracks; trackID++)
    2.54  		{
    2.55  			if (track[trackID] && (track[trackID]->time < lowestTime))
    2.56  			{
    2.57 @@ -267,6 +272,7 @@
    2.58  	currentEvent->next = 0;
    2.59  
    2.60  	currentEvent = head->next;
    2.61 +    free(track);
    2.62  	free(head);	/* release the dummy head event */
    2.63  	return currentEvent;
    2.64  }
    2.65 @@ -304,7 +310,16 @@
    2.66  	
    2.67  	fread(&tracks, 1, 2, fp);
    2.68  	tracks = BE_SHORT(tracks);
    2.69 -	
    2.70 +	mididata->nTracks = tracks;
    2.71 +    
    2.72 +    /* Allocate tracks */
    2.73 +    mididata->track = (MIDITrack*) calloc(1, sizeof(MIDITrack) * mididata->nTracks);
    2.74 +    if (NULL == mididata->track)
    2.75 +    {
    2.76 +        Mix_SetError("Out of memory");
    2.77 +        goto bail;
    2.78 +    }
    2.79 +    
    2.80  	/* Retrieve the PPQN value, needed for playback */
    2.81  	fread(&division, 1, 2, fp);
    2.82  	mididata->division = BE_SHORT(division);
    2.83 @@ -371,12 +386,13 @@
    2.84  	
    2.85  	eventList = MIDItoStream(mididata);
    2.86  	
    2.87 -	for(trackID = 0; trackID < MIDI_TRACKS; trackID++)
    2.88 +	for(trackID = 0; trackID < mididata->nTracks; trackID++)
    2.89  	{
    2.90  		if (mididata->track[trackID].data)
    2.91  			free(mididata->track[trackID].data);
    2.92  	}
    2.93 -	free(mididata);
    2.94 +	free(mididata->track);
    2.95 +    free(mididata);
    2.96  	
    2.97  	return eventList;
    2.98  }