mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09:00
AK: Fix offset calculation error in DuplexMemoryStream::write.
This commit is contained in:
parent
ce15c9a04c
commit
5b104eedaf
Notes:
sideshowbarker
2024-07-19 00:56:35 +09:00
Author: https://github.com/asynts
Commit: 5b104eedaf
Pull-request: https://github.com/SerenityOS/serenity/pull/4371
2 changed files with 21 additions and 1 deletions
|
@ -284,12 +284,14 @@ public:
|
||||||
|
|
||||||
size_t write(ReadonlyBytes bytes) override
|
size_t write(ReadonlyBytes bytes) override
|
||||||
{
|
{
|
||||||
|
// FIXME: This doesn't write around chunk borders correctly?
|
||||||
|
|
||||||
size_t nwritten = 0;
|
size_t nwritten = 0;
|
||||||
while (bytes.size() - nwritten > 0) {
|
while (bytes.size() - nwritten > 0) {
|
||||||
if ((m_write_offset + nwritten) % chunk_size == 0)
|
if ((m_write_offset + nwritten) % chunk_size == 0)
|
||||||
m_chunks.append(ByteBuffer::create_uninitialized(chunk_size));
|
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;
|
m_write_offset += nwritten;
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include <AK/TestSuite.h>
|
#include <AK/TestSuite.h>
|
||||||
|
|
||||||
#include <AK/Array.h>
|
#include <AK/Array.h>
|
||||||
|
#include <AK/LogStream.h>
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
|
|
||||||
static bool compare(ReadonlyBytes lhs, ReadonlyBytes rhs)
|
static bool compare(ReadonlyBytes lhs, ReadonlyBytes rhs)
|
||||||
|
@ -216,4 +217,21 @@ TEST_CASE(unsigned_integer_underflow_regression)
|
||||||
stream << buffer;
|
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)
|
TEST_MAIN(MemoryStream)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue