QDir: change qt_{cleanPath,normalizePathSegments} to returning bool

Since qt_normalizePathSegments very often modifies the path, pass that
as pointer, and return a boolean with whether the path is attempting to
go up above the root.

Change-Id: I851fcb94db4606a6bd97fffd81910930dea8222a
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
(cherry picked from commit 0314491abac092b20ebefc05e2e9f27fd038fc38)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2024-09-03 18:21:29 -05:00 committed by Qt Cherry-pick Bot
parent 2ce3153b83
commit 16571ac456
3 changed files with 18 additions and 28 deletions

View File

@ -970,7 +970,7 @@ QString QDir::fromNativeSeparators(const QString &pathName)
#endif #endif
} }
static QString qt_cleanPath(const QString &path, bool *ok = nullptr); static bool qt_cleanPath(QString *path);
/*! /*!
Changes the QDir's directory to \a dirName. Changes the QDir's directory to \a dirName.
@ -992,7 +992,8 @@ bool QDir::cd(const QString &dirName)
return true; return true;
QString newPath; QString newPath;
if (isAbsolutePath(dirName)) { if (isAbsolutePath(dirName)) {
newPath = qt_cleanPath(dirName); newPath = dirName;
qt_cleanPath(&newPath);
} else { } else {
newPath = d->dirEntry.filePath(); newPath = d->dirEntry.filePath();
if (!newPath.endsWith(u'/')) if (!newPath.endsWith(u'/'))
@ -1001,9 +1002,7 @@ bool QDir::cd(const QString &dirName)
if (dirName.indexOf(u'/') >= 0 if (dirName.indexOf(u'/') >= 0
|| dirName == ".."_L1 || dirName == ".."_L1
|| d->dirEntry.filePath() == u'.') { || d->dirEntry.filePath() == u'.') {
bool ok; if (!qt_cleanPath(&newPath))
newPath = qt_cleanPath(newPath, &ok);
if (!ok)
return false; return false;
/* /*
If newPath starts with .., we convert it to absolute to If newPath starts with .., we convert it to absolute to
@ -2358,25 +2357,15 @@ bool qt_normalizePathSegments(QString *path, QDirPrivate::PathNormalizations fla
return ok || prefixLength == 0; return ok || prefixLength == 0;
} }
QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormalizations flags, bool *ok) static bool qt_cleanPath(QString *path)
{ {
// temporary compat if (path->isEmpty())
QString copy = name; return true;
bool r = qt_normalizePathSegments(&copy, flags);
if (ok)
*ok = r;
return copy;
}
static QString qt_cleanPath(const QString &path, bool *ok) QString &ret = *path;
{ ret = QDir::fromNativeSeparators(ret);
if (path.isEmpty()) { auto normalization = OSSupportsUncPaths ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization;
Q_ASSERT(!ok); // The only caller passing ok knows its path is non-empty bool ok = qt_normalizePathSegments(&ret, normalization);
return path;
}
QString name = QDir::fromNativeSeparators(path);
QString ret = qt_normalizePathSegments(name, OSSupportsUncPaths ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization, ok);
// Strip away last slash except for root directories // Strip away last slash except for root directories
if (ret.size() > 1 && ret.endsWith(u'/')) { if (ret.size() > 1 && ret.endsWith(u'/')) {
@ -2386,7 +2375,7 @@ static QString qt_cleanPath(const QString &path, bool *ok)
ret.chop(1); ret.chop(1);
} }
return ret; return ok;
} }
/*! /*!
@ -2403,7 +2392,9 @@ static QString qt_cleanPath(const QString &path, bool *ok)
*/ */
QString QDir::cleanPath(const QString &path) QString QDir::cleanPath(const QString &path)
{ {
return qt_cleanPath(path); QString ret = path;
qt_cleanPath(&ret);
return ret;
} }
/*! /*!

View File

@ -80,8 +80,7 @@ public:
Q_DECLARE_OPERATORS_FOR_FLAGS(QDirPrivate::PathNormalizations) Q_DECLARE_OPERATORS_FOR_FLAGS(QDirPrivate::PathNormalizations)
Q_AUTOTEST_EXPORT QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormalizations flags, bool *ok = nullptr); Q_AUTOTEST_EXPORT bool qt_normalizePathSegments(QString *path, QDirPrivate::PathNormalizations flags);
bool qt_normalizePathSegments(QString *path, QDirPrivate::PathNormalizations flags);
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -1453,8 +1453,8 @@ void tst_QDir::normalizePathSegments()
QFETCH(UncHandling, uncHandling); QFETCH(UncHandling, uncHandling);
QFETCH(QString, expected); QFETCH(QString, expected);
// for QDirPrivate::RemotePath, see tst_QUrl::resolving // for QDirPrivate::RemotePath, see tst_QUrl::resolving
QString cleaned = qt_normalizePathSegments(path, uncHandling == HandleUnc ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization); qt_normalizePathSegments(&path, uncHandling == HandleUnc ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization);
QCOMPARE(cleaned, expected); QCOMPARE(path, expected);
} }
# endif //QT_BUILD_INTERNAL # endif //QT_BUILD_INTERNAL