From f90c94dae7cfb519c9e3b3ed64c2daf8813671ab Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Mon, 10 Jul 2023 06:52:30 +0300 Subject: [PATCH] kdecore: strip the trailing slash when passing the URL path to kPathDirectory() from KUrl::upUrl() Signed-off-by: Ivailo Monev --- kdecore/io/kurl.cpp | 6 ++++-- kdecore/tests/kurltest.cpp | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/kdecore/io/kurl.cpp b/kdecore/io/kurl.cpp index f32a05f8..fa8fb817 100644 --- a/kdecore/io/kurl.cpp +++ b/kdecore/io/kurl.cpp @@ -64,6 +64,7 @@ static QString kPathDirectory(const QString &path) if (lastslash == 0) { return QString(QLatin1Char('/')); } + Q_ASSERT(!path.endsWith(QLatin1Char('/'))); QString result = path.left(lastslash); result.append(QLatin1Char('/')); return result; @@ -614,7 +615,8 @@ KUrl KUrl::upUrl() const return result; } - if (QDir::cleanPath(urlpath).count(QLatin1Char('/')) <= 1) { + const QString cleanurlpath = QDir::cleanPath(urlpath); + if (cleanurlpath.count(QLatin1Char('/')) <= 1) { // something like /home KUrl result(*this); result.setPath(QLatin1String("/")); @@ -627,7 +629,7 @@ KUrl KUrl::upUrl() const QString newpath; if (urlpath.endsWith(QLatin1Char('/'))) { // assuming it is directory - newpath = kPathDirectory(urlpath); + newpath = kPathDirectory(cleanurlpath); } else { // the only way to be sure is to stat() then KDE_struct_stat statbuff; diff --git a/kdecore/tests/kurltest.cpp b/kdecore/tests/kurltest.cpp index 778c255b..74d67097 100644 --- a/kdecore/tests/kurltest.cpp +++ b/kdecore/tests/kurltest.cpp @@ -48,6 +48,9 @@ void KUrlTest::testUpUrl_data() QTest::newRow("local file 3") << KUrl("kde//foo?bar=baz#foobar") << KUrl("../kde/"); + QTest::newRow("local file 4 - trailing slash") + << KUrl("/home/foo/bar/") + << KUrl("/home/foo/"); QTest::newRow("ftp url") << KUrl("ftp://ftp.kde.org/foo?bar=baz#foobar") << KUrl("ftp://ftp.kde.org/");