From 5b2496e522082d82fb135c1cb00d7e4ef3199dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Tue, 19 Sep 2023 12:02:05 +0200 Subject: [PATCH] AK: Make writability violation of FixedMemoryStream non-fatal Writing to a read-only file is not a program-crashing error either, so we just return the standard EBADFD (see write(2)) here. --- AK/MemoryStream.cpp | 6 +++++- Tests/AK/TestMemoryStream.cpp | 6 +----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/AK/MemoryStream.cpp b/AK/MemoryStream.cpp index 47ef8f0f28..530395a66b 100644 --- a/AK/MemoryStream.cpp +++ b/AK/MemoryStream.cpp @@ -93,7 +93,11 @@ ErrorOr FixedMemoryStream::seek(i64 offset, SeekMode seek_mode) ErrorOr FixedMemoryStream::write_some(ReadonlyBytes bytes) { - VERIFY(m_writing_enabled); + // MemoryStream isn't based on file-descriptors, but since most other + // Stream implementations are, the interface specifies EBADF as the + // "we don't support this particular operation" error code. + if (!m_writing_enabled) + return Error::from_errno(EBADF); // FIXME: Can this not error? auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset)); diff --git a/Tests/AK/TestMemoryStream.cpp b/Tests/AK/TestMemoryStream.cpp index a945bb3cbb..cda14cb71c 100644 --- a/Tests/AK/TestMemoryStream.cpp +++ b/Tests/AK/TestMemoryStream.cpp @@ -190,11 +190,7 @@ TEST_CASE(fixed_memory_read_only) TRY_OR_FAIL(stream.seek(0)); ReadonlyBytes buffer { some_words.characters_without_null_termination(), some_words.length() }; - EXPECT_CRASH("Write protection assert", [&] { - (void)stream.write_some(buffer); - return Test::Crash::Failure::DidNotCrash; - }); - + EXPECT(stream.write_some(buffer).is_error()); EXPECT_EQ(TRY_OR_FAIL(stream.tell()), 0ull); EXPECT(!stream.is_eof()); }