From 35f8ea13d9a8863e1398438e4c84179e01b06616 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 31 Jan 2021 15:31:02 +1000 Subject: [PATCH] AudioStream: Prevent writing too many frames at once and hanging --- src/common/audio_stream.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/common/audio_stream.cpp b/src/common/audio_stream.cpp index 1ba911eca..e06d93cae 100644 --- a/src/common/audio_stream.cpp +++ b/src/common/audio_stream.cpp @@ -111,7 +111,8 @@ void AudioStream::BeginWrite(SampleType** buffer_ptr, u32* num_frames) { m_buffer_mutex.lock(); - EnsureBuffer(*num_frames * m_channels); + const u32 requested_frames = std::min(*num_frames, m_buffer_size); + EnsureBuffer(requested_frames * m_channels); *buffer_ptr = m_buffer.GetWritePointer(); *num_frames = m_buffer.GetContiguousSpace() / m_channels; @@ -119,6 +120,7 @@ void AudioStream::BeginWrite(SampleType** buffer_ptr, u32* num_frames) void AudioStream::WriteFrames(const SampleType* frames, u32 num_frames) { + Assert(num_frames <= m_buffer_size); const u32 num_samples = num_frames * m_channels; { std::unique_lock lock(m_buffer_mutex); @@ -261,6 +263,7 @@ void AudioStream::ReadFrames(SampleType* samples, u32 num_frames, bool apply_vol void AudioStream::EnsureBuffer(u32 size) { + DebugAssert(size <= m_buffer_size); if (GetBufferSpace() >= size) return;