summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2014-02-07 11:20:58 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-10 11:00:41 +0100
commit5c9d671bfb5b5111069aadc9bf3f742f369c74ae (patch)
tree3ff758089963ad4552604206c42b9138e6321cc7
parent4965cf78c1d9800944cec53babcf6be1cadf5940 (diff)
QStandardPaths: fix empty path in XDG_DATA_DIRS being treated as '/'.
The basedir xdg spec says: "All paths set in these environment variables must be absolute. If an implementation encounters a relative path in any of these variables it should consider the path invalid and ignore it." Therefore we ignore relative paths including the empty string. Change-Id: I8f779b78981018051b16de23b2514f2e62b7ab39 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/io/qstandardpaths_unix.cpp15
-rw-r--r--tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp14
2 files changed, 25 insertions, 4 deletions
diff --git a/src/corelib/io/qstandardpaths_unix.cpp b/src/corelib/io/qstandardpaths_unix.cpp
index 10ca20629e..e2ed7c3766 100644
--- a/src/corelib/io/qstandardpaths_unix.cpp
+++ b/src/corelib/io/qstandardpaths_unix.cpp
@@ -259,10 +259,17 @@ static QStringList xdgDataDirs()
dirs.append(QString::fromLatin1("/usr/local/share"));
dirs.append(QString::fromLatin1("/usr/share"));
} else {
- dirs = xdgDataDirsEnv.split(QLatin1Char(':'));
- // Normalize paths
- for (int i = 0; i < dirs.count(); i++)
- dirs[i] = QDir::cleanPath(dirs.at(i));
+ dirs = xdgDataDirsEnv.split(QLatin1Char(':'), QString::SkipEmptyParts);
+
+ // Normalize paths, skip relative paths
+ QMutableListIterator<QString> it(dirs);
+ while (it.hasNext()) {
+ const QString dir = it.next();
+ if (!dir.startsWith(QLatin1Char('/')))
+ it.remove();
+ else
+ it.setValue(QDir::cleanPath(dir));
+ }
// Remove duplicates from the list, there's no use for duplicated
// paths in XDG_DATA_DIRS - if it's not found in the given
diff --git a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp
index 5c0485d228..4503f6fcbc 100644
--- a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp
+++ b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp
@@ -77,6 +77,7 @@ private slots:
void testAllWritableLocations_data();
void testAllWritableLocations();
void testCleanPath();
+ void testXdgPathCleanup();
private:
#ifdef Q_XDG_PLATFORM
@@ -491,6 +492,19 @@ void tst_qstandardpaths::testCleanPath()
}
}
+void tst_qstandardpaths::testXdgPathCleanup()
+{
+#ifdef Q_XDG_PLATFORM
+ setCustomLocations();
+ const QString uncleanGlobalAppDir = "/./" + QFile::encodeName(m_globalAppDir);
+ qputenv("XDG_DATA_DIRS", QFile::encodeName(uncleanGlobalAppDir) + "::relative/path");
+ const QStringList appsDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
+ QVERIFY(!appsDirs.contains("/applications"));
+ QVERIFY(!appsDirs.contains(uncleanGlobalAppDir + "/applications"));
+ QVERIFY(!appsDirs.contains("relative/path/applications"));
+#endif
+}
+
QTEST_MAIN(tst_qstandardpaths)
#include "tst_qstandardpaths.moc"