diff options
author | Bernd Weimer <bweimer@blackberry.com> | 2013-05-27 10:23:07 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-06-05 16:11:02 +0200 |
commit | fd956af25745854690bb2d1f0837bf8386c8bc1d (patch) | |
tree | c8f4cc18646a9117445fd8ef59dd240e321e9892 /src | |
parent | 4f2c96eaa8bfa4d8a6dfb92096e4e4030d0cdea7 (diff) |
Fixed evaluation of application file path
Empty arguments list could lead to crash, due to access of first element.
Besides, empty application file path will be cached now universally.
Change-Id: Ibe1a668da364d87d8431567dfc999cb394686081
Reviewed-by: Sérgio Martins <sergio.martins.qnx@kdab.com>
Reviewed-by: Fabian Bumberger <fbumberger@rim.com>
Reviewed-by: Thomas McGuire <thomas.mcguire@kdab.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/kernel/qcoreapplication.cpp | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 100e014e99..878962ce04 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -1952,7 +1952,6 @@ QString QCoreApplication::applicationFilePath() char buff[maximum_path+1]; if (_cmdname(buff)) { d->cachedApplicationFilePath = QDir::cleanPath(QString::fromLocal8Bit(buff)); - return d->cachedApplicationFilePath; } else { qWarning("QCoreApplication::applicationFilePath: _cmdname() failed"); // _cmdname() won't fail, but just in case, fallback to the old method @@ -1961,11 +1960,11 @@ QString QCoreApplication::applicationFilePath() if (!executables.empty()) { //We assume that there is only one executable in the folder d->cachedApplicationFilePath = dir.absoluteFilePath(executables.first()); - return d->cachedApplicationFilePath; } else { - return QString(); + d->cachedApplicationFilePath = QString(); } } + return d->cachedApplicationFilePath; #elif defined(Q_OS_MAC) QString qAppFileName_str = qAppFileName(); if(!qAppFileName_str.isEmpty()) { @@ -1984,34 +1983,38 @@ QString QCoreApplication::applicationFilePath() return d->cachedApplicationFilePath; } # endif + if (!arguments().isEmpty()) { + QString argv0 = QFile::decodeName(arguments().at(0).toLocal8Bit()); + QString absPath; + + if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) { + /* + If argv0 starts with a slash, it is already an absolute + file path. + */ + absPath = argv0; + } else if (argv0.contains(QLatin1Char('/'))) { + /* + If argv0 contains one or more slashes, it is a file path + relative to the current directory. + */ + absPath = QDir::current().absoluteFilePath(argv0); + } else { + /* + Otherwise, the file path has to be determined using the + PATH environment variable. + */ + absPath = QStandardPaths::findExecutable(argv0); + } + + absPath = QDir::cleanPath(absPath); - QString argv0 = QFile::decodeName(arguments().at(0).toLocal8Bit()); - QString absPath; - - if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) { - /* - If argv0 starts with a slash, it is already an absolute - file path. - */ - absPath = argv0; - } else if (argv0.contains(QLatin1Char('/'))) { - /* - If argv0 contains one or more slashes, it is a file path - relative to the current directory. - */ - absPath = QDir::current().absoluteFilePath(argv0); + QFileInfo fi(absPath); + d->cachedApplicationFilePath = fi.exists() ? fi.canonicalFilePath() : QString(); } else { - /* - Otherwise, the file path has to be determined using the - PATH environment variable. - */ - absPath = QStandardPaths::findExecutable(argv0); + d->cachedApplicationFilePath = QString(); } - absPath = QDir::cleanPath(absPath); - - QFileInfo fi(absPath); - d->cachedApplicationFilePath = fi.exists() ? fi.canonicalFilePath() : QString(); return d->cachedApplicationFilePath; #endif } |