Skip to content

Commit

Permalink
ResampleSfx: Fix resample compute can overflow with very big sounds
Browse files Browse the repository at this point in the history
Issue seen with e.g. gram1.wav from the 'A day like no other' mod.
  • Loading branch information
vsonnier authored and sezero committed Jul 18, 2024
1 parent 2e6a4eb commit 2e6c7d7
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Quake/snd_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e6c7d7

Please sign in to comment.