From e98120162b779d7a5c63ebd1a44c16ede0d309f7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 3 Jul 2024 20:46:54 +0200 Subject: [PATCH] ssh/Connection: add `noexcept` to SendPacket() This method cannot throw; it only enqueues the buffer. Adding `noexcept` allows doing the same with many other functions. --- src/SessionChannel.cxx | 2 +- src/SessionChannel.hxx | 8 +++----- src/ssh/CConnection.cxx | 21 ++++++--------------- src/ssh/CConnection.hxx | 5 +---- src/ssh/Channel.cxx | 8 ++++---- src/ssh/Channel.hxx | 11 ++++------- src/ssh/Connection.cxx | 8 ++++---- src/ssh/Connection.hxx | 7 ++----- 8 files changed, 25 insertions(+), 45 deletions(-) diff --git a/src/SessionChannel.cxx b/src/SessionChannel.cxx index 095a62e..133e239 100644 --- a/src/SessionChannel.cxx +++ b/src/SessionChannel.cxx @@ -56,7 +56,7 @@ SessionChannel::~SessionChannel() noexcept } void -SessionChannel::CloseIfInactive() +SessionChannel::CloseIfInactive() noexcept { if (!IsActive()) Close(); diff --git a/src/SessionChannel.hxx b/src/SessionChannel.hxx index 7be920c..37945d9 100644 --- a/src/SessionChannel.hxx +++ b/src/SessionChannel.hxx @@ -66,11 +66,9 @@ private: /** * Call SSH::Channel::SendEof() if all data sources have ended. * - * Throws on error. - * * @return true if SendEof() was called */ - bool MaybeSendEof() { + bool MaybeSendEof() noexcept { if (stdout_pipe.IsDefined() || stderr_pipe.IsDefined() || tty.IsDefined()) return false; @@ -78,12 +76,12 @@ private: return true; } - void CloseIfInactive(); + void CloseIfInactive() noexcept; /** * Combination of MaybeSendEof() and CloseIfInactive(). */ - void MaybeSendEofAndClose() { + void MaybeSendEofAndClose() noexcept { if (MaybeSendEof()) CloseIfInactive(); } diff --git a/src/ssh/CConnection.cxx b/src/ssh/CConnection.cxx index 422ae3d..a4d351e 100644 --- a/src/ssh/CConnection.cxx +++ b/src/ssh/CConnection.cxx @@ -154,7 +154,7 @@ IsOpeningChannel(const Channel &channel) noexcept } void -CConnection::CloseChannel(Channel &channel) +CConnection::CloseChannel(Channel &channel) noexcept { assert(!IsTombstoneChannel(channel)); assert(!IsOpeningChannel(channel)); @@ -276,15 +276,10 @@ CConnection::AsyncChannelOpenSuccess(Channel &channel) noexcept opening.cancel_ptr = {}; delete &opening; - try { - SendPacket(MakeChannelOpenConfirmation(channel.GetPeerChannel(), - local_channel, - MAXIMUM_PACKET_SIZE, - channel)); - } catch (...) { - OnBufferedError(std::current_exception()); - return; - } + SendPacket(MakeChannelOpenConfirmation(channel.GetPeerChannel(), + local_channel, + MAXIMUM_PACKET_SIZE, + channel)); channels[local_channel] = &channel; } @@ -303,11 +298,7 @@ CConnection::AsyncChannelOpenFailure(ChannelInit init, opening.cancel_ptr = {}; delete &opening; - try { - SendPacket(MakeChannelOpenFailure(init.peer_channel, reason_code, description)); - } catch (...) { - OnBufferedError(std::current_exception()); - } + SendPacket(MakeChannelOpenFailure(init.peer_channel, reason_code, description)); } inline void diff --git a/src/ssh/CConnection.hxx b/src/ssh/CConnection.hxx index c9f006c..1ca1f86 100644 --- a/src/ssh/CConnection.hxx +++ b/src/ssh/CConnection.hxx @@ -64,10 +64,7 @@ public: ChannelFactory &factory, CancellablePointer &cancel_ptr); - /** - * Throws on error. - */ - void CloseChannel(Channel &channel); + void CloseChannel(Channel &channel) noexcept; /** * Exception class to be thrown from inside CreateChannel(), diff --git a/src/ssh/Channel.cxx b/src/ssh/Channel.cxx index 75234fa..4ed8817 100644 --- a/src/ssh/Channel.cxx +++ b/src/ssh/Channel.cxx @@ -22,7 +22,7 @@ Channel::Channel(CConnection &_connection, ChannelInit init, Channel::~Channel() noexcept = default; void -Channel::Close() +Channel::Close() noexcept { connection.CloseChannel(*this); } @@ -36,7 +36,7 @@ Channel::ConsumeReceiveWindow(std::size_t nbytes) noexcept } void -Channel::SendWindowAdjust(uint_least32_t nbytes) +Channel::SendWindowAdjust(uint_least32_t nbytes) noexcept { assert(nbytes > 0); assert(nbytes <= SIZE_MAX - receive_window); @@ -81,7 +81,7 @@ Channel::SendStderr(std::span src) } void -Channel::SendEof() +Channel::SendEof() noexcept { PacketSerializer s{MessageNumber::CHANNEL_EOF}; s.WriteU32(GetPeerChannel()); @@ -89,7 +89,7 @@ Channel::SendEof() } void -Channel::SendExitStatus(uint_least32_t exit_status) +Channel::SendExitStatus(uint_least32_t exit_status) noexcept { auto s = MakeChannelReqest(GetPeerChannel(), "exit-status"sv, false); s.WriteU32(exit_status); diff --git a/src/ssh/Channel.hxx b/src/ssh/Channel.hxx index 13fda00..a2ba255 100644 --- a/src/ssh/Channel.hxx +++ b/src/ssh/Channel.hxx @@ -68,18 +68,15 @@ public: return send_window; } - /** - * Throws on error. - */ - void Close(); + void Close() noexcept; - void SendWindowAdjust(uint_least32_t nbytes); + void SendWindowAdjust(uint_least32_t nbytes) noexcept; void SendData(std::span src); void SendExtendedData(ChannelExtendedDataType data_type, std::span src); void SendStderr(std::span src); - void SendEof(); - void SendExitStatus(uint_least32_t exit_status); + void SendEof() noexcept; + void SendExitStatus(uint_least32_t exit_status) noexcept; void SendExitSignal(std::string_view signal_name, bool core_dumped, std::string_view error_message); diff --git a/src/ssh/Connection.cxx b/src/ssh/Connection.cxx index 038ab66..678c8b4 100644 --- a/src/ssh/Connection.cxx +++ b/src/ssh/Connection.cxx @@ -72,7 +72,7 @@ Connection::IsEncrypted() const noexcept } inline void -Connection::SendPacket(std::span src) +Connection::SendPacket(std::span src) noexcept { if (metrics != nullptr) { ++metrics->packets_sent; @@ -86,7 +86,7 @@ Connection::SendPacket(std::span src) } void -Connection::SendPacket(PacketSerializer &&s) +Connection::SendPacket(PacketSerializer &&s) noexcept { const auto *send_cipher = output.GetCipher(); SendPacket(s.Finish(send_cipher @@ -108,9 +108,9 @@ Connection::DoDisconnect(DisconnectReasonCode reason_code, std::string_view msg) { OnDisconnecting(reason_code, msg); - try { - SendPacket(MakeDisconnect(reason_code, msg)); + SendPacket(MakeDisconnect(reason_code, msg)); + try { /* attempt to flush the DISCONNECT packet immediately before we close the socket */ switch (output.Flush()) { diff --git a/src/ssh/Connection.hxx b/src/ssh/Connection.hxx index 5195418..06cbad3 100644 --- a/src/ssh/Connection.hxx +++ b/src/ssh/Connection.hxx @@ -135,13 +135,10 @@ protected: authenticated = true; } - void SendPacket(std::span src); + void SendPacket(std::span src) noexcept; public: - /** - * Throws on error. - */ - void SendPacket(PacketSerializer &&s); + void SendPacket(PacketSerializer &&s) noexcept; void SendPacket(MessageNumber msg, std::span src);