From 5d7ba3f76862cd561b295b79125a68dc4acf20df Mon Sep 17 00:00:00 2001 From: Edwin Mons Date: Thu, 4 Oct 2018 15:52:48 +0200 Subject: Prevent connect on active sluift clients and components Connect should not be called twice on clients or compoments without disconnecting first. While this worked in some cases, mixing connect options, specifically first trying without, and then with a bosh_url, would lead to asserts being triggered. The connect logic now checks whether there's already a connection in progress or established, and raises a Lua exception early if there is. Test-Information: Tested on macOS 10.13 Connected components and clients with invalid and valid hosts, and with valid and invalid bosh_urls, all with and without disconnecting in between attempts. No asserts were triggered, and appropriate Lua exceptions were thrown. Change-Id: I6b91b57945844bce7fce0073e5d0fe199ab815d5 diff --git a/Sluift/SluiftClient.cpp b/Sluift/SluiftClient.cpp index e8c43c9..5243ed0 100644 --- a/Sluift/SluiftClient.cpp +++ b/Sluift/SluiftClient.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2017 Isode Limited. + * Copyright (c) 2013-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -48,6 +48,9 @@ SluiftClient::~SluiftClient() { } void SluiftClient::connect() { + if (client->isActive()) { + throw Lua::Exception("Client is already connecting or connected"); + } rosterReceived = false; blockListReceived = false; disconnectedError = boost::optional(); diff --git a/Sluift/SluiftComponent.cpp b/Sluift/SluiftComponent.cpp index e1d1738..126562e 100644 --- a/Sluift/SluiftComponent.cpp +++ b/Sluift/SluiftComponent.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2016 Isode Limited. + * Copyright (c) 2014-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -41,6 +41,9 @@ SluiftComponent::~SluiftComponent() { } void SluiftComponent::connect(const std::string& host, int port) { + if (component->isActive()) { + throw Lua::Exception("Component is already connecting or connected"); + } disconnectedError = boost::optional(); component->connect(host, port); } diff --git a/Swiften/Component/CoreComponent.cpp b/Swiften/Component/CoreComponent.cpp index 3896bdd..5b2277b 100644 --- a/Swiften/Component/CoreComponent.cpp +++ b/Swiften/Component/CoreComponent.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -166,4 +166,12 @@ void CoreComponent::sendData(const std::string& data) { sessionStream_->writeData(data); } +bool CoreComponent::isActive() const { + return session_ || connector_; +} + +bool CoreComponent::isAvailable() const { + return stanzaChannel_->isAvailable(); +} + } diff --git a/Swiften/Component/CoreComponent.h b/Swiften/Component/CoreComponent.h index f673643..1a487d2 100644 --- a/Swiften/Component/CoreComponent.h +++ b/Swiften/Component/CoreComponent.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2017 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -61,9 +61,18 @@ namespace Swift { return stanzaChannel_; } - bool isAvailable() const { - return stanzaChannel_->isAvailable(); - } + /** + * Checks whether the component is connected to the server, + * and stanzas can be sent. + */ + bool isAvailable() const; + + /** + * Checks whether the component is active. + * + * A component is active when it is connected or connecting to the server. + */ + bool isActive() const; /** * Returns the JID of the component -- cgit v0.10.2-6-g49f6