Handle failure of op_pcm_total function

According to the documentation, this function returns `0` on success and a negative number (error code) otherwise, which would cause an allocation of an invalid size.
This commit is contained in:
Robert Müller 2024-04-25 20:33:59 +02:00
parent d0e27fdcd4
commit d06f6d3370

View file

@ -346,8 +346,6 @@ bool CSound::DecodeOpus(CSample &Sample, const void *pData, unsigned DataSize) c
if(pOpusFile)
{
const int NumChannels = op_channel_count(pOpusFile, -1);
const int NumSamples = op_pcm_total(pOpusFile, -1); // per channel!
if(NumChannels > 2)
{
op_free(pOpusFile);
@ -355,6 +353,14 @@ bool CSound::DecodeOpus(CSample &Sample, const void *pData, unsigned DataSize) c
return false;
}
const int NumSamples = op_pcm_total(pOpusFile, -1); // per channel!
if(NumSamples < 0)
{
op_free(pOpusFile);
dbg_msg("sound/opus", "failed to get number of samples, error %d", NumSamples);
return false;
}
short *pSampleData = (short *)calloc((size_t)NumSamples * NumChannels, sizeof(short));
int Pos = 0;