QCoreApplication::applicationFilePath: don't check for existence twice

A file that doesn't exist can't have a canonical path, so we don't need
to check fi.exists() before calling fi.canonicalFilePath(). Also avoids
a TOCTOU mistake: if the exists() returned true, we would store whatever
canonicalFilePath() returned, even an empty string (which can also
happen if realpath(3) fails for some reason).

Change-Id: I7a386ad4f0cb4e2ba629fffd16789aaa8367e641
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
This commit is contained in:
Thiago Macieira 2021-04-23 14:42:03 -07:00
parent ccd47237ee
commit 21b123f888

View File

@ -2332,18 +2332,9 @@ QString QCoreApplication::applicationFilePath()
if (QCoreApplicationPrivate::cachedApplicationFilePath)
return *QCoreApplicationPrivate::cachedApplicationFilePath;
QString qAppFileName_str = qAppFileName();
if (!qAppFileName_str.isEmpty()) {
QFileInfo fi(qAppFileName_str);
if (fi.exists()) {
QCoreApplicationPrivate::setApplicationFilePath(fi.canonicalFilePath());
return *QCoreApplicationPrivate::cachedApplicationFilePath;
}
}
if (!arguments().isEmpty()) {
QString absPath = qAppFileName();
if (absPath.isEmpty() && !arguments().isEmpty()) {
QString argv0 = QFile::decodeName(arguments().at(0).toLocal8Bit());
QString absPath;
if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) {
/*
@ -2366,14 +2357,13 @@ QString QCoreApplication::applicationFilePath()
}
absPath = QDir::cleanPath(absPath);
QFileInfo fi(absPath);
if (fi.exists()) {
QCoreApplicationPrivate::setApplicationFilePath(fi.canonicalFilePath());
return *QCoreApplicationPrivate::cachedApplicationFilePath;
}
}
absPath = QFileInfo(absPath).canonicalFilePath();
if (!absPath.isEmpty()) {
QCoreApplicationPrivate::setApplicationFilePath(absPath);
return *QCoreApplicationPrivate::cachedApplicationFilePath;
}
return QString();
}