From 767fb01a8cf54f0760e5bbf408eb89d67239575d Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Tue, 4 Apr 2023 18:29:43 +0200 Subject: [PATCH] AK: Report copied bytes when seekback copying from CircularBuffer Otherwise, we have no way of determining whether our copy was truncated by accident. --- AK/CircularBuffer.cpp | 9 +++++---- AK/CircularBuffer.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/AK/CircularBuffer.cpp b/AK/CircularBuffer.cpp index 6d8aa4d742c..884bad0f345 100644 --- a/AK/CircularBuffer.cpp +++ b/AK/CircularBuffer.cpp @@ -206,20 +206,21 @@ ErrorOr CircularBuffer::fill_from_stream(Stream& stream) return bytes.size(); } -ErrorOr CircularBuffer::copy_from_seekback(size_t distance, size_t length) +ErrorOr CircularBuffer::copy_from_seekback(size_t distance, size_t length) { if (distance > m_seekback_limit) return Error::from_string_literal("Tried a seekback copy beyond the seekback limit"); - while (length > 0) { + auto remaining_length = length; + while (remaining_length > 0) { auto next_span = next_read_span_with_seekback(distance); if (next_span.size() == 0) break; - length -= write(next_span.trim(length)); + remaining_length -= write(next_span.trim(remaining_length)); } - return {}; + return length - remaining_length; } } diff --git a/AK/CircularBuffer.h b/AK/CircularBuffer.h index be23b791e47..89861d02151 100644 --- a/AK/CircularBuffer.h +++ b/AK/CircularBuffer.h @@ -33,7 +33,7 @@ public: /// before the current write pointer and allows for reading already-read data. ErrorOr read_with_seekback(Bytes bytes, size_t distance); - ErrorOr copy_from_seekback(size_t distance, size_t length); + ErrorOr copy_from_seekback(size_t distance, size_t length); [[nodiscard]] size_t empty_space() const; [[nodiscard]] size_t used_space() const;