diff options
author | Karol Kosek <krkk@krkk.ct8.pl> | 2021-07-22 16:56:05 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-22 22:57:05 +0200 |
commit | 69c7b66f06bbfbb565a8e879889ba89042622874 (patch) | |
tree | 460b4827aa22565abbce31fb99df9e1d47c2ea37 | |
parent | 73fc2b3748dbf13a05b1ea60e0fa97529df6283d (diff) | |
download | serenity-69c7b66f06bbfbb565a8e879889ba89042622874.zip |
LibAudio: Don't read too much bytes in FLAC
This fixes crash when reading the end of the file.
The logic is mostly borrowed from WavLoader.
-rw-r--r-- | Userland/Libraries/LibAudio/FlacLoader.cpp | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index 3289dcc846..639ea05231 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -210,8 +210,13 @@ void FlacLoaderPlugin::seek(const int position) RefPtr<Buffer> FlacLoaderPlugin::get_more_samples([[maybe_unused]] size_t max_bytes_to_read_from_input) { Vector<Frame> samples; - size_t remaining_samples = max_bytes_to_read_from_input; - while (remaining_samples > 0) { + ssize_t remaining_samples = m_total_samples - m_loaded_samples; + if (remaining_samples <= 0) { + return nullptr; + } + + size_t samples_to_read = min(max_bytes_to_read_from_input, remaining_samples); + while (samples_to_read > 0) { if (!m_current_frame.has_value()) { next_frame(); if (!m_error_string.is_empty()) { @@ -227,7 +232,7 @@ RefPtr<Buffer> FlacLoaderPlugin::get_more_samples([[maybe_unused]] size_t max_by if (m_current_frame_data.size() == 0) { m_current_frame.clear(); } - --remaining_samples; + --samples_to_read; } return Buffer::create_with_samples(move(samples)); |