Skip to content

Commit b2511fb

Browse files
authored
Check if --cppcheck-build-dir exists (#5254)
Cppcheck does not report that cppcheck build dir does not exist and also does not report any write issues to the non-existent directory. This means that cppcheck build dir is actually not used. We should either create the directory or fail.
1 parent 5d201c4 commit b2511fb

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

cli/cmdlineparser.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,14 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
285285
}
286286

287287
else if (std::strncmp(argv[i], "--cppcheck-build-dir=", 21) == 0) {
288-
// TODO: bail out when the folder does not exist? will silently do nothing
289288
mSettings.buildDir = Path::fromNativeSeparators(argv[i] + 21);
290289
if (endsWith(mSettings.buildDir, '/'))
291290
mSettings.buildDir.pop_back();
291+
292+
if (!Path::directoryExists(mSettings.buildDir)) {
293+
printError("Directory '" + mSettings.buildDir + "' specified by --cppcheck-build-dir argument has to be existent.");
294+
return false;
295+
}
292296
}
293297

294298
// Show --debug output after the first simplifications

lib/path.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <algorithm>
2727
#include <cstdlib>
2828
#include <fstream>
29+
#include <sys/stat.h>
2930
#include <utility>
3031

3132
#include <simplecpp.h>
@@ -266,6 +267,13 @@ bool Path::fileExists(const std::string &file)
266267
return f.is_open();
267268
}
268269

270+
271+
bool Path::directoryExists(const std::string &path)
272+
{
273+
struct stat info;
274+
return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);
275+
}
276+
269277
std::string Path::join(std::string path1, std::string path2) {
270278
if (path1.empty() || path2.empty())
271279
return path1 + path2;

lib/path.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ class CPPCHECKLIB Path {
187187
*/
188188
static bool fileExists(const std::string &file);
189189

190+
/**
191+
* @brief Checks if a directory exists
192+
* @param path Path to be checked
193+
* @return true if given path is a directory
194+
*/
195+
static bool directoryExists(const std::string &path);
196+
190197
/**
191198
* join 2 paths with '/' separators
192199
*/

test/testcmdlineparser.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ class TestCmdlineParser : public TestFixture {
237237
TEST_CASE(undefs_noarg3);
238238
TEST_CASE(undefs);
239239
TEST_CASE(undefs2);
240+
241+
TEST_CASE(cppcheckBuildDirExistent);
242+
TEST_CASE(cppcheckBuildDirNonExistent);
243+
TEST_CASE(cppcheckBuildDirEmpty);
240244
}
241245

242246

@@ -1947,6 +1951,27 @@ class TestCmdlineParser : public TestFixture {
19471951
ASSERT_EQUALS(false, defParser.parseFromArgs(4, argv));
19481952
ASSERT_EQUALS("cppcheck: error: argument to '-U' is missing.\n", GET_REDIRECT_OUTPUT);
19491953
}
1954+
1955+
void cppcheckBuildDirExistent() {
1956+
REDIRECT;
1957+
const char * const argv[] = {"cppcheck", "--cppcheck-build-dir=."};
1958+
ASSERT_EQUALS(true, defParser.parseFromArgs(2, argv));
1959+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1960+
}
1961+
1962+
void cppcheckBuildDirNonExistent() {
1963+
REDIRECT;
1964+
const char * const argv[] = {"cppcheck", "--cppcheck-build-dir=non-existent-path"};
1965+
ASSERT_EQUALS(false, defParser.parseFromArgs(2, argv));
1966+
ASSERT_EQUALS("cppcheck: error: Directory 'non-existent-path' specified by --cppcheck-build-dir argument has to be existent.\n", GET_REDIRECT_OUTPUT);
1967+
}
1968+
1969+
void cppcheckBuildDirEmpty() {
1970+
REDIRECT;
1971+
const char * const argv[] = {"cppcheck", "--cppcheck-build-dir="};
1972+
ASSERT_EQUALS(false, defParser.parseFromArgs(2, argv));
1973+
ASSERT_EQUALS("cppcheck: error: Directory '' specified by --cppcheck-build-dir argument has to be existent.\n", GET_REDIRECT_OUTPUT);
1974+
}
19501975
};
19511976

19521977
REGISTER_TEST(TestCmdlineParser)

0 commit comments

Comments
 (0)