1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:24:58 +00:00

AK: Disallow returning of string literals for errors in kernel code

This code should not be used in the kernel - we should always propagate
proper errno codes in case we need to return those to userland so it
could decode it in a reasonable way.
This commit is contained in:
Liav A 2023-02-04 14:18:36 +02:00 committed by Linus Groh
parent 56b799c556
commit 048fb2c204
5 changed files with 51 additions and 20 deletions

View file

@ -59,19 +59,19 @@ ErrorOr<size_t> FixedMemoryStream::seek(i64 offset, SeekMode seek_mode)
switch (seek_mode) {
case SeekMode::SetPosition:
if (offset > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the end of the stream memory");
return Error::from_string_view_or_print_error_and_return_errno("Offset past the end of the stream memory"sv, EINVAL);
m_offset = offset;
break;
case SeekMode::FromCurrentPosition:
if (offset + static_cast<i64>(m_offset) > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the end of the stream memory");
return Error::from_string_view_or_print_error_and_return_errno("Offset past the end of the stream memory"sv, EINVAL);
m_offset += offset;
break;
case SeekMode::FromEndPosition:
if (offset > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the start of the stream memory");
return Error::from_string_view_or_print_error_and_return_errno("Offset past the start of the stream memory"sv, EINVAL);
m_offset = m_bytes.size() - offset;
break;
@ -92,7 +92,7 @@ ErrorOr<size_t> FixedMemoryStream::write(ReadonlyBytes bytes)
ErrorOr<void> FixedMemoryStream::write_entire_buffer(ReadonlyBytes bytes)
{
if (remaining() < bytes.size())
return Error::from_string_literal("Write of entire buffer ends past the memory area");
return Error::from_string_view_or_print_error_and_return_errno("Write of entire buffer ends past the memory area"sv, EINVAL);
TRY(write(bytes));
return {};
@ -163,7 +163,7 @@ ErrorOr<void> AllocatingMemoryStream::discard(size_t count)
VERIFY(m_write_offset >= m_read_offset);
if (count > used_buffer_size())
return Error::from_string_literal("Number of discarded bytes is higher than the number of allocated bytes");
return Error::from_string_view_or_print_error_and_return_errno("Number of discarded bytes is higher than the number of allocated bytes"sv, EINVAL);
m_read_offset += count;