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() {