From 4ee7abb7116eb48520d0378d936048edca8f1037 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Thu, 12 Dec 2024 09:03:40 +0000 Subject: [PATCH] fix: use exceptions to bail out of parseheader --- src/dpp/discordclient.cpp | 9 +++++---- src/dpp/wsclient.cpp | 12 ++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/dpp/discordclient.cpp b/src/dpp/discordclient.cpp index f45ed0de93..f232f0c278 100644 --- a/src/dpp/discordclient.cpp +++ b/src/dpp/discordclient.cpp @@ -212,14 +212,17 @@ bool discord_client::handle_frame(const std::string &buffer, ws_opcode opcode) case Z_STREAM_ERROR: this->error(err_compression_stream); this->close(); + return false; return true; case Z_DATA_ERROR: this->error(err_compression_data); this->close(); + return false; return true; case Z_MEM_ERROR: this->error(err_compression_memory); this->close(); + return false; return true; case Z_OK: this->decompressed.append((const char*)decomp_buffer.data(), have); @@ -353,9 +356,8 @@ bool discord_client::handle_frame(const std::string &buffer, ws_opcode opcode) } break; case ft_reconnect: - log(dpp::ll_debug, "Reconnection requested, closing socket " + sessionid); message_queue.clear(); - this->close(); + throw dpp::connection_exception("Reconnection requested, closing socket " + sessionid); break; /* Heartbeat ack */ case ft_heartbeat_ack: @@ -369,8 +371,7 @@ bool discord_client::handle_frame(const std::string &buffer, ws_opcode opcode) case ft_resume: case ft_request_guild_members: case ft_request_soundboard_sounds: - log(dpp::ll_error, "Received invalid opcode on websocket for session " + sessionid); - break; + throw dpp::connection_exception("Received invalid opcode on websocket for session " + sessionid); } } return true; diff --git a/src/dpp/wsclient.cpp b/src/dpp/wsclient.cpp index d4e6d749fb..2180ec5f5b 100644 --- a/src/dpp/wsclient.cpp +++ b/src/dpp/wsclient.cpp @@ -200,7 +200,13 @@ bool websocket_client::handle_buffer(std::string& buffer) } } else if (state == CONNECTED) { /* Process packets until we can't (buffer will erase data until parseheader returns false) */ - while (this->parseheader(buffer)) { } + try { + while (this->parseheader(buffer)) { } + } + catch (const std::exception &e) { + log(ll_debug, "Receiving exception: " + std::string(e.what())); + return false; + } } return true; @@ -274,7 +280,9 @@ bool websocket_client::parseheader(std::string& data) handle_ping(data.substr(payloadstartoffset, len)); } else if ((opcode & ~WS_FINBIT) != OP_PONG) { /* Otherwise, handle everything else apart from a PONG. */ /* Pass this frame to the deriving class */ - this->handle_frame(data.substr(payloadstartoffset, len), static_cast(opcode & ~WS_FINBIT)); + if (!this->handle_frame(data.substr(payloadstartoffset, len), static_cast(opcode & ~WS_FINBIT))) { + return false; + } } /* Remove this frame from the input buffer */