Skip to content

Commit

Permalink
ssh/Connection: add noexcept to SendPacket()
Browse files Browse the repository at this point in the history
This method cannot throw; it only enqueues the buffer.  Adding
`noexcept` allows doing the same with many other functions.
  • Loading branch information
MaxKellermann committed Jul 3, 2024
1 parent 62f4ca0 commit e981201
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/SessionChannel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ SessionChannel::~SessionChannel() noexcept
}

void
SessionChannel::CloseIfInactive()
SessionChannel::CloseIfInactive() noexcept
{
if (!IsActive())
Close();
Expand Down
8 changes: 3 additions & 5 deletions src/SessionChannel.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,22 @@ 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;

SendEof();
return true;
}

void CloseIfInactive();
void CloseIfInactive() noexcept;

/**
* Combination of MaybeSendEof() and CloseIfInactive().
*/
void MaybeSendEofAndClose() {
void MaybeSendEofAndClose() noexcept {
if (MaybeSendEof())
CloseIfInactive();
}
Expand Down
21 changes: 6 additions & 15 deletions src/ssh/CConnection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/ssh/CConnection.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions src/ssh/Channel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Channel::Channel(CConnection &_connection, ChannelInit init,
Channel::~Channel() noexcept = default;

void
Channel::Close()
Channel::Close() noexcept
{
connection.CloseChannel(*this);
}
Expand All @@ -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);
Expand Down Expand Up @@ -81,15 +81,15 @@ Channel::SendStderr(std::span<const std::byte> src)
}

void
Channel::SendEof()
Channel::SendEof() noexcept
{
PacketSerializer s{MessageNumber::CHANNEL_EOF};
s.WriteU32(GetPeerChannel());
connection.SendPacket(std::move(s));
}

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);
Expand Down
11 changes: 4 additions & 7 deletions src/ssh/Channel.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<const std::byte> src);
void SendExtendedData(ChannelExtendedDataType data_type,
std::span<const std::byte> src);
void SendStderr(std::span<const std::byte> 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);

Expand Down
8 changes: 4 additions & 4 deletions src/ssh/Connection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Connection::IsEncrypted() const noexcept
}

inline void
Connection::SendPacket(std::span<const std::byte> src)
Connection::SendPacket(std::span<const std::byte> src) noexcept
{
if (metrics != nullptr) {
++metrics->packets_sent;
Expand All @@ -86,7 +86,7 @@ Connection::SendPacket(std::span<const std::byte> src)
}

void
Connection::SendPacket(PacketSerializer &&s)
Connection::SendPacket(PacketSerializer &&s) noexcept
{
const auto *send_cipher = output.GetCipher();
SendPacket(s.Finish(send_cipher
Expand All @@ -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()) {
Expand Down
7 changes: 2 additions & 5 deletions src/ssh/Connection.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,10 @@ protected:
authenticated = true;
}

void SendPacket(std::span<const std::byte> src);
void SendPacket(std::span<const std::byte> src) noexcept;

public:
/**
* Throws on error.
*/
void SendPacket(PacketSerializer &&s);
void SendPacket(PacketSerializer &&s) noexcept;

void SendPacket(MessageNumber msg, std::span<const std::byte> src);

Expand Down

0 comments on commit e981201

Please sign in to comment.