From 33494596a47a2228dd42ebe21d09082b5f9a72f9 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 7 Aug 2020 21:49:39 -0700 Subject: [PATCH] Fix OOB read on fast sample generation in MIDI Valgrind identified cases where the chunk read position came out to be -1, which was causing a read of memory before the actual cache buffers. This could result in add'l noise on playback. --- src/libtinysoundfont/tsf.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libtinysoundfont/tsf.h b/src/libtinysoundfont/tsf.h index be47f66..c3368ff 100644 --- a/src/libtinysoundfont/tsf.h +++ b/src/libtinysoundfont/tsf.h @@ -1268,6 +1268,7 @@ short tsf_read_short_cached(tsf *f, int pos) f->timestamp[repl] = f->epoch++; f->offset[repl] = readOff; misses++; +// fprintf(stderr, "buffer[%d][%d-%d = %d]\n", repl, pos, readOff, pos - readOff); return f->buffer[repl][pos - readOff]; } @@ -1420,9 +1421,9 @@ static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, i } -void DumpF32P32(char *name, long long x) { - printf("%s = %08x.%08x\n", name, (int32_t)((x>>32)&0xffffffff), (int32_t)(x&0xffffffff)); -} +//void DumpF32P32(char *name, long long x) { +// printf("%s = %08x.%08x\n", name, (int32_t)((x>>32)&0xffffffff), (int32_t)(x&0xffffffff)); +//} static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuffer, int numSamples) { struct tsf_region* region = v->region; @@ -1501,6 +1502,7 @@ static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuff while (blockSamples-- && tmpSourceSamplePositionF32P32 < tmpSampleEndF32P32) { unsigned int pos = (unsigned int)(tmpSourceSamplePositionF32P32>>32); + if (pos == 0xffffffff) pos = 0; short val = tsf_read_short_cached(f, pos); int32_t val32 = (int)val * (int)gainMonoFP;