avoid possibly crashing static_cast
authorMatthieu Gallien <matthieu.gallien@nextcloud.com>
Fri, 30 Sep 2022 19:36:58 +0000 (21:36 +0200)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Mon, 3 Oct 2022 08:23:24 +0000 (10:23 +0200)
ran
run-clang-tidy-14.py -header-filter='.*' -checks='-*,cppcoreguidelines-pro-type-static-cast-downcast' -fix

this can prevent casting to a type that is unrelated to the real type
and later cause a crash because you go into undefined behavior domain

Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
16 files changed:
src/3rdparty/kirigami/wheelhandler.cpp
src/gui/application.cpp
src/gui/creds/webflowcredentialsdialog.cpp
src/gui/folderstatusdelegate.cpp
src/gui/folderwizard.cpp
src/gui/selectivesyncdialog.cpp
src/gui/settingsdialog.cpp
src/gui/socketapi/socketapi.cpp
src/gui/tooltipupdater.cpp
src/gui/tray/sortedactivitylistmodel.cpp
src/gui/wizard/abstractcredswizardpage.cpp
src/gui/wizard/owncloudadvancedsetuppage.cpp
src/libsync/account.cpp
src/libsync/clientsideencryption.cpp
src/libsync/creds/httpcredentials.cpp
src/libsync/owncloudpropagator.cpp

index ce21e51ac30d44d38446f744645bd48f740fa647..fdfcee044b81ef7cd14e972df078aea2989ca95c 100644 (file)
@@ -37,12 +37,12 @@ void GlobalWheelFilter::setItemHandlerAssociation(QQuickItem *item, WheelHandler
     m_handlersForItem.insert(item, handler);
 
     connect(item, &QObject::destroyed, this, [this](QObject *obj) {
-        auto item = static_cast<QQuickItem *>(obj);
+        auto item = dynamic_cast<QQuickItem *>(obj);
         m_handlersForItem.remove(item);
     });
 
     connect(handler, &QObject::destroyed, this, [this](QObject *obj) {
-        auto handler = static_cast<WheelHandler *>(obj);
+        auto handler = dynamic_cast<WheelHandler *>(obj);
         removeItemHandlerAssociation(handler->target(), handler);
     });
 }
@@ -65,7 +65,7 @@ bool GlobalWheelFilter::eventFilter(QObject *watched, QEvent *event)
         if (!item || !item->isEnabled()) {
             return QObject::eventFilter(watched, event);
         }
-        auto we = static_cast<QWheelEvent *>(event);
+        auto we = dynamic_cast<QWheelEvent *>(event);
         m_wheelEvent.initializeFromEvent(we);
 
         bool shouldBlock = false;
index b99bf4a6a67455c387afb47d2646e7cfbf66a822..21cce1a67c74865f65d53ff32134756d73f0dbef 100644 (file)
@@ -896,7 +896,7 @@ void Application::tryTrayAgain()
 bool Application::event(QEvent *event)
 {
     if (event->type() == QEvent::FileOpen) {
-        const auto openEvent = static_cast<QFileOpenEvent *>(event);
+        const auto openEvent = dynamic_cast<QFileOpenEvent *>(event);
         qCDebug(lcApplication) << "macOS: Received a QFileOpenEvent";
 
         if(!openEvent->file().isEmpty()) {
index 6edf9f3fb4634650ec49540d074cd2b18f8b420d..dce3d43a1495ceb323a7883869700346caf14303 100644 (file)
@@ -61,7 +61,7 @@ WebFlowCredentialsDialog::WebFlowCredentialsDialog(Account *account, bool useFlo
 #endif // WITH_WEBENGINE
     }
 
-    auto app = static_cast<Application *>(qApp);
+    auto app = dynamic_cast<Application *>(qApp);
     connect(app, &Application::isShowingSettingsDialog, this, &WebFlowCredentialsDialog::slotShowSettingsDialog);
 
     _errorLabel = new QLabel();
index ed5b222f4bc2690c34407f16a396491e1e9ef05a..cb8ef67ed978b02482448a0d4c413fd61ef6bceb 100644 (file)
@@ -58,7 +58,7 @@ QSize FolderStatusDelegate::sizeHint(const QStyleOptionViewItem &option,
     QFontMetrics fm(font);
     QFontMetrics aliasFm(aliasFont);
 
-    auto classif = static_cast<const FolderStatusModel *>(index.model())->classify(index);
+    auto classif = dynamic_cast<const FolderStatusModel *>(index.model())->classify(index);
     if (classif == FolderStatusModel::AddButton) {
         const int margins = aliasFm.height(); // same as 2*aliasMargin of paint
         QFontMetrics fm(qApp->font("QPushButton"));
@@ -148,7 +148,7 @@ void FolderStatusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
         return;
     }
 
-    if (static_cast<const FolderStatusModel *>(index.model())->classify(index) != FolderStatusModel::RootFolder) {
+    if (dynamic_cast<const FolderStatusModel *>(index.model())->classify(index) != FolderStatusModel::RootFolder) {
         return;
     }
     painter->save();
@@ -367,7 +367,7 @@ bool FolderStatusDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
     case QEvent::MouseButtonPress:
     case QEvent::MouseMove:
         if (const auto *view = qobject_cast<const QAbstractItemView *>(option.widget)) {
-            auto *me = static_cast<QMouseEvent *>(event);
+            auto *me = dynamic_cast<QMouseEvent *>(event);
             QModelIndex index;
             if (me->buttons()) {
                 index = view->indexAt(me->pos());
index cb4fa2a130d1a30fb9ad3beb69569a53df5ec66f..e1373339832a1b3d9bf5e9d95f0ddf9b9653e16b 100644 (file)
@@ -239,7 +239,7 @@ void FolderWizardRemotePath::slotCreateRemoteFolderFinished()
     qCDebug(lcWizard) << "webdav mkdir request finished";
     showWarn(tr("Folder was successfully created on %1.").arg(Theme::instance()->appNameGUI()));
     slotRefreshFolders();
-    _ui.folderEntry->setText(static_cast<MkColJob *>(sender())->path());
+    _ui.folderEntry->setText(dynamic_cast<MkColJob *>(sender())->path());
     slotLsColFolderEntry();
 }
 
index 3b8a3361216281d8464f806cbf8210b1537d7df6..33ad4c064ae03bbc25cf73b2678e0f9ffbbbd25c 100644 (file)
@@ -163,7 +163,7 @@ void SelectiveSyncWidget::recursiveInsert(QTreeWidgetItem *parent, QStringList p
         parent->setToolTip(0, path);
         parent->setData(0, Qt::UserRole, path);
     } else {
-        auto *item = static_cast<SelectiveSyncTreeViewItem *>(findFirstChild(parent, pathTrail.first()));
+        auto *item = dynamic_cast<SelectiveSyncTreeViewItem *>(findFirstChild(parent, pathTrail.first()));
         if (!item) {
             item = new SelectiveSyncTreeViewItem(parent);
             if (parent->checkState(0) == Qt::Checked
@@ -201,7 +201,7 @@ void SelectiveSyncWidget::slotUpdateDirectories(QStringList list)
     QScopedValueRollback<bool> isInserting(_inserting);
     _inserting = true;
 
-    auto *root = static_cast<SelectiveSyncTreeViewItem *>(_folderTree->topLevelItem(0));
+    auto *root = dynamic_cast<SelectiveSyncTreeViewItem *>(_folderTree->topLevelItem(0));
 
     QUrl url = _account->davUrl();
     QString pathToRemove = url.path();
index 611ef236e6d1d0a7a8614920b56d4eb7845bee37..e66cd2a96218f142d6ab6a246fbf5f377bb0bd5a 100644 (file)
@@ -276,7 +276,7 @@ void SettingsDialog::accountAdded(AccountState *s)
 
 void SettingsDialog::slotAccountAvatarChanged()
 {
-    auto *account = static_cast<Account *>(sender());
+    auto *account = dynamic_cast<Account *>(sender());
     if (account && _actionForAccount.contains(account)) {
         QAction *action = _actionForAccount[account];
         if (action) {
@@ -290,7 +290,7 @@ void SettingsDialog::slotAccountAvatarChanged()
 
 void SettingsDialog::slotAccountDisplayNameChanged()
 {
-    auto *account = static_cast<Account *>(sender());
+    auto *account = dynamic_cast<Account *>(sender());
     if (account && _actionForAccount.contains(account)) {
         QAction *action = _actionForAccount[account];
         if (action) {
index 23de14381a427a3eaff10100deb984320547285c..9b14bb63e5e35a52143550626ce4a72514831b19 100644 (file)
@@ -338,7 +338,7 @@ void SocketApi::onLostConnection()
 
 void SocketApi::slotSocketDestroyed(QObject *obj)
 {
-    auto *socket = static_cast<QIODevice *>(obj);
+    auto *socket = dynamic_cast<QIODevice *>(obj);
     _listeners.remove(socket);
 }
 
index 5b4233a0114bfc384d22e61bd1937a5e3e54a8cd..12a6b25b376e356ff1668a2f83f34fad4799f568 100644 (file)
@@ -32,7 +32,7 @@ ToolTipUpdater::ToolTipUpdater(QTreeView *treeView)
 bool ToolTipUpdater::eventFilter(QObject * /*obj*/, QEvent *ev)
 {
     if (ev->type() == QEvent::ToolTip) {
-        auto *helpEvent = static_cast<QHelpEvent *>(ev);
+        auto *helpEvent = dynamic_cast<QHelpEvent *>(ev);
         _toolTipPos = helpEvent->globalPos();
     }
     return false;
index 8614ec83e560f25e1f70a4d2f6d2f625b46dcfbf..4879143f23b31d9978701af541fb25f654007a76 100644 (file)
@@ -30,7 +30,7 @@ void SortedActivityListModel::sortModel()
 
 ActivityListModel* SortedActivityListModel::activityListModel() const
 {
-    return static_cast<ActivityListModel*>(sourceModel());
+    return dynamic_cast<ActivityListModel*>(sourceModel());
 }
 
 void SortedActivityListModel::setActivityListModel(ActivityListModel* activityListModel)
index 696e98e0918e963d9ff941f30369e2603c0bffef..80b3028879f2a0957531a4f175ec94e65404a780 100644 (file)
@@ -26,7 +26,7 @@ void AbstractCredentialsWizardPage::cleanupPage()
 {
     // Reset the credentials when the 'Back' button is used.
 
-    AccountPtr account = static_cast<OwncloudWizard *>(wizard())->account();
+    AccountPtr account = dynamic_cast<OwncloudWizard *>(wizard())->account();
     AbstractCredentials *creds = account->credentials();
     if (creds) {
         if (!creds->inherits("DummyCredentials")) {
index ccd54edc4633eddd44459bf4f7708bf8154da3cb..5435cf71e3dc6cdc10c4907f08e921dafc4bda74 100644 (file)
@@ -159,7 +159,7 @@ void OwncloudAdvancedSetupPage::initializePage()
     // ensure "next" gets the focus, not obSelectLocalFolder
     QTimer::singleShot(0, wizard()->button(QWizard::FinishButton), qOverload<>(&QWidget::setFocus));
 
-    auto acc = static_cast<OwncloudWizard *>(wizard())->account();
+    auto acc = dynamic_cast<OwncloudWizard *>(wizard())->account();
     auto quotaJob = new PropfindJob(acc, _remoteFolder, this);
     quotaJob->setProperties(QList<QByteArray>() << "http://owncloud.org/ns:size");
 
@@ -331,8 +331,8 @@ void OwncloudAdvancedSetupPage::stopSpinner()
 
 QUrl OwncloudAdvancedSetupPage::serverUrl() const
 {
-    const QString urlString = static_cast<OwncloudWizard *>(wizard())->ocUrl();
-    const QString user = static_cast<OwncloudWizard *>(wizard())->getCredentials()->user();
+    const QString urlString = dynamic_cast<OwncloudWizard *>(wizard())->ocUrl();
+    const QString user = dynamic_cast<OwncloudWizard *>(wizard())->getCredentials()->user();
 
     QUrl url(urlString);
     url.setUserName(user);
@@ -447,7 +447,7 @@ void OwncloudAdvancedSetupPage::slotSelectFolder()
 
 void OwncloudAdvancedSetupPage::slotSelectiveSyncClicked()
 {
-    AccountPtr acc = static_cast<OwncloudWizard *>(wizard())->account();
+    AccountPtr acc = dynamic_cast<OwncloudWizard *>(wizard())->account();
     auto *dlg = new SelectiveSyncDialog(acc, _remoteFolder, _selectiveSyncBlacklist, this);
     dlg->setAttribute(Qt::WA_DeleteOnClose);
 
index fa874386bb412c98f0b612369e847e8fc0b83184..bb23cd07cbcecf5a1647e15b4383d5317be68123 100644 (file)
@@ -716,7 +716,7 @@ void Account::writeAppPasswordOnce(QString appPassword){
     job->setKey(kck);
     job->setBinaryData(appPassword.toLatin1());
     connect(job, &WritePasswordJob::finished, [this](Job *incoming) {
-        auto *writeJob = static_cast<WritePasswordJob *>(incoming);
+        auto *writeJob = dynamic_cast<WritePasswordJob *>(incoming);
         if (writeJob->error() == NoError)
             qCInfo(lcAccount) << "appPassword stored in keychain";
         else
@@ -739,7 +739,7 @@ void Account::retrieveAppPassword(){
     job->setInsecureFallback(false);
     job->setKey(kck);
     connect(job, &ReadPasswordJob::finished, [this](Job *incoming) {
-        auto *readJob = static_cast<ReadPasswordJob *>(incoming);
+        auto *readJob = dynamic_cast<ReadPasswordJob *>(incoming);
         QString pwd("");
         // Error or no valid public key error out
         if (readJob->error() == NoError &&
@@ -769,7 +769,7 @@ void Account::deleteAppPassword()
     job->setInsecureFallback(false);
     job->setKey(kck);
     connect(job, &DeletePasswordJob::finished, [this](Job *incoming) {
-        auto *deleteJob = static_cast<DeletePasswordJob *>(incoming);
+        auto *deleteJob = dynamic_cast<DeletePasswordJob *>(incoming);
         if (deleteJob->error() == NoError)
             qCInfo(lcAccount) << "appPassword deleted from keychain";
         else
index 9d676784e7cec3ab3f689f438254b6a8681fd387..a9a81b1a218a5bbe84d1294112b6fa3f63a5d768 100644 (file)
@@ -906,7 +906,7 @@ bool ClientSideEncryption::checkServerPublicKeyValidity(const QByteArray &server
 
 void ClientSideEncryption::publicKeyFetched(Job *incoming)
 {
-    auto *readJob = static_cast<ReadPasswordJob *>(incoming);
+    auto *readJob = dynamic_cast<ReadPasswordJob *>(incoming);
     auto account = readJob->property(accountProperty).value<AccountPtr>();
     Q_ASSERT(account);
 
@@ -943,7 +943,7 @@ void ClientSideEncryption::publicKeyFetched(Job *incoming)
 
 void ClientSideEncryption::privateKeyFetched(Job *incoming)
 {
-    auto *readJob = static_cast<ReadPasswordJob *>(incoming);
+    auto *readJob = dynamic_cast<ReadPasswordJob *>(incoming);
     auto account = readJob->property(accountProperty).value<AccountPtr>();
     Q_ASSERT(account);
 
@@ -981,7 +981,7 @@ void ClientSideEncryption::privateKeyFetched(Job *incoming)
 
 void ClientSideEncryption::mnemonicKeyFetched(QKeychain::Job *incoming)
 {
-    auto *readJob = static_cast<ReadPasswordJob *>(incoming);
+    auto *readJob = dynamic_cast<ReadPasswordJob *>(incoming);
     auto account = readJob->property(accountProperty).value<AccountPtr>();
     Q_ASSERT(account);
 
index 58ebbf455962c1a6865130d0f5a5bc6f7843488c..292680ee1f373b3b088b3ec566efd7c508fd7312 100644 (file)
@@ -357,7 +357,7 @@ bool HttpCredentials::stillValid(QNetworkReply *reply)
 
 void HttpCredentials::slotReadJobDone(QKeychain::Job *incoming)
 {
-    auto *job = static_cast<QKeychain::ReadPasswordJob *>(incoming);
+    auto *job = dynamic_cast<QKeychain::ReadPasswordJob *>(incoming);
     QKeychain::Error error = job->error();
 
     // If we can't find the credentials at the keys that include the account id,
index 428d82d908e4667c1e8cd6890eaaf78745f465c8..198f1b73b392aff0d745c675da60af18b39a89fd 100644 (file)
@@ -1076,7 +1076,7 @@ bool PropagatorCompositeJob::scheduleSelfOrChild()
 
 void PropagatorCompositeJob::slotSubJobFinished(SyncFileItem::Status status)
 {
-    auto *subJob = static_cast<PropagatorJob *>(sender());
+    auto *subJob = dynamic_cast<PropagatorJob *>(sender());
     ASSERT(subJob);
 
     // Delete the job and remove it from our list of jobs.