diff options
author | Tim Schumacher <timschumi@gmx.de> | 2023-05-29 11:40:59 +0200 |
---|---|---|
committer | Jelle Raaijmakers <jelle@gmta.nl> | 2023-05-29 13:30:46 +0200 |
commit | 58b1d9c319fbafac8318bffbf41380e1c9ee1175 (patch) | |
tree | 43f24e6a6413c9765e6f984ec8aa4da6eff3eef4 /AK | |
parent | 52aab509142b71a9fd0c4accb7ea25d94619d52f (diff) | |
download | serenity-58b1d9c319fbafac8318bffbf41380e1c9ee1175.zip |
AK: Correctly calculate size of the last AllocatingMemoryStream chunk
Diffstat (limited to 'AK')
-rw-r--r-- | AK/MemoryStream.cpp | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/AK/MemoryStream.cpp b/AK/MemoryStream.cpp index c0d2be82c4..19448bfbbe 100644 --- a/AK/MemoryStream.cpp +++ b/AK/MemoryStream.cpp @@ -210,8 +210,16 @@ ErrorOr<Optional<size_t>> AllocatingMemoryStream::offset_of(ReadonlyBytes needle search_spans[i] = m_chunks[i].span(); } + auto used_size_of_last_chunk = m_write_offset % CHUNK_SIZE; + + // The case where the stored write offset is actually the used space is the only case where a result of zero + // actually is zero. In other cases (i.e. our write offset is beyond the size of a chunk) the write offset + // already points to the beginning of the next chunk, in that case a result of zero indicates "use the last chunk in full". + if (m_write_offset >= CHUNK_SIZE && used_size_of_last_chunk == 0) + used_size_of_last_chunk = CHUNK_SIZE; + // Trimming is done first to ensure that we don't unintentionally shift around if the first and last chunks are the same. - search_spans[chunk_count - 1] = search_spans[chunk_count - 1].trim(m_write_offset % CHUNK_SIZE); + search_spans[chunk_count - 1] = search_spans[chunk_count - 1].trim(used_size_of_last_chunk); search_spans[0] = search_spans[0].slice(m_read_offset); return AK::memmem(search_spans.begin(), search_spans.end(), needle); |