cmdlineparser: Stop processing in case an invalid path for --includes-file=<PATH> and --config-excludes-file=<PATH> is provided.

This commit is contained in:
orbitcowboy 2018-09-04 16:37:43 +02:00
parent 5afb755a3c
commit 361ae311f6
2 changed files with 27 additions and 9 deletions

View File

@ -65,9 +65,8 @@ static void addFilesToList(const std::string& FileList, std::vector<std::string>
} }
} }
static void addIncludePathsToList(const std::string& FileList, std::list<std::string>* PathNames) static bool addIncludePathsToList(const std::string& FileList, std::list<std::string>* PathNames)
{ {
// To keep things initially simple, if the file can't be opened, just be silent and move on.
std::ifstream Files(FileList); std::ifstream Files(FileList);
if (Files) { if (Files) {
std::string PathName; std::string PathName;
@ -83,14 +82,18 @@ static void addIncludePathsToList(const std::string& FileList, std::list<std::st
PathNames->push_back(PathName); PathNames->push_back(PathName);
} }
} }
return true;
} }
return false;
} }
static void addPathsToSet(const std::string& FileName, std::set<std::string>* set) static bool addPathsToSet(const std::string& FileName, std::set<std::string>* set)
{ {
std::list<std::string> templist; std::list<std::string> templist;
addIncludePathsToList(FileName, &templist); if (!addIncludePathsToList(FileName, &templist))
return false;
set->insert(templist.begin(), templist.end()); set->insert(templist.begin(), templist.end());
return true;
} }
CmdLineParser::CmdLineParser(Settings *settings) CmdLineParser::CmdLineParser(Settings *settings)
@ -446,14 +449,22 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings->userIncludes.push_back(path); mSettings->userIncludes.push_back(path);
} else if (std::strncmp(argv[i], "--includes-file=", 16) == 0) { } else if (std::strncmp(argv[i], "--includes-file=", 16) == 0) {
// open this file and read every input file (1 file name per line) // 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) { } else if (std::strncmp(argv[i], "--config-exclude=",17) ==0) {
std::string path = argv[i] + 17; std::string path = argv[i] + 17;
path = Path::fromNativeSeparators(path); path = Path::fromNativeSeparators(path);
mSettings->configExcludePaths.insert(path); mSettings->configExcludePaths.insert(path);
} else if (std::strncmp(argv[i], "--config-excludes-file=", 23) == 0) { } else if (std::strncmp(argv[i], "--config-excludes-file=", 23) == 0) {
// open this file and read every input file (1 file name per line) // 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 // file list specified

View File

@ -74,6 +74,7 @@ private:
TEST_CASE(includesnospace); TEST_CASE(includesnospace);
TEST_CASE(includes2); TEST_CASE(includes2);
TEST_CASE(includesFile); TEST_CASE(includesFile);
TEST_CASE(configExcludesFile);
TEST_CASE(enabledAll); TEST_CASE(enabledAll);
TEST_CASE(enabledStyle); TEST_CASE(enabledStyle);
TEST_CASE(enabledPerformance); TEST_CASE(enabledPerformance);
@ -450,11 +451,17 @@ private:
} }
void includesFile() { void includesFile() {
// TODO: Fails since cannot open the file
REDIRECT; 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(); 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() { void enabledAll() {