AK: Fix offset calculation error in DuplexMemoryStream::write.

This commit is contained in:
asynts 2020-12-09 15:11:09 +01:00 committed by Andreas Kling
parent ce15c9a04c
commit 5b104eedaf
Notes: sideshowbarker 2024-07-19 00:56:35 +09:00
2 changed files with 21 additions and 1 deletions

View file

@ -284,12 +284,14 @@ public:
size_t write(ReadonlyBytes bytes) override
{
// FIXME: This doesn't write around chunk borders correctly?
size_t nwritten = 0;
while (bytes.size() - nwritten > 0) {
if ((m_write_offset + nwritten) % chunk_size == 0)
m_chunks.append(ByteBuffer::create_uninitialized(chunk_size));
nwritten += bytes.slice(nwritten).copy_trimmed_to(m_chunks.last().bytes().slice(m_write_offset % chunk_size));
nwritten += bytes.slice(nwritten).copy_trimmed_to(m_chunks.last().bytes().slice((m_write_offset + nwritten) % chunk_size));
}
m_write_offset += nwritten;

View file

@ -27,6 +27,7 @@
#include <AK/TestSuite.h>
#include <AK/Array.h>
#include <AK/LogStream.h>
#include <AK/MemoryStream.h>
static bool compare(ReadonlyBytes lhs, ReadonlyBytes rhs)
@ -216,4 +217,21 @@ TEST_CASE(unsigned_integer_underflow_regression)
stream << buffer;
}
TEST_CASE(offset_calculation_error_regression)
{
Array<u8, DuplexMemoryStream::chunk_size> input, output;
input.span().fill(0xff);
DuplexMemoryStream stream;
stream << 0x00000000 << input << 0x00000000;
stream.discard_or_error(sizeof(int));
stream.read(output);
EXPECT(compare(input, output));
AK::dump_bytes(input);
AK::dump_bytes(output);
}
TEST_MAIN(MemoryStream)