From f7600615c23c27f285bac67303bda5670e6b4582 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Tue, 20 Jul 2021 15:07:05 +0200 Subject: QFileSelector: Preserve path to root directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While QFileSelector is documented to work on files, select accepts arbitrary URLs. Moreover, the QML engine can end up intercepting arbitrary (user provided) URLs, including to directories. Prior to this change, passing "file:///" or "/" to the function would break: We would temporarily get a "//" path, which is invalid, and thus we would return an invalid in the end. Prevent this by only appending a slash to the path when it doesn't have one. Fixes: QTBUG-85410 Change-Id: I1d2807a9d225df611c3a5e871e3c1d90a6a25953 Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit 7b1b1d990b5cb5cf7a10e689803f9ab6729d1e7f) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qfileselector.cpp | 5 ++++- tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/corelib/io/qfileselector.cpp b/src/corelib/io/qfileselector.cpp index 1a58da1e18..dd6257a885 100644 --- a/src/corelib/io/qfileselector.cpp +++ b/src/corelib/io/qfileselector.cpp @@ -278,7 +278,10 @@ QString QFileSelectorPrivate::select(const QString &filePath) const Q_Q(const QFileSelector); QFileInfo fi(filePath); - QString ret = selectionHelper(fi.path().isEmpty() ? QString() : fi.path() + QLatin1Char('/'), + QString pathString; + if (auto path = fi.path(); !path.isEmpty()) + pathString = path.endsWith(u'/') ? path : path + u'/'; + QString ret = selectionHelper(pathString, fi.fileName(), q->allSelectors()); if (!ret.isEmpty()) diff --git a/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp b/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp index d8639b6864..3d79507050 100644 --- a/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp +++ b/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp @@ -215,6 +215,9 @@ void tst_QFileSelector::urlConvenience_data() strUrlWithFragment = QString("file:") + testWithQueryAndFragment; QTest::newRow("file with query and fragment too") << QUrl(strUrlWithFragment) << (QStringList()) << QUrl(strUrlWithFragment); + // preserve path to root + QTest::newRow("path to root") << QUrl("file:///") << (QStringList()) << QUrl("file:///"); + // http://qt-project.org/images/qtdn/sprites-combined-latest.png is chosen as a representative real world URL // But note that this test is checking that http urls are NOT selected so it shouldn't be checked QUrl testHttpUrl("http://qt-project.org/images/sprites-combined-latest.png"); -- cgit v1.2.3