From 2e6c7d769454e4456026ed052a6596d70fe33553 Mon Sep 17 00:00:00 2001 From: Vincent Sonnier Date: Thu, 18 Jul 2024 07:05:06 +0200 Subject: [PATCH] ResampleSfx: Fix resample compute can overflow with very big sounds Issue seen with e.g. gram1.wav from the 'A day like no other' mod. --- Quake/snd_mem.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Quake/snd_mem.c b/Quake/snd_mem.c index 80813c2a3..43a29eced 100644 --- a/Quake/snd_mem.c +++ b/Quake/snd_mem.c @@ -34,7 +34,7 @@ static void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data) int srcsample; float stepscale; int i; - int sample, samplefrac, fracstep; + int sample, fracstep; sfxcache_t *sc; sc = (sfxcache_t *) Cache_Check (&sfx->cache); @@ -66,12 +66,13 @@ static void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data) else { // general case - samplefrac = 0; - fracstep = stepscale*256; + // samplefrac can overflow 2**31 with very big sounds, see below. + int64_t samplefrac = 0; + fracstep = (int)(stepscale * 256); for (i = 0; i < outcount; i++) { - srcsample = samplefrac >> 8; - samplefrac += fracstep; + srcsample = (int)(samplefrac >> 8); + samplefrac += fracstep; // need int64_t here to prevent overflow... if (inwidth == 2) sample = LittleShort ( ((short *)data)[srcsample] ); else