Merge bitcoin-core/gui#742: Exit and show error if unrecognized command line args are present

51e4dc49f5335b5bae6c14606d1cc653a08a96b1 gui: Show error if unrecognized command line args are present (John Moffett)

Pull request description:

  Fixes https://github.com/bitcoin-core/gui/issues/741

  Starting bitcoin-qt with non-hyphen ("-") arguments causes it to silently ignore any later valid options. For instance, invoking `bitcoin-qt -server=1 foo -regtest` on a fresh install will run `mainnet` instead of `regtest`.

  This change makes the client exit with an error message if any such "loose" arguments are encountered. This mirrors how `bitcoind` handles it:

  c6287faae4/src/bitcoind.cpp (L127-L132)

  However, BIP-21 `bitcoin:` payment URIs are still allowed, but only if they're not followed by any additional options.

ACKs for top commit:
  maflcko:
    lgtm ACK 51e4dc49f5335b5bae6c14606d1cc653a08a96b1
  hernanmarino:
    tested ACK 51e4dc49f5335b5bae6c14606d1cc653a08a96b1
  pablomartin4btc:
    tACK 51e4dc49f5335b5bae6c14606d1cc653a08a96b1
  hebasto:
    ACK 51e4dc49f5335b5bae6c14606d1cc653a08a96b1, I have reviewed the code and it looks OK.

Tree-SHA512: 3997a7a9a747314f13e118aee63e8679e00ed832d9c6f115559a4c39c9c4091572207c60e362cb4c19fc8da980d4b0b040050aa70c5ef84a855cb7e3568bbf13
This commit is contained in:
Hennadii Stepanov 2023-10-25 13:05:04 +01:00
commit afa081a39b
No known key found for this signature in database
GPG Key ID: 410108112E7EA81F
2 changed files with 30 additions and 0 deletions

View File

@ -547,6 +547,34 @@ int GuiMain(int argc, char* argv[])
return EXIT_FAILURE;
}
// Error out when loose non-argument tokens are encountered on command line
// However, allow BIP-21 URIs only if no options follow
bool payment_server_token_seen = false;
for (int i = 1; i < argc; i++) {
QString arg(argv[i]);
bool invalid_token = !arg.startsWith("-");
#ifdef ENABLE_WALLET
if (arg.startsWith(BITCOIN_IPC_PREFIX, Qt::CaseInsensitive)) {
invalid_token &= false;
payment_server_token_seen = true;
}
#endif
if (payment_server_token_seen && arg.startsWith("-")) {
InitError(Untranslated(strprintf("Options ('%s') cannot follow a BIP-21 payment URI", argv[i])));
QMessageBox::critical(nullptr, PACKAGE_NAME,
// message cannot be translated because translations have not been initialized
QString::fromStdString("Options ('%1') cannot follow a BIP-21 payment URI").arg(QString::fromStdString(argv[i])));
return EXIT_FAILURE;
}
if (invalid_token) {
InitError(Untranslated(strprintf("Command line contains unexpected token '%s', see bitcoin-qt -h for a list of options.", argv[i])));
QMessageBox::critical(nullptr, PACKAGE_NAME,
// message cannot be translated because translations have not been initialized
QString::fromStdString("Command line contains unexpected token '%1', see bitcoin-qt -h for a list of options.").arg(QString::fromStdString(argv[i])));
return EXIT_FAILURE;
}
}
// Now that the QApplication is setup and we have parsed our parameters, we can set the platform style
app.setupPlatformStyle();

View File

@ -54,6 +54,8 @@ class QLocalServer;
class QUrl;
QT_END_NAMESPACE
extern const QString BITCOIN_IPC_PREFIX;
class PaymentServer : public QObject
{
Q_OBJECT