From fee6283b85de8a06be10aee180b37ec3e479c9b7 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 5 Aug 2024 09:28:32 -0700 Subject: [PATCH] QUrl::toString: fix using of NormalizePathSegments and RemoveFilename We were overwriting the normalization if RemoveFilename was used. [ChangeLog][QtCore][QUrl] Fixed a bug that caused QUrl::toString(), QUrl::toEncoded() and QUrl::adjusted() to ignore QUrl::NormalizePathSegments if QUrl::RemoveFilename was set. Fixes: QTBUG-127711 Pick-to: 6.7 6.5 6.2 5.15 Change-Id: I8a96935cf6c742259c9dfffd17e8e1f7fec44cb6 Reviewed-by: Ahmad Samir (cherry picked from commit c9ff625865c355fb57c824b13f3cb4b26e53fbe7) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qurl.cpp | 4 +- tests/auto/corelib/io/qurl/tst_qurl.cpp | 68 +++++++++++++++++++------ 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 9fb96e9f84e..6bf59288fdb 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -916,10 +916,10 @@ inline void QUrlPrivate::appendPath(QString &appendTo, QUrl::FormattingOptions o QStringView thePathView(thePath); if (options & QUrl::RemoveFilename) { - const qsizetype slash = path.lastIndexOf(u'/'); + const qsizetype slash = thePathView.lastIndexOf(u'/'); if (slash == -1) return; - thePathView = QStringView{path}.left(slash + 1); + thePathView = thePathView.left(slash + 1); } // check if we need to remove trailing slashes if (options & QUrl::StripTrailingSlash) { diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index 2e1b5526c89..c4a5c077c99 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -2927,6 +2927,7 @@ void tst_QUrl::stripTrailingSlash_data() QTest::newRow("file root") << "file:///" << "file:///" << "file:///" << "file:///"; QTest::newRow("file_root_manyslashes") << "file://///" << "file:///" << "file://///" << "file:///"; QTest::newRow("no path") << "remote://" << "remote://" << "remote://" << "remote://"; + QTest::newRow("no authority") << "/root/test/../foo/bar" << "/root/test/../foo/bar" << "/root/test/../foo/" << "/root/test/../foo"; } void tst_QUrl::stripTrailingSlash() @@ -4370,30 +4371,67 @@ void tst_QUrl::normalizeRemotePaths_data() { QTest::addColumn("url"); QTest::addColumn("expected"); + QTest::addColumn("expectedNoFilename"); - QTest::newRow("dotdot-slashslash") << QUrl("http://qt-project.org/some/long/..//path") << "http://qt-project.org/some//path"; - QTest::newRow("slashslash-dotdot") << QUrl("http://qt-project.org/some//../path") << "http://qt-project.org/some/path"; - QTest::newRow("slashslash-dotdot2") << QUrl("http://qt-project.org/some//path/../") << "http://qt-project.org/some//"; - QTest::newRow("dot-slash") << QUrl("http://qt-project.org/some/./path") << "http://qt-project.org/some/path"; - QTest::newRow("slashslash-dot-slashslash") << QUrl("http://qt-project.org/some//.//path") << "http://qt-project.org/some///path"; - QTest::newRow("dot-slashslash") << QUrl("http://qt-project.org/some/.//path") << "http://qt-project.org/some//path"; - QTest::newRow("multiple-slashes") << QUrl("http://qt-project.org/some//path") << "http://qt-project.org/some//path"; - QTest::newRow("multiple-slashes4") << QUrl("http://qt-project.org/some////path") << "http://qt-project.org/some////path"; - QTest::newRow("slashes-at-end") << QUrl("http://qt-project.org/some//") << "http://qt-project.org/some//"; - QTest::newRow("dot-dotdot") << QUrl("http://qt-project.org/path/./../") << "http://qt-project.org/"; - QTest::newRow("slash-dot-slash-dot-slash") << QUrl("http://qt-project.org/path//.//.//") << "http://qt-project.org/path////"; - QTest::newRow("dotdot") << QUrl("http://qt-project.org/../") << "http://qt-project.org/"; - QTest::newRow("dotdot-dotdot") << QUrl("http://qt-project.org/path/../../") << "http://qt-project.org/"; - QTest::newRow("dot-dotdot-tail") << QUrl("http://qt-project.org/stem/path/./../tail") << "http://qt-project.org/stem/tail"; - QTest::newRow("slash-dotdot-slash-tail") << QUrl("http://qt-project.org/stem/path//..//tail") << "http://qt-project.org/stem/path//tail"; + QTest::newRow("dotdot-slashslash") << QUrl("http://qt-project.org/some/long/..//path") + << "http://qt-project.org/some//path" + << "http://qt-project.org/some//"; + QTest::newRow("slashslash-dotdot") << QUrl("http://qt-project.org/some//../path") + << "http://qt-project.org/some/path" + << "http://qt-project.org/some/"; + QTest::newRow("slashslash-dotdot2") << QUrl("http://qt-project.org/some//path/../") + << "http://qt-project.org/some//" + << "http://qt-project.org/some//"; + QTest::newRow("dot-slash") << QUrl("http://qt-project.org/some/./path") + << "http://qt-project.org/some/path" + << "http://qt-project.org/some/"; + QTest::newRow("slashslash-dot-slashslash") << QUrl("http://qt-project.org/some//.//path") + << "http://qt-project.org/some///path" + << "http://qt-project.org/some///"; + QTest::newRow("dot-slashslash") << QUrl("http://qt-project.org/some/.//path") + << "http://qt-project.org/some//path" + << "http://qt-project.org/some//"; + QTest::newRow("multiple-slashes") << QUrl("http://qt-project.org/some//path") + << "http://qt-project.org/some//path" + << "http://qt-project.org/some//"; + QTest::newRow("multiple-slashes4") << QUrl("http://qt-project.org/some////path") + << "http://qt-project.org/some////path" + << "http://qt-project.org/some////"; + QTest::newRow("slashes-at-end") << QUrl("http://qt-project.org/some//") + << "http://qt-project.org/some//" + << "http://qt-project.org/some//"; + QTest::newRow("dot-dotdot") << QUrl("http://qt-project.org/path/./../") + << "http://qt-project.org/" + << "http://qt-project.org/"; + QTest::newRow("slash-dot-slash-dot-slash") << QUrl("http://qt-project.org/path//.//.//") + << "http://qt-project.org/path////" + << "http://qt-project.org/path////"; + QTest::newRow("dotdot") << QUrl("http://qt-project.org/../") + << "http://qt-project.org/" + << "http://qt-project.org/"; + QTest::newRow("dotdot-tail") << QUrl("http://qt-project.org/root/test/../foo/bar") + << "http://qt-project.org/root/foo/bar" + << "http://qt-project.org/root/foo/"; + QTest::newRow("dotdot-dotdot") << QUrl("http://qt-project.org/path/../../") + << "http://qt-project.org/" + << "http://qt-project.org/"; + QTest::newRow("dot-dotdot-tail") << QUrl("http://qt-project.org/stem/path/./../tail") + << "http://qt-project.org/stem/tail" + << "http://qt-project.org/stem/"; + QTest::newRow("slash-dotdot-slash-tail") << QUrl("http://qt-project.org/stem/path//..//tail") + << "http://qt-project.org/stem/path//tail" + << "http://qt-project.org/stem/path//"; } void tst_QUrl::normalizeRemotePaths() { QFETCH(QUrl, url); QFETCH(QString, expected); + QFETCH(QString, expectedNoFilename); QCOMPARE(url.adjusted(QUrl::NormalizePathSegments).toString(), expected); + QCOMPARE(url.adjusted(QUrl::NormalizePathSegments | QUrl::RemoveFilename).toString(), + expectedNoFilename); } QTEST_MAIN(tst_QUrl)