From c69a982563b892fbd37936c91c3e760d2ca57aae Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 19 Mar 2025 08:44:28 -0400 Subject: [PATCH] MT#59962 AmMultiPartyMixer: protect against /0 Avoid possible division by zero. Change-Id: I8e4127ac318aff8c90c2c6282e910d1c340a4ea0 Warned-by: Coverity --- core/AmMultiPartyMixer.cpp | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/core/AmMultiPartyMixer.cpp b/core/AmMultiPartyMixer.cpp index 59428f32..dcc24adb 100644 --- a/core/AmMultiPartyMixer.cpp +++ b/core/AmMultiPartyMixer.cpp @@ -113,21 +113,27 @@ void AmMultiPartyMixer::PutChannelPacket(unsigned int channel_id, return; assert(size <= AUDIO_BUFFER_SIZE); - std::deque::iterator bstate = findOrCreateBufferState(GetCurrentSampleRate()); + int srate = GetCurrentSampleRate(); + if (srate < 100) { + ERROR("Invalid/unknown sample rate (%d)", srate); + return; + } + + std::deque::iterator bstate = findOrCreateBufferState(srate); SampleArrayShort* channel = 0; if((channel = bstate->get_channel(channel_id)) != 0) { unsigned samples = PCM16_B2S(size); unsigned long long put_ts = system_ts + (MIXER_DELAY_MS * WALLCLOCK_RATE / 1000); - unsigned long long user_put_ts = put_ts * (GetCurrentSampleRate()/100) / (WALLCLOCK_RATE/100); + unsigned long long user_put_ts = put_ts * (srate/100) / (WALLCLOCK_RATE/100); channel->put(user_put_ts,(short*)buffer,samples); bstate->mixed_channel->get(user_put_ts,tmp_buffer,samples); mix_add(tmp_buffer,tmp_buffer,(short*)buffer,samples); bstate->mixed_channel->put(user_put_ts,tmp_buffer,samples); - bstate->last_ts = put_ts + (samples * (WALLCLOCK_RATE/100) / (GetCurrentSampleRate()/100)); + bstate->last_ts = put_ts + (samples * (WALLCLOCK_RATE/100) / (srate/100)); } else { /* ERROR("XXDebugMixerXX: MultiPartyMixer::PutChannelPacket: " @@ -149,13 +155,20 @@ void AmMultiPartyMixer::GetChannelPacket(unsigned int channel_id, return; assert(size <= AUDIO_BUFFER_SIZE); - unsigned int last_ts = system_ts + (PCM16_B2S(size) * (WALLCLOCK_RATE/100) / (GetCurrentSampleRate()/100)); - std::deque::iterator bstate = findBufferStateForReading(GetCurrentSampleRate(), last_ts); + int srate = GetCurrentSampleRate(); + if (srate < 100) { + ERROR("Invalid/unknown sample rate (%d)", srate); + size = 0; + return; + } + + unsigned int last_ts = system_ts + (PCM16_B2S(size) * (WALLCLOCK_RATE/100) / (srate/100)); + std::deque::iterator bstate = findBufferStateForReading(srate, last_ts); SampleArrayShort* channel = 0; if(bstate != buffer_state.end() && (channel = bstate->get_channel(channel_id)) != 0) { - unsigned int samples = PCM16_B2S(size) * (bstate->sample_rate/100) / (GetCurrentSampleRate()/100); + unsigned int samples = PCM16_B2S(size) * (bstate->sample_rate/100) / (srate/100); assert(samples <= PCM16_B2S(AUDIO_BUFFER_SIZE)); unsigned long long cur_ts = system_ts * (bstate->sample_rate/100) / (WALLCLOCK_RATE/100); @@ -168,7 +181,7 @@ void AmMultiPartyMixer::GetChannelPacket(unsigned int channel_id, output_sample_rate = bstate->sample_rate; } else if (bstate != buffer_state.end()) { memset(buffer,0,size); - output_sample_rate = GetCurrentSampleRate(); + output_sample_rate = srate; //DBG("XXDebugMixerXX: GetChannelPacket returned zeroes, ts=%u, last_ts=%u, output_sample_rate=%u", ts, last_ts, output_sample_rate); } else { /*