From 06a44ac2ffb31585f4b2845d425b2f3ef8e06b97 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Mon, 17 Jul 2023 16:39:41 +1200 Subject: [PATCH] LibCore: Error on a fail to write in Command::write_lines --- Userland/Libraries/LibCore/Command.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibCore/Command.cpp b/Userland/Libraries/LibCore/Command.cpp index e7e8d257f6..2e6420c595 100644 --- a/Userland/Libraries/LibCore/Command.cpp +++ b/Userland/Libraries/LibCore/Command.cpp @@ -74,17 +74,17 @@ ErrorOr Command::write_lines(Span lines) struct sigaction old_action_handler; TRY(Core::System::sigaction(SIGPIPE, &action_handler, &old_action_handler)); - for (DeprecatedString const& line : lines) { - if (m_stdin->write_until_depleted(DeprecatedString::formatted("{}\n", line).bytes()).is_error()) - break; - } + auto close_stdin = ScopeGuard([this, &old_action_handler] { + // Ensure that the input stream ends here, whether we were able to write all lines or not + m_stdin->close(); - // Ensure that the input stream ends here, whether we were able to write all lines or not - m_stdin->close(); + // It's not really a problem if this signal failed + if (sigaction(SIGPIPE, &old_action_handler, nullptr) < 0) + perror("sigaction"); + }); - // It's not really a problem if this signal failed - if (sigaction(SIGPIPE, &old_action_handler, nullptr) < 0) - perror("sigaction"); + for (DeprecatedString const& line : lines) + TRY(m_stdin->write_until_depleted(DeprecatedString::formatted("{}\n", line).bytes())); return {}; }