diff options
author | Peter Varga <pvarga@inf.u-szeged.hu> | 2023-01-18 13:38:00 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-01-20 17:32:22 +0000 |
commit | 2e9bd519a29da0a38fa326a597c3ffda8771483f (patch) | |
tree | 7c04a59f93fa4408bf504e9ddc8dec2a77f40d24 | |
parent | 81e97579055b6bb21c94ef164addea5fa9616e5b (diff) |
Improve error handling of argument parsing
As a corner case, QCoreApplication::arguments() might be empty. For
example, the embedder sets argc=0. It is invalid but doesn't crash
or warn.
base::CommandLine expects program name to be set and Chromium code might
use it. It is not possible to set program name if argv is not passed to
QCoreApplication.
This change does not handle this corner case but detects it, and warns
the user to not expect proper behavior.
Task-number: QTBUG-110157
Change-Id: Ibf14b11bbf8b8c72d8a1d8419377a25b311b9ebe
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
(cherry picked from commit 614d6639b875f53b21eaabd2d5928b84b59af707)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/core/web_engine_context.cpp | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/src/core/web_engine_context.cpp b/src/core/web_engine_context.cpp index ee90a5810..67d015e48 100644 --- a/src/core/web_engine_context.cpp +++ b/src/core/web_engine_context.cpp @@ -842,44 +842,52 @@ gpu::SyncPointManager *WebEngineContext::syncPointManager() base::CommandLine *WebEngineContext::initCommandLine(bool &useEmbeddedSwitches, bool &enableGLSoftwareRendering) { - if (base::CommandLine::CreateEmpty()) { - base::CommandLine* parsedCommandLine = base::CommandLine::ForCurrentProcess(); - QStringList appArgs = QCoreApplication::arguments(); - if (qEnvironmentVariableIsSet(kChromiumFlagsEnv)) { - appArgs = appArgs.mid(0, 1); // Take application name and drop the rest - appArgs.append(parseEnvCommandLine(qEnvironmentVariable(kChromiumFlagsEnv))); + if (!base::CommandLine::CreateEmpty()) + qFatal("base::CommandLine has been initialized unexpectedly."); + + base::CommandLine *parsedCommandLine = base::CommandLine::ForCurrentProcess(); + QStringList appArgs = QCoreApplication::arguments(); + if (qEnvironmentVariableIsSet(kChromiumFlagsEnv)) { + appArgs = appArgs.mid(0, 1); // Take application name and drop the rest + appArgs.append(parseEnvCommandLine(qEnvironmentVariable(kChromiumFlagsEnv))); + } else { + int index = appArgs.indexOf(QLatin1String("--webEngineArgs")); + if (index > -1) { + appArgs.erase(appArgs.begin() + 1, appArgs.begin() + index + 1); } else { - int index = appArgs.indexOf(QLatin1String("--webEngineArgs")); - if (index > -1) { - appArgs.erase(appArgs.begin() + 1, appArgs.begin() + index + 1); - } else { - appArgs = appArgs.mid(0, 1); - } + appArgs = appArgs.mid(0, 1); } + } #if defined(QTWEBENGINE_EMBEDDED_SWITCHES) - useEmbeddedSwitches = !appArgs.contains(QStringLiteral("--disable-embedded-switches")); + useEmbeddedSwitches = !appArgs.contains(QStringLiteral("--disable-embedded-switches")); #else - useEmbeddedSwitches = appArgs.contains(QStringLiteral("--enable-embedded-switches")); + useEmbeddedSwitches = appArgs.contains(QStringLiteral("--enable-embedded-switches")); #endif - enableGLSoftwareRendering = + enableGLSoftwareRendering = appArgs.removeAll(QStringLiteral("--enable-webgl-software-rendering")); - appArgs.removeAll(QStringLiteral("--disable-embedded-switches")); - appArgs.removeAll(QStringLiteral("--enable-embedded-switches")); + appArgs.removeAll(QStringLiteral("--disable-embedded-switches")); + appArgs.removeAll(QStringLiteral("--enable-embedded-switches")); - base::CommandLine::StringVector argv; - argv.resize(appArgs.size()); + base::CommandLine::StringVector argv; + argv.resize(appArgs.size()); #if defined(Q_OS_WIN) - for (int i = 0; i < appArgs.size(); ++i) - argv[i] = appArgs[i].toStdWString(); + for (int i = 0; i < appArgs.size(); ++i) + argv[i] = appArgs[i].toStdWString(); #else - for (int i = 0; i < appArgs.size(); ++i) - argv[i] = appArgs[i].toStdString(); + for (int i = 0; i < appArgs.size(); ++i) + argv[i] = appArgs[i].toStdString(); #endif - parsedCommandLine->InitFromArgv(argv); - return parsedCommandLine; - } else { - return base::CommandLine::ForCurrentProcess(); + parsedCommandLine->InitFromArgv(argv); + + if (QCoreApplication::arguments().empty()) { + // TODO: Replace this qWarning with a qFatal at the beginning of the function + // when the corresponding Active Qt issue gets fixed: QTBUG-110158. + qWarning("Argument list is empty, the program name is not passed to QCoreApplication. " + "Command line arguments might be ignored. Unexpected behavior may occur."); + Q_ASSERT(false); } + + return parsedCommandLine; } bool WebEngineContext::closingDown() |