From ecc588c27a0a7c47f2090be864a111471c982dff Mon Sep 17 00:00:00 2001 From: Matthieu Gallien Date: Fri, 30 Sep 2022 21:36:58 +0200 Subject: [PATCH] avoid possibly crashing static_cast 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 --- src/3rdparty/kirigami/wheelhandler.cpp | 6 +++--- src/gui/application.cpp | 2 +- src/gui/creds/webflowcredentialsdialog.cpp | 2 +- src/gui/folderstatusdelegate.cpp | 6 +++--- src/gui/folderwizard.cpp | 2 +- src/gui/selectivesyncdialog.cpp | 4 ++-- src/gui/settingsdialog.cpp | 4 ++-- src/gui/socketapi/socketapi.cpp | 2 +- src/gui/tooltipupdater.cpp | 2 +- src/gui/tray/sortedactivitylistmodel.cpp | 2 +- src/gui/wizard/abstractcredswizardpage.cpp | 2 +- src/gui/wizard/owncloudadvancedsetuppage.cpp | 8 ++++---- src/libsync/account.cpp | 6 +++--- src/libsync/clientsideencryption.cpp | 6 +++--- src/libsync/creds/httpcredentials.cpp | 2 +- src/libsync/owncloudpropagator.cpp | 2 +- 16 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/3rdparty/kirigami/wheelhandler.cpp b/src/3rdparty/kirigami/wheelhandler.cpp index ce21e51ac..fdfcee044 100644 --- a/src/3rdparty/kirigami/wheelhandler.cpp +++ b/src/3rdparty/kirigami/wheelhandler.cpp @@ -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(obj); + auto item = dynamic_cast(obj); m_handlersForItem.remove(item); }); connect(handler, &QObject::destroyed, this, [this](QObject *obj) { - auto handler = static_cast(obj); + auto handler = dynamic_cast(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(event); + auto we = dynamic_cast(event); m_wheelEvent.initializeFromEvent(we); bool shouldBlock = false; diff --git a/src/gui/application.cpp b/src/gui/application.cpp index b99bf4a6a..21cce1a67 100644 --- a/src/gui/application.cpp +++ b/src/gui/application.cpp @@ -896,7 +896,7 @@ void Application::tryTrayAgain() bool Application::event(QEvent *event) { if (event->type() == QEvent::FileOpen) { - const auto openEvent = static_cast(event); + const auto openEvent = dynamic_cast(event); qCDebug(lcApplication) << "macOS: Received a QFileOpenEvent"; if(!openEvent->file().isEmpty()) { diff --git a/src/gui/creds/webflowcredentialsdialog.cpp b/src/gui/creds/webflowcredentialsdialog.cpp index 6edf9f3fb..dce3d43a1 100644 --- a/src/gui/creds/webflowcredentialsdialog.cpp +++ b/src/gui/creds/webflowcredentialsdialog.cpp @@ -61,7 +61,7 @@ WebFlowCredentialsDialog::WebFlowCredentialsDialog(Account *account, bool useFlo #endif // WITH_WEBENGINE } - auto app = static_cast(qApp); + auto app = dynamic_cast(qApp); connect(app, &Application::isShowingSettingsDialog, this, &WebFlowCredentialsDialog::slotShowSettingsDialog); _errorLabel = new QLabel(); diff --git a/src/gui/folderstatusdelegate.cpp b/src/gui/folderstatusdelegate.cpp index ed5b222f4..cb8ef67ed 100644 --- a/src/gui/folderstatusdelegate.cpp +++ b/src/gui/folderstatusdelegate.cpp @@ -58,7 +58,7 @@ QSize FolderStatusDelegate::sizeHint(const QStyleOptionViewItem &option, QFontMetrics fm(font); QFontMetrics aliasFm(aliasFont); - auto classif = static_cast(index.model())->classify(index); + auto classif = dynamic_cast(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(index.model())->classify(index) != FolderStatusModel::RootFolder) { + if (dynamic_cast(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(option.widget)) { - auto *me = static_cast(event); + auto *me = dynamic_cast(event); QModelIndex index; if (me->buttons()) { index = view->indexAt(me->pos()); diff --git a/src/gui/folderwizard.cpp b/src/gui/folderwizard.cpp index cb4fa2a13..e13733398 100644 --- a/src/gui/folderwizard.cpp +++ b/src/gui/folderwizard.cpp @@ -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(sender())->path()); + _ui.folderEntry->setText(dynamic_cast(sender())->path()); slotLsColFolderEntry(); } diff --git a/src/gui/selectivesyncdialog.cpp b/src/gui/selectivesyncdialog.cpp index 3b8a33612..33ad4c064 100644 --- a/src/gui/selectivesyncdialog.cpp +++ b/src/gui/selectivesyncdialog.cpp @@ -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(findFirstChild(parent, pathTrail.first())); + auto *item = dynamic_cast(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 isInserting(_inserting); _inserting = true; - auto *root = static_cast(_folderTree->topLevelItem(0)); + auto *root = dynamic_cast(_folderTree->topLevelItem(0)); QUrl url = _account->davUrl(); QString pathToRemove = url.path(); diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index 611ef236e..e66cd2a96 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -276,7 +276,7 @@ void SettingsDialog::accountAdded(AccountState *s) void SettingsDialog::slotAccountAvatarChanged() { - auto *account = static_cast(sender()); + auto *account = dynamic_cast(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(sender()); + auto *account = dynamic_cast(sender()); if (account && _actionForAccount.contains(account)) { QAction *action = _actionForAccount[account]; if (action) { diff --git a/src/gui/socketapi/socketapi.cpp b/src/gui/socketapi/socketapi.cpp index 23de14381..9b14bb63e 100644 --- a/src/gui/socketapi/socketapi.cpp +++ b/src/gui/socketapi/socketapi.cpp @@ -338,7 +338,7 @@ void SocketApi::onLostConnection() void SocketApi::slotSocketDestroyed(QObject *obj) { - auto *socket = static_cast(obj); + auto *socket = dynamic_cast(obj); _listeners.remove(socket); } diff --git a/src/gui/tooltipupdater.cpp b/src/gui/tooltipupdater.cpp index 5b4233a01..12a6b25b3 100644 --- a/src/gui/tooltipupdater.cpp +++ b/src/gui/tooltipupdater.cpp @@ -32,7 +32,7 @@ ToolTipUpdater::ToolTipUpdater(QTreeView *treeView) bool ToolTipUpdater::eventFilter(QObject * /*obj*/, QEvent *ev) { if (ev->type() == QEvent::ToolTip) { - auto *helpEvent = static_cast(ev); + auto *helpEvent = dynamic_cast(ev); _toolTipPos = helpEvent->globalPos(); } return false; diff --git a/src/gui/tray/sortedactivitylistmodel.cpp b/src/gui/tray/sortedactivitylistmodel.cpp index 8614ec83e..4879143f2 100644 --- a/src/gui/tray/sortedactivitylistmodel.cpp +++ b/src/gui/tray/sortedactivitylistmodel.cpp @@ -30,7 +30,7 @@ void SortedActivityListModel::sortModel() ActivityListModel* SortedActivityListModel::activityListModel() const { - return static_cast(sourceModel()); + return dynamic_cast(sourceModel()); } void SortedActivityListModel::setActivityListModel(ActivityListModel* activityListModel) diff --git a/src/gui/wizard/abstractcredswizardpage.cpp b/src/gui/wizard/abstractcredswizardpage.cpp index 696e98e09..80b302887 100644 --- a/src/gui/wizard/abstractcredswizardpage.cpp +++ b/src/gui/wizard/abstractcredswizardpage.cpp @@ -26,7 +26,7 @@ void AbstractCredentialsWizardPage::cleanupPage() { // Reset the credentials when the 'Back' button is used. - AccountPtr account = static_cast(wizard())->account(); + AccountPtr account = dynamic_cast(wizard())->account(); AbstractCredentials *creds = account->credentials(); if (creds) { if (!creds->inherits("DummyCredentials")) { diff --git a/src/gui/wizard/owncloudadvancedsetuppage.cpp b/src/gui/wizard/owncloudadvancedsetuppage.cpp index ccd54edc4..5435cf71e 100644 --- a/src/gui/wizard/owncloudadvancedsetuppage.cpp +++ b/src/gui/wizard/owncloudadvancedsetuppage.cpp @@ -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(wizard())->account(); + auto acc = dynamic_cast(wizard())->account(); auto quotaJob = new PropfindJob(acc, _remoteFolder, this); quotaJob->setProperties(QList() << "http://owncloud.org/ns:size"); @@ -331,8 +331,8 @@ void OwncloudAdvancedSetupPage::stopSpinner() QUrl OwncloudAdvancedSetupPage::serverUrl() const { - const QString urlString = static_cast(wizard())->ocUrl(); - const QString user = static_cast(wizard())->getCredentials()->user(); + const QString urlString = dynamic_cast(wizard())->ocUrl(); + const QString user = dynamic_cast(wizard())->getCredentials()->user(); QUrl url(urlString); url.setUserName(user); @@ -447,7 +447,7 @@ void OwncloudAdvancedSetupPage::slotSelectFolder() void OwncloudAdvancedSetupPage::slotSelectiveSyncClicked() { - AccountPtr acc = static_cast(wizard())->account(); + AccountPtr acc = dynamic_cast(wizard())->account(); auto *dlg = new SelectiveSyncDialog(acc, _remoteFolder, _selectiveSyncBlacklist, this); dlg->setAttribute(Qt::WA_DeleteOnClose); diff --git a/src/libsync/account.cpp b/src/libsync/account.cpp index fa874386b..bb23cd07c 100644 --- a/src/libsync/account.cpp +++ b/src/libsync/account.cpp @@ -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(incoming); + auto *writeJob = dynamic_cast(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(incoming); + auto *readJob = dynamic_cast(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(incoming); + auto *deleteJob = dynamic_cast(incoming); if (deleteJob->error() == NoError) qCInfo(lcAccount) << "appPassword deleted from keychain"; else diff --git a/src/libsync/clientsideencryption.cpp b/src/libsync/clientsideencryption.cpp index 9d676784e..a9a81b1a2 100644 --- a/src/libsync/clientsideencryption.cpp +++ b/src/libsync/clientsideencryption.cpp @@ -906,7 +906,7 @@ bool ClientSideEncryption::checkServerPublicKeyValidity(const QByteArray &server void ClientSideEncryption::publicKeyFetched(Job *incoming) { - auto *readJob = static_cast(incoming); + auto *readJob = dynamic_cast(incoming); auto account = readJob->property(accountProperty).value(); Q_ASSERT(account); @@ -943,7 +943,7 @@ void ClientSideEncryption::publicKeyFetched(Job *incoming) void ClientSideEncryption::privateKeyFetched(Job *incoming) { - auto *readJob = static_cast(incoming); + auto *readJob = dynamic_cast(incoming); auto account = readJob->property(accountProperty).value(); Q_ASSERT(account); @@ -981,7 +981,7 @@ void ClientSideEncryption::privateKeyFetched(Job *incoming) void ClientSideEncryption::mnemonicKeyFetched(QKeychain::Job *incoming) { - auto *readJob = static_cast(incoming); + auto *readJob = dynamic_cast(incoming); auto account = readJob->property(accountProperty).value(); Q_ASSERT(account); diff --git a/src/libsync/creds/httpcredentials.cpp b/src/libsync/creds/httpcredentials.cpp index 58ebbf455..292680ee1 100644 --- a/src/libsync/creds/httpcredentials.cpp +++ b/src/libsync/creds/httpcredentials.cpp @@ -357,7 +357,7 @@ bool HttpCredentials::stillValid(QNetworkReply *reply) void HttpCredentials::slotReadJobDone(QKeychain::Job *incoming) { - auto *job = static_cast(incoming); + auto *job = dynamic_cast(incoming); QKeychain::Error error = job->error(); // If we can't find the credentials at the keys that include the account id, diff --git a/src/libsync/owncloudpropagator.cpp b/src/libsync/owncloudpropagator.cpp index 428d82d90..198f1b73b 100644 --- a/src/libsync/owncloudpropagator.cpp +++ b/src/libsync/owncloudpropagator.cpp @@ -1076,7 +1076,7 @@ bool PropagatorCompositeJob::scheduleSelfOrChild() void PropagatorCompositeJob::slotSubJobFinished(SyncFileItem::Status status) { - auto *subJob = static_cast(sender()); + auto *subJob = dynamic_cast(sender()); ASSERT(subJob); // Delete the job and remove it from our list of jobs.