From 8cc4aaf6eaa05046950c33fca67957e60f98a0f2 Mon Sep 17 00:00:00 2001 From: Malte Langkabel Date: Mon, 27 Jan 2020 21:52:05 +0100 Subject: [PATCH] logic: fix crash when project directory contains non-latin character (issue #899) (#901) * added "isValid()" to FilePath * used "isValid" in check of selected project directory --- src/lib/utility/file/FilePath.cpp | 44 +++++++++++++++++-- src/lib/utility/file/FilePath.h | 1 + .../QtProjectWizardContentProjectData.cpp | 23 +++++++++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/lib/utility/file/FilePath.cpp b/src/lib/utility/file/FilePath.cpp index 0ed39c4ad..5add50d76 100644 --- a/src/lib/utility/file/FilePath.cpp +++ b/src/lib/utility/file/FilePath.cpp @@ -113,6 +113,43 @@ bool FilePath::isAbsolute() const return m_path->is_absolute(); } +bool FilePath::isValid() const +{ + if (!isDirectory()) + { + if (!boost::filesystem::portable_file_name(m_path->filename().generic_string())) + { + return false; + } + } + + boost::filesystem::path::iterator it = m_path->begin(); + +#if WIN32 + if (isAbsolute() && m_path->has_root_path()) + { + std::string root = m_path->root_path().string(); + std::string current = ""; + while (current.size() < root.size()) + { + current += it->string(); + it++; + } + } +#endif + + for (; it != m_path->end(); ++it) + { + std::string ss = it->string(); + if (!boost::filesystem::portable_directory_name(ss)) + { + return false; + } + } + + return true; +} + FilePath FilePath::getParentDirectory() const { FilePath parentDirectory(m_path->parent_path().wstring()); @@ -182,9 +219,10 @@ FilePath& FilePath::makeCanonical() boost::filesystem::path symlink = boost::filesystem::read_symlink(canonicalPath); if (!symlink.empty()) { - // on Windows the read_symlink function discards the drive letter (this is a boost - // bug). Therefore we need to make the path absolute again. We also have to discard - // the trailing \0 characters so that we can continue appending to the path. + // on Windows the read_symlink function discards the drive letter (this is a + // boost bug). Therefore we need to make the path absolute again. We also have + // to discard the trailing \0 characters so that we can continue appending to + // the path. canonicalPath = utility::substrBeforeFirst( boost::filesystem::absolute(symlink).string(), '\0'); } diff --git a/src/lib/utility/file/FilePath.h b/src/lib/utility/file/FilePath.h index 244fb1e6f..8e1a2bdf9 100644 --- a/src/lib/utility/file/FilePath.h +++ b/src/lib/utility/file/FilePath.h @@ -31,6 +31,7 @@ class FilePath bool recheckExists() const; bool isDirectory() const; bool isAbsolute() const; + bool isValid() const; FilePath getParentDirectory() const; diff --git a/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentProjectData.cpp b/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentProjectData.cpp index c541172de..fa6e8fd42 100644 --- a/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentProjectData.cpp +++ b/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentProjectData.cpp @@ -109,11 +109,30 @@ bool QtProjectWizardContentProjectData::check() std::vector paths = FilePath(m_projectFileLocation->getText().toStdWString()).expandEnvironmentVariables(); - if (paths.size() != 1 || !paths[0].isAbsolute()) + if (paths.size() != 1) { QMessageBox msgBox; msgBox.setText( - "The specified location is invalid. Please enter an absolute directory path."); + "The specified location seems to be invalid. Please make sure that the used " + "environment variables are unambiguous."); + msgBox.exec(); + return false; + } + else if (!paths.front().isAbsolute()) + { + QMessageBox msgBox; + msgBox.setText( + "The specified location seems to be invalid. Please specify an absolute directory " + "path."); + msgBox.exec(); + return false; + } + else if (!paths.front().isValid()) + { + QMessageBox msgBox; + msgBox.setText( + "The specified location seems to be invalid. Please check the characters used in the " + "path."); msgBox.exec(); return false; }