diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2021-04-23 14:42:03 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2021-04-29 19:54:39 -0700 |
commit | 21b123f8881d6680208e5b08aee03c95f883b164 (patch) | |
tree | 2c2696d5581c368c7142700ae029f52ee4ec2c4d /src/corelib/kernel/qcoreapplication.cpp | |
parent | ccd47237ee027af64b531ba9beca9397d3cde04c (diff) |
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>
Diffstat (limited to 'src/corelib/kernel/qcoreapplication.cpp')
-rw-r--r-- | src/corelib/kernel/qcoreapplication.cpp | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 2e718d11fc..3fbff3cb8c 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -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(); } |