From 767a2ced73d4989bb7204b01d12189b940a0ff2f Mon Sep 17 00:00:00 2001 From: Ivo Creusen Date: Fri, 23 Mar 2018 15:08:51 +0100 Subject: [PATCH] Fix for crash when reading from audio file in NetEq. The neteq_rtpplay tool can crash when the replacement audio file is too short. The desired behavior is that the audio file is looped as much as necessary. Bug: webrtc:9061 Change-Id: Iefba4c47271584845662a415598bf2197dba0fae Reviewed-on: https://webrtc-review.googlesource.com/64460 Commit-Queue: Ivo Creusen Reviewed-by: Henrik Lundin Cr-Commit-Position: refs/heads/master@{#22585} --- modules/audio_coding/neteq/tools/input_audio_file.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/audio_coding/neteq/tools/input_audio_file.cc b/modules/audio_coding/neteq/tools/input_audio_file.cc index 31ebf98342..330a874ea9 100644 --- a/modules/audio_coding/neteq/tools/input_audio_file.cc +++ b/modules/audio_coding/neteq/tools/input_audio_file.cc @@ -56,13 +56,18 @@ bool InputAudioFile::Seek(int samples) { RTC_CHECK_NE(EOF, file_size) << "Error returned when getting file position."; // Find new position. long new_pos = current_pos + sizeof(int16_t) * samples; // Samples to bytes. - RTC_CHECK_GE(new_pos, 0) - << "Trying to move to before the beginning of the file"; if (loop_at_end_) { new_pos = new_pos % file_size; // Wrap around the end of the file. + if (new_pos < 0) { + // For negative values of new_pos, newpos % file_size will also be + // negative. To get the correct result it's needed to add file_size. + new_pos += file_size; + } } else { new_pos = new_pos > file_size ? file_size : new_pos; // Don't loop. } + RTC_CHECK_GE(new_pos, 0) + << "Trying to move to before the beginning of the file"; // Move to new position relative to the beginning of the file. RTC_CHECK_EQ(0, fseek(fp_, new_pos, SEEK_SET)); return true;