From a5df9eea5a99fb154b27e4b919829c6a5c4edd1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Thu, 20 Jul 2023 14:40:01 +0000 Subject: [PATCH 1/5] check if cppcheck build dir exists --- cli/cmdlineparser.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index d76edbf9114..39b913098da 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -45,6 +45,7 @@ #include #include // IWYU pragma: keep #include +#include #include #include @@ -290,10 +291,15 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) } else if (std::strncmp(argv[i], "--cppcheck-build-dir=", 21) == 0) { - // TODO: bail out when the folder does not exist? will silently do nothing mSettings.buildDir = Path::fromNativeSeparators(argv[i] + 21); if (endsWith(mSettings.buildDir, '/')) mSettings.buildDir.pop_back(); + + struct stat info; + if (stat(mSettings.buildDir.c_str(), &info) != 0 || !(info.st_mode & S_IFDIR)) { + printError("Directory '" + mSettings.buildDir + "' specified by --cppcheck-build-dir argument has to be existent."); + return false; + } } // Show --debug output after the first simplifications From 50d1c87892336f12c767cb43fc71722f787b2a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Fri, 28 Jul 2023 09:46:34 +0000 Subject: [PATCH 2/5] refactor: Path::directoryExists --- cli/cmdlineparser.cpp | 4 +--- lib/path.cpp | 8 ++++++++ lib/path.h | 7 +++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 39b913098da..2c0dbfd387c 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -45,7 +45,6 @@ #include #include // IWYU pragma: keep #include -#include #include #include @@ -295,8 +294,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) if (endsWith(mSettings.buildDir, '/')) mSettings.buildDir.pop_back(); - struct stat info; - if (stat(mSettings.buildDir.c_str(), &info) != 0 || !(info.st_mode & S_IFDIR)) { + if (!Path::directoryExists(mSettings.buildDir)) { printError("Directory '" + mSettings.buildDir + "' specified by --cppcheck-build-dir argument has to be existent."); return false; } diff --git a/lib/path.cpp b/lib/path.cpp index 442d88f441f..2b6540b73d1 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -266,6 +267,13 @@ bool Path::fileExists(const std::string &file) return f.is_open(); } + +bool Path::directoryExists(const std::string &path) +{ + struct stat info; + return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR); +} + std::string Path::join(std::string path1, std::string path2) { if (path1.empty() || path2.empty()) return path1 + path2; diff --git a/lib/path.h b/lib/path.h index 621360efeef..8ab8c79825a 100644 --- a/lib/path.h +++ b/lib/path.h @@ -187,6 +187,13 @@ class CPPCHECKLIB Path { */ static bool fileExists(const std::string &file); + /** + * @brief Checks if a directory exists + * @param path Path to be checked + * @return true if given path is a directory + */ + static bool directoryExists(const std::string &path); + /** * join 2 paths with '/' separators */ From 492791c0deac5d7d2fa14e876ce9fe62ced485bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Fri, 28 Jul 2023 11:04:11 +0000 Subject: [PATCH 3/5] Add UT --- test/testcmdlineparser.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index fffa19f5ff7..455f1e50c6c 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -237,6 +237,10 @@ class TestCmdlineParser : public TestFixture { TEST_CASE(undefs_noarg3); TEST_CASE(undefs); TEST_CASE(undefs2); + + TEST_CASE(cppcheckBuildDirExistent); + TEST_CASE(cppcheckBuildDirNonExistent); + TEST_CASE(cppcheckBuildDirEmpty); } @@ -1947,6 +1951,27 @@ class TestCmdlineParser : public TestFixture { ASSERT_EQUALS(false, defParser.parseFromArgs(4, argv)); ASSERT_EQUALS("cppcheck: error: argument to '-U' is missing.\n", GET_REDIRECT_OUTPUT); } + + void cppcheckBuildDirExistent() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--cppcheck-build-dir=."}; + ASSERT_EQUALS(true, defParser.parseFromArgs(2, argv)); + ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); + } + + void cppcheckBuildDirNonExistent() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--cppcheck-build-dir=non-existent-path"}; + ASSERT_EQUALS(false, defParser.parseFromArgs(2, argv)); + ASSERT_EQUALS("cppcheck: error: Directory 'non-existent-path' specified by --cppcheck-build-dir argument has to be existent.\n", GET_REDIRECT_OUTPUT); + } + + void cppcheckBuildDirEmpty() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--cppcheck-build-dir="}; + ASSERT_EQUALS(false, defParser.parseFromArgs(2, argv)); + ASSERT_EQUALS("cppcheck: error: Directory '' specified by --cppcheck-build-dir argument has to be existent.\n", GET_REDIRECT_OUTPUT); + } }; REGISTER_TEST(TestCmdlineParser) From 8996df34ebba3c8bb4fd25da05188bcb22ddd84c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Mon, 31 Jul 2023 09:33:08 +0000 Subject: [PATCH 4/5] Retrigger CI From 3881eada68cad1ca28502fb09df2b453daf4718f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pol=C3=A1=C4=8Dek?= Date: Fri, 4 Aug 2023 13:08:38 +0000 Subject: [PATCH 5/5] Retrigger CI