Browse Source

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.
kleines Filmröllchen 1 năm trước cách đây
mục cha
commit
5b2496e522

+ 5 - 1
AK/MemoryStream.cpp

@@ -93,7 +93,11 @@ ErrorOr<size_t> FixedMemoryStream::seek(i64 offset, SeekMode seek_mode)
 
 
 ErrorOr<size_t> FixedMemoryStream::write_some(ReadonlyBytes bytes)
 ErrorOr<size_t> 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?
     // FIXME: Can this not error?
     auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
     auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));

+ 1 - 5
Tests/AK/TestMemoryStream.cpp

@@ -190,11 +190,7 @@ TEST_CASE(fixed_memory_read_only)
 
 
     TRY_OR_FAIL(stream.seek(0));
     TRY_OR_FAIL(stream.seek(0));
     ReadonlyBytes buffer { some_words.characters_without_null_termination(), some_words.length() };
     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_EQ(TRY_OR_FAIL(stream.tell()), 0ull);
     EXPECT(!stream.is_eof());
     EXPECT(!stream.is_eof());
 }
 }