From f606e44d7873529b65e5b0d4f15e11fdf3049bad Mon Sep 17 00:00:00 2001
From: sezero <[EMAIL REDACTED]>
Date: Thu, 31 Jul 2025 05:56:10 +0300
Subject: [PATCH] sndfont.c (load_from_file): allocate the sp data with 3
padding samples
prevents invalid reads in the resampler.
---
src/timidity/sndfont.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/timidity/sndfont.c b/src/timidity/sndfont.c
index adcc89bb..110670b2 100644
--- a/src/timidity/sndfont.c
+++ b/src/timidity/sndfont.c
@@ -267,9 +267,12 @@ static Instrument *load_from_file(MidiSong *song, SFInsts *rec, InstList *ip)
Sint16 *tmp, s;
#endif
SDL_memcpy(sample, &sp->v, sizeof(Sample));
- sample->data = (sample_t*) SDL_malloc(sp->endsample);
+ sample->data = (sample_t*) SDL_malloc(sp->endsample + 6);
SDL_SeekIO(rec->io, sp->startsample, SDL_IO_SEEK_SET);
SDL_ReadIO(rec->io, sample->data, sp->endsample);
+ /* initialize the 3 extra samples at the end (those +6 bytes) */
+ sample->data[sp->endsample/2] = sample->data[sp->endsample/2 + 1] =
+ sample->data[sp->endsample/2 + 2] = 0;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
tmp = (Sint16*)sample->data;
for (j = 0; j < sp->endsample/2; j++) {