From 60a115b0a75e7677158ccc53479945e4e11be854 Mon Sep 17 00:00:00 2001 From: ilag11111 Date: Fri, 25 Apr 2014 00:37:13 -0700 Subject: [PATCH 1/3] Fix (Linux) 64-bit crash in CEZ3 (playback of sfx_litng3). --- src/sdl/mixer_sound.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 6888331d..84003a87 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -152,7 +152,7 @@ static Mix_Chunk *ds2chunk(void *stream) if (!(frac & 0xFFFF)) // other solid multiples (change if FRACBITS != 16) newsamples = samples * (frac >> FRACBITS); else // strange and unusual fractional frequency steps, plus anything higher than 44100hz. - newsamples = FixedMul(frac, samples) + 1; // add 1 sample for security! the code below rounds up. + newsamples = FixedMul(frac, samples) + 2; // add 2 samples for security! the code below rounds up. if (newsamples >= UINT32_MAX>>2) return NULL; // would and/or did wrap, can't store. break; From f7bbf8c6d0d6409577b0fe17564cc9351574ba05 Mon Sep 17 00:00:00 2001 From: ilag11111 Date: Fri, 25 Apr 2014 12:42:43 -0700 Subject: [PATCH 2/3] Improved fix by changing the formula for how memory is allocated to arbitrary sample rate conversion. --- src/sdl/mixer_sound.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 84003a87..717f3e61 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -152,7 +152,7 @@ static Mix_Chunk *ds2chunk(void *stream) if (!(frac & 0xFFFF)) // other solid multiples (change if FRACBITS != 16) newsamples = samples * (frac >> FRACBITS); else // strange and unusual fractional frequency steps, plus anything higher than 44100hz. - newsamples = FixedMul(frac, samples) + 2; // add 2 samples for security! the code below rounds up. + newsamples=(samples/freq + 1) *44100 ; //Result of division is not fractional, so 1 is added to ensure a roundup rather than truncation. if (newsamples >= UINT32_MAX>>2) return NULL; // would and/or did wrap, can't store. break; From b21d5c55c91e564729031deaa036c36aa386f701 Mon Sep 17 00:00:00 2001 From: ilag11111 Date: Fri, 25 Apr 2014 13:17:05 -0700 Subject: [PATCH 3/3] Uses fixed-point math to properly calculate the exact amount of space needed. --- src/sdl/mixer_sound.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 717f3e61..98159b47 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -152,7 +152,7 @@ static Mix_Chunk *ds2chunk(void *stream) if (!(frac & 0xFFFF)) // other solid multiples (change if FRACBITS != 16) newsamples = samples * (frac >> FRACBITS); else // strange and unusual fractional frequency steps, plus anything higher than 44100hz. - newsamples=(samples/freq + 1) *44100 ; //Result of division is not fractional, so 1 is added to ensure a roundup rather than truncation. + newsamples = FixedMul(FixedDiv(samples, freq), 44100) + 1; // add 1 to counter truncation. if (newsamples >= UINT32_MAX>>2) return NULL; // would and/or did wrap, can't store. break;