From 361ae311f6ec4b6f7be7c70c9d9aa7eb5344fd22 Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Tue, 4 Sep 2018 16:37:43 +0200 Subject: [PATCH] cmdlineparser: Stop processing in case an invalid path for --includes-file= and --config-excludes-file= is provided. --- cli/cmdlineparser.cpp | 23 +++++++++++++++++------ test/testcmdlineparser.cpp | 13 ++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index c0150216e..7f7363701 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -65,9 +65,8 @@ static void addFilesToList(const std::string& FileList, std::vector } } -static void addIncludePathsToList(const std::string& FileList, std::list* PathNames) +static bool addIncludePathsToList(const std::string& FileList, std::list* PathNames) { - // To keep things initially simple, if the file can't be opened, just be silent and move on. std::ifstream Files(FileList); if (Files) { std::string PathName; @@ -83,14 +82,18 @@ static void addIncludePathsToList(const std::string& FileList, std::listpush_back(PathName); } } + return true; } + return false; } -static void addPathsToSet(const std::string& FileName, std::set* set) +static bool addPathsToSet(const std::string& FileName, std::set* set) { std::list templist; - addIncludePathsToList(FileName, &templist); + if (!addIncludePathsToList(FileName, &templist)) + return false; set->insert(templist.begin(), templist.end()); + return true; } CmdLineParser::CmdLineParser(Settings *settings) @@ -446,14 +449,22 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) mSettings->userIncludes.push_back(path); } else if (std::strncmp(argv[i], "--includes-file=", 16) == 0) { // open this file and read every input file (1 file name per line) - addIncludePathsToList(16 + argv[i], &mSettings->includePaths); + const std::string includesFile(16 + argv[i]); + if (!addIncludePathsToList(includesFile, &mSettings->includePaths)) { + printMessage("Cppcheck: unable to open includes file at '" + includesFile + "'"); + return false; + } } else if (std::strncmp(argv[i], "--config-exclude=",17) ==0) { std::string path = argv[i] + 17; path = Path::fromNativeSeparators(path); mSettings->configExcludePaths.insert(path); } else if (std::strncmp(argv[i], "--config-excludes-file=", 23) == 0) { // open this file and read every input file (1 file name per line) - addPathsToSet(23 + argv[i], &mSettings->configExcludePaths); + const std::string cfgExcludesFile(23 + argv[i]); + if (!addPathsToSet(cfgExcludesFile, &mSettings->configExcludePaths)) { + printMessage("Cppcheck: unable to open config excludes file at '" + cfgExcludesFile + "'"); + return false; + } } // file list specified diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index c84921307..3c9b0fa80 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -74,6 +74,7 @@ private: TEST_CASE(includesnospace); TEST_CASE(includes2); TEST_CASE(includesFile); + TEST_CASE(configExcludesFile); TEST_CASE(enabledAll); TEST_CASE(enabledStyle); TEST_CASE(enabledPerformance); @@ -450,11 +451,17 @@ private: } void includesFile() { - // TODO: Fails since cannot open the file REDIRECT; - const char *argv[] = {"cppcheck", "--includes-file=inclpaths.txt", "file.cpp"}; + const char * const argv[] = {"cppcheck", "--includes-file=fileThatDoesNotExist.txt", "file.cpp"}; settings.includePaths.clear(); - ASSERT_EQUALS(true, defParser.parseFromArgs(3, argv)); + ASSERT_EQUALS(false, defParser.parseFromArgs(3, argv)); + } + + void configExcludesFile() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--config-excludes-file=fileThatDoesNotExist.txt", "file.cpp"}; + settings.includePaths.clear(); + ASSERT_EQUALS(false, defParser.parseFromArgs(3, argv)); } void enabledAll() {