From ed724fecb15d90d8075ed1edb963f455cb91b0a1 Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Sun, 4 Aug 2019 16:58:20 -0400 Subject: Midiplay plugin ehancements - Improved robustness - Improved sound quality - Use mixer and DSP Patch by Igor Poretsky Change-Id: I6fa617158cbaa53ae842295cdbdbe3a478e49ded --- apps/plugins/midi/midifile.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'apps/plugins/midi/midifile.c') diff --git a/apps/plugins/midi/midifile.c b/apps/plugins/midi/midifile.c index 86544fd944..2340c6d14f 100644 --- a/apps/plugins/midi/midifile.c +++ b/apps/plugins/midi/midifile.c @@ -106,6 +106,11 @@ struct MIDIfile * loadFile(const char * filename) if(id == ID_MTRK) { mfload->tracks[track] = readTrack(file); + if (!mfload->tracks[track]) + { + rb->close(file); + return NULL; + } track++; } else { @@ -125,7 +130,7 @@ struct MIDIfile * loadFile(const char * filename) * and then track 2 starts loading */ int rStatus = 0; -/* Returns 0 if done, 1 if keep going */ +/* Returns 0 if done, 1 if keep going and -1 in case of error */ static int readEvent(int file, void * dest) { struct Event dummy; @@ -152,6 +157,8 @@ static int readEvent(int file, void * dest) { /* Null-terminate for text events */ ev->evData = malloc(ev->len+1); /* Extra byte for the null termination */ + if (!ev->evData) + return -1; rb->read(file, ev->evData, ev->len); ev->evData[ev->len] = 0; @@ -272,27 +279,35 @@ struct Track * readTrack(int file) int pos = rb->lseek(file, 0, SEEK_CUR); - while(readEvent(file, NULL)) /* Memory saving technique */ + int evstat; + + while ((evstat = readEvent(file, NULL)) > 0) /* Memory saving technique */ numEvents++; /* Attempt to read in events, count how many */ /* THEN allocate memory and read them in */ + if (evstat < 0) + return NULL; rb->lseek(file, pos, SEEK_SET); int trackSize = (numEvents+1) * sizeof(struct Event); void * dataPtr = malloc(trackSize); + if (!dataPtr) + return NULL; trk->dataBlock = dataPtr; numEvents=0; - while(readEvent(file, dataPtr)) + while ((evstat = readEvent(file, dataPtr)) > 0) { if(trackSize < dataPtr-trk->dataBlock) { midi_debug("Track parser memory out of bounds"); - exit(1); + return NULL; } dataPtr+=sizeof(struct Event); numEvents++; } + if (evstat < 0) + return NULL; trk->numEvents = numEvents; return trk; -- cgit v1.2.3