From 16571ac456819ed07a81d1997edc1e99b20a384a Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 3 Sep 2024 18:21:29 -0500 Subject: [PATCH] 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 Reviewed-by: David Faure (cherry picked from commit 0314491abac092b20ebefc05e2e9f27fd038fc38) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qdir.cpp | 39 ++++++++++--------------- src/corelib/io/qdir_p.h | 3 +- tests/auto/corelib/io/qdir/tst_qdir.cpp | 4 +-- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 606d2a97c78..2495ae93c73 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -970,7 +970,7 @@ QString QDir::fromNativeSeparators(const QString &pathName) #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. @@ -992,7 +992,8 @@ bool QDir::cd(const QString &dirName) return true; QString newPath; if (isAbsolutePath(dirName)) { - newPath = qt_cleanPath(dirName); + newPath = dirName; + qt_cleanPath(&newPath); } else { newPath = d->dirEntry.filePath(); if (!newPath.endsWith(u'/')) @@ -1001,9 +1002,7 @@ bool QDir::cd(const QString &dirName) if (dirName.indexOf(u'/') >= 0 || dirName == ".."_L1 || d->dirEntry.filePath() == u'.') { - bool ok; - newPath = qt_cleanPath(newPath, &ok); - if (!ok) + if (!qt_cleanPath(&newPath)) return false; /* 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; } -QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormalizations flags, bool *ok) +static bool qt_cleanPath(QString *path) { - // temporary compat - QString copy = name; - bool r = qt_normalizePathSegments(©, flags); - if (ok) - *ok = r; - return copy; -} + if (path->isEmpty()) + return true; -static QString qt_cleanPath(const QString &path, bool *ok) -{ - if (path.isEmpty()) { - Q_ASSERT(!ok); // The only caller passing ok knows its path is non-empty - return path; - } - - QString name = QDir::fromNativeSeparators(path); - QString ret = qt_normalizePathSegments(name, OSSupportsUncPaths ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization, ok); + QString &ret = *path; + ret = QDir::fromNativeSeparators(ret); + auto normalization = OSSupportsUncPaths ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization; + bool ok = qt_normalizePathSegments(&ret, normalization); // Strip away last slash except for root directories if (ret.size() > 1 && ret.endsWith(u'/')) { @@ -2386,7 +2375,7 @@ static QString qt_cleanPath(const QString &path, bool *ok) 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) { - return qt_cleanPath(path); + QString ret = path; + qt_cleanPath(&ret); + return ret; } /*! diff --git a/src/corelib/io/qdir_p.h b/src/corelib/io/qdir_p.h index 908c1e8ed5b..748b4143143 100644 --- a/src/corelib/io/qdir_p.h +++ b/src/corelib/io/qdir_p.h @@ -80,8 +80,7 @@ public: Q_DECLARE_OPERATORS_FOR_FLAGS(QDirPrivate::PathNormalizations) -Q_AUTOTEST_EXPORT QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormalizations flags, bool *ok = nullptr); -bool qt_normalizePathSegments(QString *path, QDirPrivate::PathNormalizations flags); +Q_AUTOTEST_EXPORT bool qt_normalizePathSegments(QString *path, QDirPrivate::PathNormalizations flags); QT_END_NAMESPACE diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp index 8dd5ebca3cd..9598db7f0c5 100644 --- a/tests/auto/corelib/io/qdir/tst_qdir.cpp +++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp @@ -1453,8 +1453,8 @@ void tst_QDir::normalizePathSegments() QFETCH(UncHandling, uncHandling); QFETCH(QString, expected); // for QDirPrivate::RemotePath, see tst_QUrl::resolving - QString cleaned = qt_normalizePathSegments(path, uncHandling == HandleUnc ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization); - QCOMPARE(cleaned, expected); + qt_normalizePathSegments(&path, uncHandling == HandleUnc ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization); + QCOMPARE(path, expected); } # endif //QT_BUILD_INTERNAL