From 545127558ff5e16ac1ad5db02f0e1fa63b36846c Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 14 Nov 2017 15:11:59 -0800 Subject: [PATCH] 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. --- src/libtinysoundfont/tsf.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libtinysoundfont/tsf.h b/src/libtinysoundfont/tsf.h index 6ca0e24..00e18af 100644 --- a/src/libtinysoundfont/tsf.h +++ b/src/libtinysoundfont/tsf.h @@ -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; }