From 30639ed8dbb419890eab5a0b46d3a78896c7f22d Mon Sep 17 00:00:00 2001 From: Edwin Mons Date: Tue, 30 Oct 2018 07:24:24 +0000 Subject: Fix various uses of numeric_cast in UI bits Apart from QtUI bits, this addresses use of uncaught numeric cast exceptions in message count handling, the spell parser and the MacOS idle querier. The WindowsServicePrincipalName logic previously had an issue where using ports from 32768 onwards would result in a bad_numeric_cast exception to be thrown. This has been addressed at the same time as the uncaught exceptions, and all ports should work now. The tags file has been extended to ignore more files. Change-Id: I73ced35f06517bee5c58f990d20fa437b40ac84e diff --git a/.gitignore b/.gitignore index 2fd9953..d28db8b 100644 --- a/.gitignore +++ b/.gitignore @@ -84,3 +84,4 @@ compile_commands.json *.vcxproj.filters *.vcxproj.user *.sdf +tags diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/.gitignore b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/.gitignore index aca6fe1..ef95cc1 100644 --- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/.gitignore +++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/.gitignore @@ -1,4 +1,5 @@ EchoBot? +EchoBot0x *.cpp.xml *.h.xml EchoComponent diff --git a/SwifTools/Dock/Dock.h b/SwifTools/Dock/Dock.h index 6120445..362aabc 100644 --- a/SwifTools/Dock/Dock.h +++ b/SwifTools/Dock/Dock.h @@ -1,11 +1,13 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once +#include + namespace Swift { @@ -13,6 +15,6 @@ namespace Swift { public: virtual ~Dock(); - virtual void setNumberOfPendingMessages(int i) = 0; + virtual void setNumberOfPendingMessages(size_t i) = 0; }; } diff --git a/SwifTools/Dock/MacOSXDock.h b/SwifTools/Dock/MacOSXDock.h index 6b33506..ef85a88 100644 --- a/SwifTools/Dock/MacOSXDock.h +++ b/SwifTools/Dock/MacOSXDock.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -16,6 +16,6 @@ namespace Swift { public: MacOSXDock(CocoaApplication* application); - virtual void setNumberOfPendingMessages(int i); + virtual void setNumberOfPendingMessages(size_t i); }; } diff --git a/SwifTools/Dock/MacOSXDock.mm b/SwifTools/Dock/MacOSXDock.mm index 5c7207c..3935ac0 100644 --- a/SwifTools/Dock/MacOSXDock.mm +++ b/SwifTools/Dock/MacOSXDock.mm @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015-2016 Isode Limited. + * Copyright (c) 2015-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -18,7 +18,7 @@ namespace Swift { MacOSXDock::MacOSXDock(CocoaApplication*) { } -void MacOSXDock::setNumberOfPendingMessages(int i) { +void MacOSXDock::setNumberOfPendingMessages(size_t i) { std::string label(i > 0 ? boost::lexical_cast(i) : ""); NSString *labelString = [[NSString alloc] initWithUTF8String: label.c_str()]; [[NSApp dockTile] setBadgeLabel: labelString]; diff --git a/SwifTools/Dock/NullDock.h b/SwifTools/Dock/NullDock.h index 9f3c554..137ba03 100644 --- a/SwifTools/Dock/NullDock.h +++ b/SwifTools/Dock/NullDock.h @@ -1,11 +1,13 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once +#include + #include namespace Swift { @@ -13,7 +15,7 @@ namespace Swift { public: NullDock() {} - virtual void setNumberOfPendingMessages(int) { + virtual void setNumberOfPendingMessages(size_t) { } }; } diff --git a/SwifTools/Dock/WindowsDock.h b/SwifTools/Dock/WindowsDock.h index fc10a48..f9a9dae 100644 --- a/SwifTools/Dock/WindowsDock.h +++ b/SwifTools/Dock/WindowsDock.h @@ -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. */ @@ -18,7 +18,7 @@ namespace Swift { public: WindowsDock(QSystemTrayIcon* tray, Notifier* notifier) : tray(tray), notifier(notifier) {} - virtual void setNumberOfPendingMessages(int i) { + virtual void setNumberOfPendingMessages(size_t i) { if (notifier->isAvailable()) { return; } diff --git a/SwifTools/Idle/MacOSXIdleQuerier.cpp b/SwifTools/Idle/MacOSXIdleQuerier.cpp index 6d6780b..89fa050 100644 --- a/SwifTools/Idle/MacOSXIdleQuerier.cpp +++ b/SwifTools/Idle/MacOSXIdleQuerier.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -10,6 +10,8 @@ #include #include +#include + #include #include @@ -29,7 +31,12 @@ int MacOSXIdleQuerier::getIdleTimeSeconds() { assert(result); (void) result; CFRelease(property); - return boost::numeric_cast(idle / 1000000000); + try { + return boost::numeric_cast(idle / 1000000000); + } + catch (const boost::numeric::bad_numeric_cast&) { + return std::numeric_limits::max(); + } } } diff --git a/SwifTools/SpellParser.cpp b/SwifTools/SpellParser.cpp index 5bafa6e..e449b45 100644 --- a/SwifTools/SpellParser.cpp +++ b/SwifTools/SpellParser.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2016 Isode Limited. + * Copyright (c) 2016-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -15,7 +15,6 @@ #include #include -#include #include #include @@ -47,14 +46,14 @@ struct counter { switch (t.id()) { case ID_WWW: - position += boost::numeric_cast(t.value().size()); + position += static_cast(t.value().size()); break; case ID_HTTP: - position += boost::numeric_cast(t.value().size()); + position += static_cast(t.value().size()); break; case ID_WORD: // matched a word - wordPositions.push_back(boost::tuples::make_tuple(position, position + boost::numeric_cast(t.value().size()))); - position += boost::numeric_cast(t.value().size()); + wordPositions.push_back(boost::tuples::make_tuple(position, position + static_cast(t.value().size()))); + position += static_cast(t.value().size()); break; case ID_CHAR: // match a simple char ++position; diff --git a/Swift/Controllers/AccountController.cpp b/Swift/Controllers/AccountController.cpp index ec914a6..27655c0 100644 --- a/Swift/Controllers/AccountController.cpp +++ b/Swift/Controllers/AccountController.cpp @@ -403,7 +403,7 @@ void AccountController::handleConnected() { adHocManager_->setOnline(true); } -void AccountController::handleEventQueueLengthChange(int count) { +void AccountController::handleEventQueueLengthChange(size_t count) { dock_->setNumberOfPendingMessages(count); } diff --git a/Swift/Controllers/AccountController.h b/Swift/Controllers/AccountController.h index 774aa8b..4a31645 100644 --- a/Swift/Controllers/AccountController.h +++ b/Swift/Controllers/AccountController.h @@ -111,7 +111,7 @@ namespace Swift { void handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText); void handleDisconnected(const boost::optional& error); void handleServerDiscoInfoResponse(std::shared_ptr, ErrorPayload::ref); - void handleEventQueueLengthChange(int count); + void handleEventQueueLengthChange(size_t count); void handleVCardReceived(const JID& j, VCard::ref vCard); void handleSettingChanged(const std::string& settingPath); void handlePurgeSavedLoginRequest(const std::string& username); diff --git a/Swift/Controllers/FileTransfer/FileTransferProgressInfo.cpp b/Swift/Controllers/FileTransfer/FileTransferProgressInfo.cpp index b073017..eddace9 100644 --- a/Swift/Controllers/FileTransfer/FileTransferProgressInfo.cpp +++ b/Swift/Controllers/FileTransfer/FileTransferProgressInfo.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2016 Isode Limited. + * Copyright (c) 2016-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -18,13 +18,13 @@ namespace Swift { -FileTransferProgressInfo::FileTransferProgressInfo(boost::uintmax_t completeBytes) : completeBytes(completeBytes), completedBytes(0), percentage(0) { +FileTransferProgressInfo::FileTransferProgressInfo(size_t completeBytes) : completeBytes(completeBytes), completedBytes(0), percentage(0) { onProgressPercentage(0); } -void FileTransferProgressInfo::setBytesProcessed(int processedBytes) { +void FileTransferProgressInfo::setBytesProcessed(size_t processedBytes) { int oldPercentage = int(double(completedBytes) / double(completeBytes) * 100.0); - completedBytes += boost::numeric_cast(processedBytes); + completedBytes += processedBytes; int newPercentage = int(double(completedBytes) / double(completeBytes) * 100.0); if (oldPercentage != newPercentage) { onProgressPercentage(newPercentage); diff --git a/Swift/Controllers/FileTransfer/FileTransferProgressInfo.h b/Swift/Controllers/FileTransfer/FileTransferProgressInfo.h index 5fb955c..869ceba 100644 --- a/Swift/Controllers/FileTransfer/FileTransferProgressInfo.h +++ b/Swift/Controllers/FileTransfer/FileTransferProgressInfo.h @@ -5,31 +5,32 @@ */ /* - * Copyright (c) 2016 Isode Limited. + * Copyright (c) 2016-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once -#include +#include + #include namespace Swift { class FileTransferProgressInfo { public: - FileTransferProgressInfo(boost::uintmax_t completeBytes); + FileTransferProgressInfo(size_t completeBytes); public: - void setBytesProcessed(int processedBytes); + void setBytesProcessed(size_t processedBytes); int getPercentage() const; boost::signals2::signal onProgressPercentage; private: - boost::uintmax_t completeBytes; - boost::uintmax_t completedBytes; + size_t completeBytes; + size_t completedBytes; int percentage; }; diff --git a/Swift/Controllers/Roster/LeastCommonSubsequence.h b/Swift/Controllers/Roster/LeastCommonSubsequence.h index 8daa20c..7988ee7 100644 --- a/Swift/Controllers/Roster/LeastCommonSubsequence.h +++ b/Swift/Controllers/Roster/LeastCommonSubsequence.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2016 Isode Limited. + * Copyright (c) 2011-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -32,7 +32,7 @@ namespace Swift { Predicate predicate; for (size_t i = 1; i < width; ++i) { for (size_t j = 1; j < height; ++j) { - result[i + j*width] = predicate(*(xBegin + boost::numeric_cast(i)-1), *(yBegin + boost::numeric_cast(j)-1)) ? result[(i-1) + (j-1)*width] + 1 : std::max(result[i + (j-1)*width], result[i-1 + (j*width)]); + result[i + j*width] = predicate(*(xBegin + static_cast(i)-1), *(yBegin + static_cast(j)-1)) ? result[(i-1) + (j-1)*width] + 1 : std::max(result[i + (j-1)*width], result[i-1 + (j*width)]); } } } diff --git a/Swift/Controllers/Roster/TableRoster.cpp b/Swift/Controllers/Roster/TableRoster.cpp index 713f390..01bf4a6 100644 --- a/Swift/Controllers/Roster/TableRoster.cpp +++ b/Swift/Controllers/Roster/TableRoster.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2016 Isode Limited. + * Copyright (c) 2011-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -132,15 +132,20 @@ void TableRoster::handleUpdateTimerTick() { std::vector itemRemoves; std::vector itemInserts; computeIndexDiff(sections[sectionUpdates[i]].items, newSections[sectionPostUpdates[i]].items, itemUpdates, itemPostUpdates, itemRemoves, itemInserts); - size_t end = update.insertedRows.size(); - update.insertedRows.resize(update.insertedRows.size() + itemInserts.size()); - std::transform(itemInserts.begin(), itemInserts.end(), update.insertedRows.begin() + boost::numeric_cast(end), CreateIndexForSection(sectionPostUpdates[i])); - end = update.deletedRows.size(); - update.deletedRows.resize(update.deletedRows.size() + itemRemoves.size()); - std::transform(itemRemoves.begin(), itemRemoves.end(), update.deletedRows.begin() + boost::numeric_cast(end), CreateIndexForSection(sectionUpdates[i])); - end = update.updatedRows.size(); - update.updatedRows.resize(update.updatedRows.size() + itemUpdates.size()); - std::transform(itemUpdates.begin(), itemUpdates.end(), update.updatedRows.begin() + boost::numeric_cast(end), CreateIndexForSection(sectionPostUpdates[i])); + try { + size_t end = update.insertedRows.size(); + update.insertedRows.resize(update.insertedRows.size() + itemInserts.size()); + std::transform(itemInserts.begin(), itemInserts.end(), update.insertedRows.begin() + boost::numeric_cast(end), CreateIndexForSection(sectionPostUpdates[i])); + end = update.deletedRows.size(); + update.deletedRows.resize(update.deletedRows.size() + itemRemoves.size()); + std::transform(itemRemoves.begin(), itemRemoves.end(), update.deletedRows.begin() + boost::numeric_cast(end), CreateIndexForSection(sectionUpdates[i])); + end = update.updatedRows.size(); + update.updatedRows.resize(update.updatedRows.size() + itemUpdates.size()); + std::transform(itemUpdates.begin(), itemUpdates.end(), update.updatedRows.begin() + boost::numeric_cast(end), CreateIndexForSection(sectionPostUpdates[i])); + } + catch (const boost::numeric::bad_numeric_cast&) { + // If any container claims it has more than long long max items, we have bigger issues, so letting this pass + } } // Switch the old model with the new diff --git a/Swift/Controllers/Storages/CertificateFileStorage.cpp b/Swift/Controllers/Storages/CertificateFileStorage.cpp index 8ba7d12..2e1343f 100644 --- a/Swift/Controllers/Storages/CertificateFileStorage.cpp +++ b/Swift/Controllers/Storages/CertificateFileStorage.cpp @@ -50,10 +50,15 @@ void CertificateFileStorage::addCertificate(Certificate::ref certificate) { std::cerr << "ERROR: " << e.what() << std::endl; } } - boost::filesystem::ofstream file(certificatePath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); - ByteArray data = certificate->toDER(); - file.write(reinterpret_cast(vecptr(data)), boost::numeric_cast(data.size())); - file.close(); + try { + boost::filesystem::ofstream file(certificatePath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); + ByteArray data = certificate->toDER(); + file.write(reinterpret_cast(vecptr(data)), boost::numeric_cast(data.size())); + file.close(); + } + catch (...) { + SWIFT_LOG(warning) << "Failed to store certificate to " << certificatePath << std::endl; + } } boost::filesystem::path CertificateFileStorage::getCertificatePath(Certificate::ref certificate) const { diff --git a/Swift/Controllers/XMPPEvents/EventController.cpp b/Swift/Controllers/XMPPEvents/EventController.cpp index f8fb192..0e9429d 100644 --- a/Swift/Controllers/XMPPEvents/EventController.cpp +++ b/Swift/Controllers/XMPPEvents/EventController.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. */ @@ -51,7 +51,7 @@ void EventController::handleIncomingEvent(std::shared_ptr sourceEve if ((messageEvent && messageEvent->isReadable()) || subscriptionEvent || errorEvent || mucInviteEvent || incomingFileTransferEvent) { events_.push_back(sourceEvent); sourceEvent->onConclusion.connect(boost::bind(&EventController::handleEventConcluded, this, sourceEvent)); - onEventQueueLengthChange(boost::numeric_cast(events_.size())); + onEventQueueLengthChange(events_.size()); onEventQueueEventAdded(sourceEvent); if (sourceEvent->getConcluded()) { handleEventConcluded(sourceEvent); @@ -62,7 +62,7 @@ void EventController::handleIncomingEvent(std::shared_ptr sourceEve void EventController::handleEventConcluded(std::shared_ptr event) { event->onConclusion.disconnect(boost::bind(&EventController::handleEventConcluded, this, event)); events_.erase(std::remove(events_.begin(), events_.end(), event), events_.end()); - onEventQueueLengthChange(boost::numeric_cast(events_.size())); + onEventQueueLengthChange(events_.size()); } void EventController::disconnectAll() { diff --git a/Swift/Controllers/XMPPEvents/EventController.h b/Swift/Controllers/XMPPEvents/EventController.h index 8a095d9..5b746e4 100644 --- a/Swift/Controllers/XMPPEvents/EventController.h +++ b/Swift/Controllers/XMPPEvents/EventController.h @@ -1,11 +1,12 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once +#include #include #include @@ -22,7 +23,7 @@ namespace Swift { ~EventController(); void handleIncomingEvent(std::shared_ptr sourceEvent); - boost::signals2::signal onEventQueueLengthChange; + boost::signals2::signal onEventQueueLengthChange; boost::signals2::signal)> onEventQueueEventAdded; const EventList& getEvents() const {return events_;} void disconnectAll(); diff --git a/Swift/QtUI/QtWebView.cpp b/Swift/QtUI/QtWebView.cpp index 967be1a..24636ed 100644 --- a/Swift/QtUI/QtWebView.cpp +++ b/Swift/QtUI/QtWebView.cpp @@ -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. */ @@ -7,8 +7,6 @@ #include -#include - #include #include #include @@ -48,7 +46,7 @@ void QtWebView::keyPressEvent(QKeyEvent* event) { modifiers, event->text(), event->isAutoRepeat(), - boost::numeric_cast(event->count())); + event->count()); QWebView::keyPressEvent(translatedEvent); delete translatedEvent; } diff --git a/Swiften/SASL/UnitTest/WindowsServicePrincipalNameTest.cpp b/Swiften/SASL/UnitTest/WindowsServicePrincipalNameTest.cpp index fa07052..ef3a9b3 100644 --- a/Swiften/SASL/UnitTest/WindowsServicePrincipalNameTest.cpp +++ b/Swiften/SASL/UnitTest/WindowsServicePrincipalNameTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Isode Limited. + * Copyright (c) 2015-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -97,16 +97,16 @@ class WindowsServicePrincipalNameTest : public CppUnit::TestFixture { WindowsServicePrincipalName spn("adlon.isode.net"); spn.setInstanceName("mlink.adlon.isode.net"); - spn.setInstancePort(6222); - CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/mlink.adlon.isode.net:6222/adlon.isode.net")); + spn.setInstancePort(55222); + CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/mlink.adlon.isode.net:55222/adlon.isode.net")); spn.setInstancePort(0); CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/mlink.adlon.isode.net/adlon.isode.net")); WindowsServicePrincipalName spn2("mlink.adlon.isode.net"); - spn2.setInstancePort(6222); - CPPUNIT_ASSERT_EQUAL(spn2.toString(), std::string("xmpp/mlink.adlon.isode.net:6222")); + spn2.setInstancePort(55222); + CPPUNIT_ASSERT_EQUAL(spn2.toString(), std::string("xmpp/mlink.adlon.isode.net:55222")); spn2.setInstancePort(0); CPPUNIT_ASSERT_EQUAL(spn2.toString(), std::string("xmpp/mlink.adlon.isode.net")); diff --git a/Swiften/SASL/WindowsServicePrincipalName.h b/Swiften/SASL/WindowsServicePrincipalName.h index 4c9f557..2e4e5c4 100644 --- a/Swiften/SASL/WindowsServicePrincipalName.h +++ b/Swiften/SASL/WindowsServicePrincipalName.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Isode Limited. + * Copyright (c) 2015-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -49,7 +49,7 @@ namespace Swift { * it remain 0 to indicate that the default port is * used. */ - void setInstancePort(short int instancePort) { instancePort_ = instancePort; } + void setInstancePort(unsigned short instancePort) { instancePort_ = instancePort; } /* * This follows the rules of SPN creation on Windows and -- cgit v0.10.2-6-g49f6