From f6a19f868a12884bffb66018d056cb21d51aa4c9 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Mon, 3 Oct 2016 22:22:25 +0300 Subject: [PATCH] TRAC-6098: Rework trailing slash stripping (once more) Using QFileInfo to strip trailing slash(es) is bad when input contains non- native paths (i.e. Windows paths on non-Windows system and vice versa) as it may mistakenly treat the path as relative and change it in unespected way. --- qt/Utils.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/qt/Utils.cc b/qt/Utils.cc index b4607e211..6f1cd437f 100644 --- a/qt/Utils.cc +++ b/qt/Utils.cc @@ -45,9 +45,9 @@ extern QPixmap qt_pixmapFromWinHICON(HICON icon); #endif -#ifdef _WIN32 namespace { +#ifdef _WIN32 void addAssociatedFileIcon (const QFileInfo& fileInfo, UINT iconSize, QIcon& icon) { @@ -83,9 +83,15 @@ namespace if (!pixmap.isNull ()) icon.addPixmap (pixmap); } -} // namespace #endif + bool + isSlashChar (const QChar& c) + { + return c == QLatin1Char ('/') || c == QLatin1Char ('\\'); + } +} // namespace + QIcon Utils::guessMimeIcon (const QString& filename) { @@ -201,8 +207,10 @@ Utils::isValidUtf8 (const char * s) QString Utils::removeTrailingDirSeparator (const QString& path) { - const QFileInfo pathInfo (path); - return pathInfo.fileName ().isEmpty () ? pathInfo.absolutePath () : pathInfo.absoluteFilePath (); + int i = path.size (); + while (i > 1 && isSlashChar (path[i - 1])) + --i; + return path.left (i); } int