Skip to content

Commit 8046a3e

Browse files
committed
Merge #16348: qt: Assert QMetaObject::invokeMethod result
64fee48 qt: Assert QMetaObject::invokeMethod result (João Barbosa) f27bd96 gui: Fix missing qRegisterMetaType(WalletModel*) (João Barbosa) Pull request description: Invalid/wrong dynamic calls aren't verified by the compiler. This PR asserts those dynamic calls. Once we bump Qt to at least 5.10 these can be refactored to use the `invokeMethod` overload that allows connecting to lambdas or member pointers, which are compile checked. For reference, one of the overloaded versions is https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-5. ACKs for top commit: laanwj: ACK 64fee48 Tree-SHA512: d332e5d7eb2c7be5d3fe90e2e4ff20a67800b9664f6637c122a23647a964f7915703d3f086e2de440f695cfe14de268ff581d0092b7736e911952a4f4d248e25
2 parents c799976 + 64fee48 commit 8046a3e

File tree

8 files changed

+47
-22
lines changed

8 files changed

+47
-22
lines changed

src/qt/bitcoin.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ int GuiMain(int argc, char* argv[])
448448

449449
// Register meta types used for QMetaObject::invokeMethod
450450
qRegisterMetaType< bool* >();
451+
#ifdef ENABLE_WALLET
452+
qRegisterMetaType<WalletModel*>();
453+
#endif
451454
// Need to pass name here as CAmount is a typedef (see http://qt-project.org/doc/qt-5/qmetatype.html#qRegisterMetaType)
452455
// IMPORTANT if it is no longer a typedef use the normal variant above
453456
qRegisterMetaType< CAmount >("CAmount");

src/qt/bitcoingui.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,12 +1375,13 @@ static bool ThreadSafeMessageBox(BitcoinGUI* gui, const std::string& message, co
13751375
style &= ~CClientUIInterface::SECURE;
13761376
bool ret = false;
13771377
// In case of modal message, use blocking connection to wait for user to click a button
1378-
QMetaObject::invokeMethod(gui, "message",
1378+
bool invoked = QMetaObject::invokeMethod(gui, "message",
13791379
modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
13801380
Q_ARG(QString, QString::fromStdString(caption)),
13811381
Q_ARG(QString, QString::fromStdString(message)),
13821382
Q_ARG(unsigned int, style),
13831383
Q_ARG(bool*, &ret));
1384+
assert(invoked);
13841385
return ret;
13851386
}
13861387

src/qt/clientmodel.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,34 +184,39 @@ void ClientModel::updateBanlist()
184184
static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
185185
{
186186
// emits signal "showProgress"
187-
QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
187+
bool invoked = QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
188188
Q_ARG(QString, QString::fromStdString(title)),
189189
Q_ARG(int, nProgress));
190+
assert(invoked);
190191
}
191192

192193
static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
193194
{
194195
// Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
195-
QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
196+
bool invoked = QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
196197
Q_ARG(int, newNumConnections));
198+
assert(invoked);
197199
}
198200

199201
static void NotifyNetworkActiveChanged(ClientModel *clientmodel, bool networkActive)
200202
{
201-
QMetaObject::invokeMethod(clientmodel, "updateNetworkActive", Qt::QueuedConnection,
203+
bool invoked = QMetaObject::invokeMethod(clientmodel, "updateNetworkActive", Qt::QueuedConnection,
202204
Q_ARG(bool, networkActive));
205+
assert(invoked);
203206
}
204207

205208
static void NotifyAlertChanged(ClientModel *clientmodel)
206209
{
207210
qDebug() << "NotifyAlertChanged";
208-
QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection);
211+
bool invoked = QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection);
212+
assert(invoked);
209213
}
210214

211215
static void BannedListChanged(ClientModel *clientmodel)
212216
{
213217
qDebug() << QString("%1: Requesting update for peer banlist").arg(__func__);
214-
QMetaObject::invokeMethod(clientmodel, "updateBanlist", Qt::QueuedConnection);
218+
bool invoked = QMetaObject::invokeMethod(clientmodel, "updateBanlist", Qt::QueuedConnection);
219+
assert(invoked);
215220
}
216221

217222
static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, int height, int64_t blockTime, double verificationProgress, bool fHeader)
@@ -233,11 +238,12 @@ static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, int heig
233238
// if we are in-sync or if we notify a header update, update the UI regardless of last update time
234239
if (fHeader || !initialSync || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
235240
//pass an async signal to the UI thread
236-
QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
241+
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
237242
Q_ARG(int, height),
238243
Q_ARG(QDateTime, QDateTime::fromTime_t(blockTime)),
239244
Q_ARG(double, verificationProgress),
240245
Q_ARG(bool, fHeader));
246+
assert(invoked);
241247
nLastUpdateNotification = now;
242248
}
243249
}

src/qt/splashscreen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,12 @@ void SplashScreen::finish()
156156

157157
static void InitMessage(SplashScreen *splash, const std::string &message)
158158
{
159-
QMetaObject::invokeMethod(splash, "showMessage",
159+
bool invoked = QMetaObject::invokeMethod(splash, "showMessage",
160160
Qt::QueuedConnection,
161161
Q_ARG(QString, QString::fromStdString(message)),
162162
Q_ARG(int, Qt::AlignBottom|Qt::AlignHCenter),
163163
Q_ARG(QColor, QColor(55,55,55)));
164+
assert(invoked);
164165
}
165166

166167
static void ShowProgress(SplashScreen *splash, const std::string &title, int nProgress, bool resume_possible)

src/qt/test/wallettests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ uint256 SendCoins(CWallet& wallet, SendCoinsDialog& sendCoinsDialog, const CTxDe
6868
if (status == CT_NEW) txid = hash;
6969
}));
7070
ConfirmSend();
71-
QMetaObject::invokeMethod(&sendCoinsDialog, "on_sendButton_clicked");
71+
bool invoked = QMetaObject::invokeMethod(&sendCoinsDialog, "on_sendButton_clicked");
72+
assert(invoked);
7273
return txid;
7374
}
7475

src/qt/transactiontablemodel.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,11 @@ struct TransactionNotification
687687
{
688688
QString strHash = QString::fromStdString(hash.GetHex());
689689
qDebug() << "NotifyTransactionChanged: " + strHash + " status= " + QString::number(status);
690-
QMetaObject::invokeMethod(ttm, "updateTransaction", Qt::QueuedConnection,
690+
bool invoked = QMetaObject::invokeMethod(ttm, "updateTransaction", Qt::QueuedConnection,
691691
Q_ARG(QString, strHash),
692692
Q_ARG(int, status),
693693
Q_ARG(bool, showTransaction));
694+
assert(invoked);
694695
}
695696
private:
696697
uint256 hash;
@@ -725,12 +726,16 @@ static void ShowProgress(TransactionTableModel *ttm, const std::string &title, i
725726
if (nProgress == 100)
726727
{
727728
fQueueNotifications = false;
728-
if (vQueueNotifications.size() > 10) // prevent balloon spam, show maximum 10 balloons
729-
QMetaObject::invokeMethod(ttm, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, true));
729+
if (vQueueNotifications.size() > 10) { // prevent balloon spam, show maximum 10 balloons
730+
bool invoked = QMetaObject::invokeMethod(ttm, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, true));
731+
assert(invoked);
732+
}
730733
for (unsigned int i = 0; i < vQueueNotifications.size(); ++i)
731734
{
732-
if (vQueueNotifications.size() - i <= 10)
733-
QMetaObject::invokeMethod(ttm, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, false));
735+
if (vQueueNotifications.size() - i <= 10) {
736+
bool invoked = QMetaObject::invokeMethod(ttm, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, false));
737+
assert(invoked);
738+
}
734739

735740
vQueueNotifications[i].invoke(ttm);
736741
}

src/qt/walletcontroller.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wal
124124
} else {
125125
// Handler callback runs in a different thread so fix wallet model thread affinity.
126126
wallet_model->moveToThread(thread());
127-
QMetaObject::invokeMethod(this, "addWallet", Qt::QueuedConnection, Q_ARG(WalletModel*, wallet_model));
127+
bool invoked = QMetaObject::invokeMethod(this, "addWallet", Qt::QueuedConnection, Q_ARG(WalletModel*, wallet_model));
128+
assert(invoked);
128129
}
129130

130131
return wallet_model;

src/qt/walletmodel.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,15 @@ bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureStri
374374
static void NotifyUnload(WalletModel* walletModel)
375375
{
376376
qDebug() << "NotifyUnload";
377-
QMetaObject::invokeMethod(walletModel, "unload");
377+
bool invoked = QMetaObject::invokeMethod(walletModel, "unload");
378+
assert(invoked);
378379
}
379380

380381
static void NotifyKeyStoreStatusChanged(WalletModel *walletmodel)
381382
{
382383
qDebug() << "NotifyKeyStoreStatusChanged";
383-
QMetaObject::invokeMethod(walletmodel, "updateStatus", Qt::QueuedConnection);
384+
bool invoked = QMetaObject::invokeMethod(walletmodel, "updateStatus", Qt::QueuedConnection);
385+
assert(invoked);
384386
}
385387

386388
static void NotifyAddressBookChanged(WalletModel *walletmodel,
@@ -392,38 +394,43 @@ static void NotifyAddressBookChanged(WalletModel *walletmodel,
392394
QString strPurpose = QString::fromStdString(purpose);
393395

394396
qDebug() << "NotifyAddressBookChanged: " + strAddress + " " + strLabel + " isMine=" + QString::number(isMine) + " purpose=" + strPurpose + " status=" + QString::number(status);
395-
QMetaObject::invokeMethod(walletmodel, "updateAddressBook", Qt::QueuedConnection,
397+
bool invoked = QMetaObject::invokeMethod(walletmodel, "updateAddressBook", Qt::QueuedConnection,
396398
Q_ARG(QString, strAddress),
397399
Q_ARG(QString, strLabel),
398400
Q_ARG(bool, isMine),
399401
Q_ARG(QString, strPurpose),
400402
Q_ARG(int, status));
403+
assert(invoked);
401404
}
402405

403406
static void NotifyTransactionChanged(WalletModel *walletmodel, const uint256 &hash, ChangeType status)
404407
{
405408
Q_UNUSED(hash);
406409
Q_UNUSED(status);
407-
QMetaObject::invokeMethod(walletmodel, "updateTransaction", Qt::QueuedConnection);
410+
bool invoked = QMetaObject::invokeMethod(walletmodel, "updateTransaction", Qt::QueuedConnection);
411+
assert(invoked);
408412
}
409413

410414
static void ShowProgress(WalletModel *walletmodel, const std::string &title, int nProgress)
411415
{
412416
// emits signal "showProgress"
413-
QMetaObject::invokeMethod(walletmodel, "showProgress", Qt::QueuedConnection,
417+
bool invoked = QMetaObject::invokeMethod(walletmodel, "showProgress", Qt::QueuedConnection,
414418
Q_ARG(QString, QString::fromStdString(title)),
415419
Q_ARG(int, nProgress));
420+
assert(invoked);
416421
}
417422

418423
static void NotifyWatchonlyChanged(WalletModel *walletmodel, bool fHaveWatchonly)
419424
{
420-
QMetaObject::invokeMethod(walletmodel, "updateWatchOnlyFlag", Qt::QueuedConnection,
425+
bool invoked = QMetaObject::invokeMethod(walletmodel, "updateWatchOnlyFlag", Qt::QueuedConnection,
421426
Q_ARG(bool, fHaveWatchonly));
427+
assert(invoked);
422428
}
423429

424430
static void NotifyCanGetAddressesChanged(WalletModel* walletmodel)
425431
{
426-
QMetaObject::invokeMethod(walletmodel, "canGetAddressesChanged");
432+
bool invoked = QMetaObject::invokeMethod(walletmodel, "canGetAddressesChanged");
433+
assert(invoked);
427434
}
428435

429436
void WalletModel::subscribeToCoreSignals()

0 commit comments

Comments
 (0)