Fix the off-by-one that has been causing ogg vorbis crashes all these years.

git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5529 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
Spoike 2019-09-03 12:33:14 +00:00
parent 6e0a8d7e2d
commit 6f00bc8e8a
1 changed files with 9 additions and 7 deletions

View File

@ -199,10 +199,10 @@ static sfxcache_t *QDECL OV_DecodeSome(struct sfx_s *sfx, struct sfxcache_s *buf
if (dec->failed || start+length <= dec->decodedbytestart + dec->decodedbytecount)
break;
if (dec->decodedbufferbytes < start+length - dec->decodedbytestart + 128) //expand if needed.
if (dec->decodedbufferbytes < start+length - dec->decodedbytestart + 4096) //expand if needed. 4096 seems to be the recommended size.
{
// Con_Printf("Expand buffer\n");
dec->decodedbufferbytes = (start+length - dec->decodedbytestart) + outspeed;
dec->decodedbufferbytes = (start+length - dec->decodedbytestart) + max(outspeed, 4096); //over allocate, because we can.
dec->decodedbuffer = BZ_Realloc(dec->decodedbuffer, dec->decodedbufferbytes);
}
@ -225,9 +225,10 @@ static sfxcache_t *QDECL OV_DecodeSome(struct sfx_s *sfx, struct sfxcache_s *buf
else
{
double scale = dec->srcspeed / (double)outspeed;
int decodesize = ceil((dec->decodedbufferbytes-dec->decodedbytecount) * scale);
/*round down...*/
decodesize &= ~(2 * dec->srcchannels - 1);
int decodesize = dec->decodedbufferbytes-dec->decodedbytecount; //bytes available
decodesize /= 2*dec->srcchannels; //convert bytes to frames
decodesize = floor(decodesize * scale); //round down, so that the SND_ResampleStream won't overflow the target buffer.
decodesize *= 2*dec->srcchannels; //convert from frames back to bytes
if (decodesize > dec->tempbufferbytes)
{
dec->tempbuffer = BZ_Realloc(dec->tempbuffer, decodesize);
@ -259,8 +260,9 @@ static sfxcache_t *QDECL OV_DecodeSome(struct sfx_s *sfx, struct sfxcache_s *buf
2,
dec->srcchannels,
snd_linearresample_stream.ival);
bytesread = (int)floor(bytesread / scale) & ~(2 * dec->srcchannels - 1);
bytesread /= 2*dec->srcchannels; //convert bytes to frames
bytesread = bytesread / scale; //calculate the same ammount that SND_ResampleStream will have splurged (we should probably make the output count explicit).
bytesread *= 2*dec->srcchannels; //convert frames to bytes
}
dec->decodedbytecount += bytesread;