1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:44: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

@ -16,14 +16,19 @@ ErrorOr<void> Stream::read_entire_buffer(Bytes buffer)
size_t nread = 0;
while (nread < buffer.size()) {
if (is_eof())
return Error::from_string_literal("Reached end-of-file before filling the entire buffer");
return Error::from_string_view_or_print_error_and_return_errno("Reached end-of-file before filling the entire buffer"sv, EIO);
auto result = read(buffer.slice(nread));
if (result.is_error()) {
#ifdef KERNEL
if (result.error().code() == EINTR) {
continue;
}
#else
if (result.error().is_errno() && result.error().code() == EINTR) {
continue;
}
#endif
return result.release_error();
}
@ -69,7 +74,7 @@ ErrorOr<void> Stream::discard(size_t discarded_bytes)
while (discarded_bytes > 0) {
if (is_eof())
return Error::from_string_literal("Reached end-of-file before reading all discarded bytes");
return Error::from_string_view_or_print_error_and_return_errno("Reached end-of-file before reading all discarded bytes"sv, EIO);
auto slice = TRY(read(buffer.span().slice(0, min(discarded_bytes, continuous_read_size))));
discarded_bytes -= slice.size();
@ -84,10 +89,15 @@ ErrorOr<void> Stream::write_entire_buffer(ReadonlyBytes buffer)
while (nwritten < buffer.size()) {
auto result = write(buffer.slice(nwritten));
if (result.is_error()) {
#ifdef KERNEL
if (result.error().code() == EINTR) {
continue;
}
#else
if (result.error().is_errno() && result.error().code() == EINTR) {
continue;
}
#endif
return result.release_error();
}