Handle OOM in note_on without SEGV

When note_on tries to reallocate the voices[] array, realloc can return
NULL.  In this case, instead of faulting, restore the original pointer
(which is untouched per realloc() semantics) and ignore the note_on
command.
This commit is contained in:
Earle F. Philhower, III committed 2017-11-14 15:11:59 -08:00
1 parent f7b6f90ffe
commit 545127558f
1 file changed
+6
+6
View File
@@ -1521,7 +1521,13 @@ TSFDEF void tsf_note_on(tsf* f, int preset, int key, float vel)
if (!voice)
{
f->voiceNum += 4;
struct tsf_voice *saveVoice = f->voices;
f->voices = (struct tsf_voice*)TSF_REALLOC(f->voices, f->voiceNum * sizeof(struct tsf_voice));
if (!f->voices) {
f->voices = saveVoice;
printf("OOM, no room for new voice. Ignoring note_on\n");
return;
}
voice = &f->voices[f->voiceNum - 4];
voice[1].playingPreset = voice[2].playingPreset = voice[3].playingPreset = -1;
}