kdecore: safety-net for KUrl::upUrl(), also append trailing slash from KUrl::directory()

relative paths are tricky

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-07-03 20:25:43 +03:00
parent a051b1b284
commit bce72b0b24
4 changed files with 40 additions and 3 deletions

View file

@ -590,13 +590,23 @@ QString KUrl::directory(AdjustPathOption trailing) const
KUrl KUrl::upUrl() const KUrl KUrl::upUrl() const
{ {
const QString urlpath = QUrl::path(); const QString urlpath = QUrl::path();
if (urlpath.isEmpty()) { if (urlpath.isEmpty() || urlpath == QLatin1String("/")) {
return *this; return *this;
} }
static const QString s_dotdotslash = QString::fromLatin1("../");
if (urlpath.count(s_dotdotslash) >= 10) {
// way too long, going to reach the path limit with that
KUrl result(*this);
result.setPath(QLatin1String("/"));
result.setQuery(QString());
result.setFragment(QString());
return result;
}
if (QDir::isRelativePath(urlpath)) { if (QDir::isRelativePath(urlpath)) {
KUrl result(*this); KUrl result(*this);
QString newpath = QString::fromLatin1("../"); QString newpath = s_dotdotslash;
newpath.append(kPathDirectory(urlpath)); newpath.append(kPathDirectory(urlpath));
result.setPath(newpath); result.setPath(newpath);
result.setQuery(QString()); result.setQuery(QString());
@ -604,6 +614,15 @@ KUrl KUrl::upUrl() const
return result; return result;
} }
if (QDir::cleanPath(urlpath).count(QLatin1Char('/')) <= 1) {
// something like /home
KUrl result(*this);
result.setPath(QLatin1String("/"));
result.setQuery(QString());
result.setFragment(QString());
return result;
}
if (isLocalFile()) { if (isLocalFile()) {
QString newpath; QString newpath;
if (urlpath.endsWith(QLatin1Char('/'))) { if (urlpath.endsWith(QLatin1Char('/'))) {

View file

@ -439,7 +439,7 @@ public:
* <tt>file:///hallo/torben</tt> would return "hallo/". The returned string is decoded. * <tt>file:///hallo/torben</tt> would return "hallo/". The returned string is decoded.
* QString() is returned when there is no path. * QString() is returned when there is no path.
*/ */
QString directory(AdjustPathOption trailing = RemoveTrailingSlash) const; QString directory(AdjustPathOption trailing = AddTrailingSlash) const;
/** /**
* Set the directory to @p dir, leaving the filename empty. * Set the directory to @p dir, leaving the filename empty.

View file

@ -52,6 +52,10 @@ void KUrlTest::testUpUrl_data()
<< KUrl("ftp://ftp.kde.org/foo?bar=baz#foobar") << KUrl("ftp://ftp.kde.org/foo?bar=baz#foobar")
<< KUrl("ftp://ftp.kde.org/"); << KUrl("ftp://ftp.kde.org/");
} }
void KUrlTest::testUpUrl2_data()
{
testUpUrl_data();
}
void KUrlTest::testUpUrl() void KUrlTest::testUpUrl()
{ {
@ -69,6 +73,18 @@ void KUrlTest::testUpUrl()
QCOMPARE(newPath[newPath.size() - 1], QChar::fromLatin1('/')); QCOMPARE(newPath[newPath.size() - 1], QChar::fromLatin1('/'));
} }
void KUrlTest::testUpUrl2()
{
QFETCH(KUrl, url);
QFETCH(KUrl, url2);
KUrl copy(url);
while (copy.hasPath() && copy.path() != QLatin1String("/")) {
copy = copy.upUrl();
// qDebug() << Q_FUNC_INFO << copy.path();
}
}
void KUrlTest::testHash_data() void KUrlTest::testHash_data()
{ {
QTest::addColumn<KUrl>("url"); QTest::addColumn<KUrl>("url");

View file

@ -27,6 +27,8 @@ class KUrlTest : public QObject
private Q_SLOTS: private Q_SLOTS:
void testUpUrl_data(); void testUpUrl_data();
void testUpUrl(); void testUpUrl();
void testUpUrl2_data();
void testUpUrl2();
void testHash_data(); void testHash_data();
void testHash(); void testHash();
void testQueryAndFragment_data(); void testQueryAndFragment_data();