From e46343bf9aadaf8b310821c8a92b131f2b726654 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 13 May 2021 09:08:44 +0200 Subject: Kernel: Make UserOrKernelBuffer R/W helpers return KResultOr This makes error propagation less cumbersome (and also exposed some places where we were not doing it.) --- Kernel/TTY/TTY.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'Kernel/TTY/TTY.cpp') diff --git a/Kernel/TTY/TTY.cpp b/Kernel/TTY/TTY.cpp index 020150c86f..516b255e4d 100644 --- a/Kernel/TTY/TTY.cpp +++ b/Kernel/TTY/TTY.cpp @@ -43,10 +43,10 @@ KResultOr TTY::read(FileDescription&, u64, UserOrKernelBuffer& buffer, s if (m_input_buffer.size() < static_cast(size)) size = m_input_buffer.size(); - ssize_t nwritten; + KResultOr result = 0; bool need_evaluate_block_conditions = false; if (in_canonical_mode()) { - nwritten = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) { + result = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) { size_t i = 0; for (; i < data_size; i++) { u8 ch = m_input_buffer.dequeue(); @@ -64,10 +64,10 @@ KResultOr TTY::read(FileDescription&, u64, UserOrKernelBuffer& buffer, s } data[i] = ch; } - return (ssize_t)i; + return i; }); } else { - nwritten = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) { + result = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) { for (size_t i = 0; i < data_size; i++) { auto ch = m_input_buffer.dequeue(); if (ch == '\r' && m_termios.c_iflag & ICRNL) @@ -76,14 +76,12 @@ KResultOr TTY::read(FileDescription&, u64, UserOrKernelBuffer& buffer, s ch = '\r'; data[i] = ch; } - return (ssize_t)data_size; + return data_size; }); } - if (nwritten < 0) - return KResult((ErrnoCode)-nwritten); - if (nwritten > 0 || need_evaluate_block_conditions) + if ((!result.is_error() && result.value() > 0) || need_evaluate_block_conditions) evaluate_block_conditions(); - return (size_t)nwritten; + return result; } KResultOr TTY::write(FileDescription&, u64, const UserOrKernelBuffer& buffer, size_t size) @@ -93,8 +91,8 @@ KResultOr TTY::write(FileDescription&, u64, const UserOrKernelBuffer& bu return EINTR; } - const size_t num_chars = 256; - ssize_t nread = buffer.read_buffered((size_t)size, [&](const u8* data, size_t buffer_bytes) { + constexpr size_t num_chars = 256; + return buffer.read_buffered(size, [&](u8 const* data, size_t buffer_bytes) { u8 modified_data[num_chars * 2]; size_t extra_chars = 0; for (size_t i = 0; i < buffer_bytes; ++i) { @@ -106,10 +104,8 @@ KResultOr TTY::write(FileDescription&, u64, const UserOrKernelBuffer& bu modified_data[i + extra_chars] = ch; } on_tty_write(UserOrKernelBuffer::for_kernel_buffer(modified_data), buffer_bytes + extra_chars); - return (ssize_t)buffer_bytes; + return buffer_bytes; }); - - return nread; } bool TTY::can_read(const FileDescription&, size_t) const -- cgit v1.2.3