From 204ffae901852ae09eb4c092c3ca5ba9e2ac3c69 Mon Sep 17 00:00:00 2001 From: maksis Date: Sun, 31 Mar 2019 11:53:14 +0300 Subject: [PATCH] Fixes for https://github.com/airdcpp/airgit/issues/34#issuecomment-478320331 Also fixes possible crashes --- airdcpp-webapi/api/ApiSettingItem.cpp | 4 ++-- airdcpp-webapi/api/PrivateChatApi.cpp | 6 +++--- airdcpp-webapi/api/SessionApi.cpp | 3 +++ airdcpp-webapi/api/base/ApiModule.cpp | 2 +- airdcpp/airdcpp/Client.cpp | 4 ++-- airdcpp/airdcpp/DirectoryListing.cpp | 14 ++++++-------- airdcpp/airdcpp/PrivateChatManager.cpp | 24 ++++++++++++------------ airdcpp/airdcpp/PrivateChatManager.h | 3 ++- airdcpp/airdcpp/QueueItem.cpp | 24 ++++++++++++------------ airdcpp/airdcpp/QueueManager.cpp | 5 +---- airdcpp/airdcpp/SharePathValidator.cpp | 4 ++-- airdcpp/airdcpp/UserConnection.cpp | 2 +- 12 files changed, 47 insertions(+), 48 deletions(-) diff --git a/airdcpp-webapi/api/ApiSettingItem.cpp b/airdcpp-webapi/api/ApiSettingItem.cpp index 94a7bcf474..ae1981a866 100644 --- a/airdcpp-webapi/api/ApiSettingItem.cpp +++ b/airdcpp-webapi/api/ApiSettingItem.cpp @@ -364,8 +364,8 @@ namespace webserver { } else if (si.key == SettingsManager::BIND_ADDRESS || si.key == SettingsManager::BIND_ADDRESS6) { auto bindAddresses = AirUtil::getBindAdapters(si.key == SettingsManager::BIND_ADDRESS6); for (const auto& adapter : bindAddresses) { - auto name = adapter.ip + (!adapter.adapterName.empty() ? " (" + adapter.adapterName + ")" : Util::emptyString); - ret.emplace_back(EnumOption({ adapter.ip, name })); + auto title = adapter.ip + (!adapter.adapterName.empty() ? " (" + adapter.adapterName + ")" : Util::emptyString); + ret.emplace_back(EnumOption({ adapter.ip, title })); } } else if (si.key == SettingsManager::MAPPER) { auto mappers = ConnectivityManager::getInstance()->getMappers(false); diff --git a/airdcpp-webapi/api/PrivateChatApi.cpp b/airdcpp-webapi/api/PrivateChatApi.cpp index 377e621436..ea8f51f220 100644 --- a/airdcpp-webapi/api/PrivateChatApi.cpp +++ b/airdcpp-webapi/api/PrivateChatApi.cpp @@ -93,13 +93,13 @@ namespace webserver { api_return PrivateChatApi::handlePostChat(ApiRequest& aRequest) { auto user = Deserializer::deserializeHintedUser(aRequest.getRequestBody()); - auto chat = PrivateChatManager::getInstance()->addChat(user, false); - if (!chat) { + auto res = PrivateChatManager::getInstance()->addChat(user, false); + if (!res.second) { aRequest.setResponseErrorStr("Chat session exists"); return websocketpp::http::status_code::conflict; } - aRequest.setResponseBody(serializeChat(chat)); + aRequest.setResponseBody(serializeChat(res.first)); return websocketpp::http::status_code::ok; } diff --git a/airdcpp-webapi/api/SessionApi.cpp b/airdcpp-webapi/api/SessionApi.cpp index 0df04ac1ee..5368ed0170 100644 --- a/airdcpp-webapi/api/SessionApi.cpp +++ b/airdcpp-webapi/api/SessionApi.cpp @@ -123,6 +123,9 @@ namespace webserver { aRequest.setResponseErrorStr(e.what()); return websocketpp::http::status_code::bad_request; } + } else { + JsonUtil::throwError("grant_type", JsonUtil::ERROR_INVALID, "Invalid grant_type"); + return websocketpp::http::status_code::bad_request; } dcassert(session); diff --git a/airdcpp-webapi/api/base/ApiModule.cpp b/airdcpp-webapi/api/base/ApiModule.cpp index 2107208fbf..4930e39828 100644 --- a/airdcpp-webapi/api/base/ApiModule.cpp +++ b/airdcpp-webapi/api/base/ApiModule.cpp @@ -38,7 +38,7 @@ namespace webserver { if (aPathTokens.size() < params.size()) { return nullopt; } - } else if (method != METHOD_FORWARD && aPathTokens.size() != params.size()) { + } else if (aPathTokens.size() != params.size()) { return nullopt; } diff --git a/airdcpp/airdcpp/Client.cpp b/airdcpp/airdcpp/Client.cpp index e24468d9ab..ed72f20f31 100644 --- a/airdcpp/airdcpp/Client.cpp +++ b/airdcpp/airdcpp/Client.cpp @@ -369,7 +369,7 @@ bool Client::sendMessage(const string& aMessage, string& error_, bool aThirdPers auto error = ClientManager::getInstance()->outgoingHubMessageHook.runHooksError(aMessage, aThirdPerson, *this); if (error) { - error_ = error->formatError(error); + error_ = ActionHookRejection::formatError(error); return false; } @@ -389,7 +389,7 @@ bool Client::sendPrivateMessage(const OnlineUserPtr& aUser, const string& aMessa auto error = ClientManager::getInstance()->outgoingPrivateMessageHook.runHooksError(aMessage, aThirdPerson, HintedUser(aUser->getUser(), aUser->getHubUrl()), aEcho); if (error) { - error_ = error->formatError(error); + error_ = ActionHookRejection::formatError(error); return false; } diff --git a/airdcpp/airdcpp/DirectoryListing.cpp b/airdcpp/airdcpp/DirectoryListing.cpp index 1f726369f4..2a2b780bc7 100644 --- a/airdcpp/airdcpp/DirectoryListing.cpp +++ b/airdcpp/airdcpp/DirectoryListing.cpp @@ -1011,15 +1011,13 @@ void DirectoryListing::onLoadingFinished(int64_t aStartTime, const string& aBase dir = root; } - if (dir) { - dir->setLoading(false); - if (!aBackgroundTask) { - updateCurrentLocation(dir); - read = false; - } - - onStateChanged(); + dir->setLoading(false); + if (!aBackgroundTask) { + updateCurrentLocation(dir); + read = false; } + + onStateChanged(); fire(DirectoryListingListener::LoadingFinished(), aStartTime, dir->getAdcPath(), aBackgroundTask); } diff --git a/airdcpp/airdcpp/PrivateChatManager.cpp b/airdcpp/airdcpp/PrivateChatManager.cpp index 11c2c320c9..dae98787fe 100644 --- a/airdcpp/airdcpp/PrivateChatManager.cpp +++ b/airdcpp/airdcpp/PrivateChatManager.cpp @@ -52,23 +52,22 @@ PrivateChatManager::~PrivateChatManager() noexcept { ConnectionManager::getInstance()->disconnect(); } -PrivateChatPtr PrivateChatManager::addChat(const HintedUser& aUser, bool aReceivedMessage) noexcept { - if (getChat(aUser.user)) { - return nullptr; - } - +pair PrivateChatManager::addChat(const HintedUser& aUser, bool aReceivedMessage) noexcept { PrivateChatPtr chat; auto user = ClientManager::getInstance()->checkOnlineUrl(aUser); { WLock l(cs); - chat = std::make_shared(user, getPMConn(user.user)); - chats.emplace(user.user, chat); + auto res = chats.emplace(user.user, std::make_shared(user, getPMConn(user.user))); + chat = res.first->second; + if (!res.second) { + return { chat, false }; + } } fire(PrivateChatManagerListener::ChatCreated(), chat, aReceivedMessage); - return chat; + return { chat, true }; } PrivateChatPtr PrivateChatManager::getChat(const UserPtr& aUser) const noexcept { @@ -171,19 +170,20 @@ void PrivateChatManager::onPrivateMessage(const ChatMessagePtr& aMessage) { } } - auto c = aMessage->getFrom()->getClient(); if (wndCnt > 200) { DisconnectCCPM(user); return; } + + const auto client = aMessage->getFrom()->getClient(); const auto& identity = aMessage->getReplyTo()->getIdentity(); if ((identity.isBot() && !SETTING(POPUP_BOT_PMS)) || (identity.isHub() && !SETTING(POPUP_HUB_PMS))) { - c->addLine(STRING(PRIVATE_MESSAGE_FROM) + " " + identity.getNick() + ": " + aMessage->format()); + client->addLine(STRING(PRIVATE_MESSAGE_FROM) + " " + identity.getNick() + ": " + aMessage->format()); return; } - auto chat = addChat(HintedUser(user, aMessage->getReplyTo()->getClient()->getHubUrl()), true); + auto chat = addChat(HintedUser(user, client->getHubUrl()), true).first; chat->handleMessage(aMessage); if (ActivityManager::getInstance()->isAway() && !myPM && (!SETTING(NO_AWAYMSG_TO_BOTS) || !user->isSet(User::BOT))) { @@ -191,7 +191,7 @@ void PrivateChatManager::onPrivateMessage(const ChatMessagePtr& aMessage) { aMessage->getFrom()->getIdentity().getParams(params, "user", false); string error; - chat->sendMessage(ActivityManager::getInstance()->getAwayMessage(c->get(HubSettings::AwayMsg), params), error, false); + chat->sendMessage(ActivityManager::getInstance()->getAwayMessage(client->get(HubSettings::AwayMsg), params), error, false); } } diff --git a/airdcpp/airdcpp/PrivateChatManager.h b/airdcpp/airdcpp/PrivateChatManager.h index 8dc547190a..bb4401b619 100644 --- a/airdcpp/airdcpp/PrivateChatManager.h +++ b/airdcpp/airdcpp/PrivateChatManager.h @@ -42,7 +42,8 @@ namespace dcpp { PrivateChatManager() noexcept; ~PrivateChatManager() noexcept; - PrivateChatPtr addChat(const HintedUser& user, bool aReceivedMessage) noexcept; + // Returns the chat session and boolean whether the session was newly created + pair addChat(const HintedUser& user, bool aReceivedMessage) noexcept; PrivateChatPtr getChat(const UserPtr& aUser) const noexcept; void DisconnectCCPM(const UserPtr& aUser); diff --git a/airdcpp/airdcpp/QueueItem.cpp b/airdcpp/airdcpp/QueueItem.cpp index 2199527552..35a207c007 100644 --- a/airdcpp/airdcpp/QueueItem.cpp +++ b/airdcpp/airdcpp/QueueItem.cpp @@ -231,36 +231,36 @@ string QueueItem::getListName() const noexcept { /* INTERNAL */ uint8_t QueueItem::getMaxSegments(int64_t aFileSize) noexcept { - uint8_t maxSegments = 1; + uint8_t ret = 1; if(SETTING(SEGMENTS_MANUAL)) { - maxSegments = min((uint8_t)SETTING(NUMBER_OF_SEGMENTS), (uint8_t)10); + ret = min((uint8_t)SETTING(NUMBER_OF_SEGMENTS), (uint8_t)10); } else { if ((aFileSize >= 2*1048576) && (aFileSize < 15*1048576)) { - maxSegments = 2; + ret = 2; } else if((aFileSize >= (int64_t)15*1048576) && (aFileSize < (int64_t)30*1048576)) { - maxSegments = 3; + ret = 3; } else if((aFileSize >= (int64_t)30*1048576) && (aFileSize < (int64_t)60*1048576)) { - maxSegments = 4; + ret = 4; } else if((aFileSize >= (int64_t)60*1048576) && (aFileSize < (int64_t)120*1048576)) { - maxSegments = 5; + ret = 5; } else if((aFileSize >= (int64_t)120*1048576) && (aFileSize < (int64_t)240*1048576)) { - maxSegments = 6; + ret = 6; } else if((aFileSize >= (int64_t)240*1048576) && (aFileSize < (int64_t)480*1048576)) { - maxSegments = 7; + ret = 7; } else if((aFileSize >= (int64_t)480*1048576) && (aFileSize < (int64_t)960*1048576)) { - maxSegments = 8; + ret = 8; } else if((aFileSize >= (int64_t)960*1048576) && (aFileSize < (int64_t)1920*1048576)) { - maxSegments = 9; + ret = 9; } else if(aFileSize >= (int64_t)1920*1048576) { - maxSegments = 10; + ret = 10; } } #ifdef _DEBUG return 88; #else - return maxSegments; + return ret; #endif } diff --git a/airdcpp/airdcpp/QueueManager.cpp b/airdcpp/airdcpp/QueueManager.cpp index 0565c69cde..f237a3b0d4 100644 --- a/airdcpp/airdcpp/QueueManager.cpp +++ b/airdcpp/airdcpp/QueueManager.cpp @@ -3457,10 +3457,7 @@ void QueueManager::handleBundleUpdate(QueueToken aBundleToken) noexcept { if (b) { if (b->isSet(Bundle::FLAG_UPDATE_SIZE)) { - if (b->isSet(Bundle::FLAG_UPDATE_SIZE)) { - fire(QueueManagerListener::BundleSize(), b); - } - + fire(QueueManagerListener::BundleSize(), b); DownloadManager::getInstance()->sendSizeUpdate(b); } diff --git a/airdcpp/airdcpp/SharePathValidator.cpp b/airdcpp/airdcpp/SharePathValidator.cpp index 020f264567..eb42a67b0a 100644 --- a/airdcpp/airdcpp/SharePathValidator.cpp +++ b/airdcpp/airdcpp/SharePathValidator.cpp @@ -211,7 +211,7 @@ void SharePathValidator::validate(FileFindIter& aIter, const string& aPath, bool auto error = directoryValidationHook.runHooksError(aPath); if (error) { - throw ShareException(error->formatError(error)); + throw ShareException(ActionHookRejection::formatError(error)); } } else { auto size = aIter->getSize(); @@ -219,7 +219,7 @@ void SharePathValidator::validate(FileFindIter& aIter, const string& aPath, bool auto error = fileValidationHook.runHooksError(aPath, size); if (error) { - throw ShareException(error->formatError(error)); + throw ShareException(ActionHookRejection::formatError(error)); } } } diff --git a/airdcpp/airdcpp/UserConnection.cpp b/airdcpp/airdcpp/UserConnection.cpp index 2eff5d74ec..492f9fd098 100644 --- a/airdcpp/airdcpp/UserConnection.cpp +++ b/airdcpp/airdcpp/UserConnection.cpp @@ -236,7 +236,7 @@ void UserConnection::inf(bool withToken, int mcnSlots) { bool UserConnection::pm(const string& aMessage, string& error_, bool aThirdPerson) { auto error = ClientManager::getInstance()->outgoingPrivateMessageHook.runHooksError(aMessage, aThirdPerson, getHintedUser(), true); if (error) { - error_ = error->formatError(error); + error_ = ActionHookRejection::formatError(error); return false; }