Platform: unix - avoid modifying the environment

Modifying the process environment can cause crashes in application code
that accesses the environment via non-qt functions on worker threads.
When launching a process, we can avoid modifying the environment of the
caller by using QProcess with setEnvironment. The codepaths without
QProcess support is still prone to these issues and could potentially be
improved via execve

Task-number: QTBUG-129222
Pick-to: 6.8 6.9
Change-Id: I4e2d93abaa0e392b341041faaae0ffd11e225bcb
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Tim Blechmann 2024-12-17 12:59:24 +08:00
parent 28d0e658e2
commit 673400679d

View File

@ -134,27 +134,36 @@ static inline bool detectWebBrowser(const QByteArray &desktop,
static inline bool launch(const QString &launcher, const QUrl &url,
const QString &xdgActivationToken)
{
if (!xdgActivationToken.isEmpty()) {
qputenv("XDG_ACTIVATION_TOKEN", xdgActivationToken.toUtf8());
}
const QString command = launcher + u' ' + QLatin1StringView(url.toEncoded());
if (debug)
qDebug("Launching %s", qPrintable(command));
#if !QT_CONFIG(process)
if (!xdgActivationToken.isEmpty())
qputenv("XDG_ACTIVATION_TOKEN", xdgActivationToken.toUtf8());
const bool ok = ::system(qPrintable(command + " &"_L1));
#else
if (!xdgActivationToken.isEmpty())
qunsetenv("XDG_ACTIVATION_TOKEN");
# else
QStringList args = QProcess::splitCommand(command);
bool ok = false;
if (!args.isEmpty()) {
QString program = args.takeFirst();
ok = QProcess::startDetached(program, args);
QProcess process;
process.setProgram(program);
process.setArguments(args);
if (!xdgActivationToken.isEmpty()) {
auto env = QProcessEnvironment::systemEnvironment();
env.insert(u"XDG_ACTIVATION_TOKEN"_s, xdgActivationToken);
process.setEnvironment(env.toStringList());
}
ok = process.startDetached(nullptr);
}
#endif
# endif
if (!ok)
qWarning("Launch failed (%s)", qPrintable(command));
qunsetenv("XDG_ACTIVATION_TOKEN");
return ok;
}