From 49ef21d6505c76fb26d50c99934e801ba6016049 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 17 Jan 2021 15:13:32 +0000 Subject: MultiVersionProtocol: fix two crashes First one: add missing exception handler in ProcessProtocolIn Second: remove faulty logic dealing with incomplete packets. `a_Data = a_Data.substr(m_Buffer.GetUsedSpace() - m_Buffer.GetReadableSpace());` was incorrect; it attempted to apply a length derived from m_Buffer to an unrelated a_Data. Its purpose was to give cProtocol the data the client sent, minus initial handshake bytes. However, we can use the knowledge that during initial handshake, there is no encryption and every byte can be written unchanged into m_Buffer, to just call cProtocol with a data length of zero. This will cause it to parse from m_Buffer - wherein we have already written everything the client sent - with no a_Data manipulation needed. Additionally, removed UnsupportedButPingableProtocolException (use of exception as control flow) and encode this state as m_Protocol == nullptr, id est "no protocol for this unsupported version", which is then handled by cMultiVersionProtocol itself. --- src/Protocol/ProtocolRecognizer.h | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/Protocol/ProtocolRecognizer.h') diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index b19adaefe..56d5645c0 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -47,22 +47,17 @@ public: private: - /** Handles data reception in a newly-created client handle that doesn't yet have known protocol. + /** Handles data reception in a newly-created client handle that doesn't yet have a known protocol. a_Data contains a view of data that were just received. - Calls TryRecognizeProtocol to populate m_Protocol, and transitions to another mode depending on success. */ + Tries to recognize a protocol, populate m_Protocol, and transitions to another mode depending on success. */ void HandleIncomingDataInRecognitionStage(cClientHandle & a_Client, std::string_view a_Data); /** Handles and responds to unsupported clients sending pings. */ - void HandleIncomingDataInOldPingResponseStage(cClientHandle & a_Client, const std::string_view a_Data); - - /* Tries to recognize a protocol based on a_Data and m_Buffer contents. - a_Data is replaced with a sub-view, with handshake packet removed. */ - void TryRecognizeProtocol(cClientHandle & a_Client, std::string_view & a_Data); + void HandleIncomingDataInOldPingResponseStage(cClientHandle & a_Client, std::string_view a_Data); /** Tries to recognize a protocol in the lengthed family (1.7+), based on m_Buffer. - The packet length and type have already been read, type is 0. Returns a cProtocol_XXX instance if recognized. */ - std::unique_ptr TryRecognizeLengthedProtocol(cClientHandle & a_Client, std::string_view & a_Data); + std::unique_ptr TryRecognizeLengthedProtocol(cClientHandle & a_Client, std::string_view a_Data); /** Sends one packet inside a cByteBuffer. This is used only when handling an outdated server ping. */ -- cgit v1.2.3