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>
This commit is contained in:
David Faure 2014-02-07 11:20:58 +01:00 committed by The Qt Project
parent 4965cf78c1
commit 5c9d671bfb
2 changed files with 25 additions and 4 deletions

View File

@ -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

View File

@ -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"