Check stat return value in addFiles2 (#3214)
This commit is contained in:
parent
5de58c4ddd
commit
5768c3a726
|
@ -177,8 +177,12 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
|
||||||
} else if (!pathnames.empty()) {
|
} else if (!pathnames.empty()) {
|
||||||
// Execute recursiveAddFiles() to each given file parameter
|
// Execute recursiveAddFiles() to each given file parameter
|
||||||
const PathMatch matcher(ignored, caseSensitive);
|
const PathMatch matcher(ignored, caseSensitive);
|
||||||
for (const std::string &pathname : pathnames)
|
for (const std::string &pathname : pathnames) {
|
||||||
FileLister::recursiveAddFiles(mFiles, Path::toNativeSeparators(pathname), mSettings->library.markupExtensions(), matcher);
|
std::string err = FileLister::recursiveAddFiles(mFiles, Path::toNativeSeparators(pathname), mSettings->library.markupExtensions(), matcher);
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cout << "cppcheck: " << err << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mFiles.empty() && settings.project.fileSettings.empty()) {
|
if (mFiles.empty() && settings.project.fileSettings.empty()) {
|
||||||
|
|
|
@ -71,12 +71,12 @@ static BOOL myFileExists(const std::string& path)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
|
std::string FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
|
||||||
{
|
{
|
||||||
addFiles(files, path, extra, true, ignored);
|
return addFiles(files, path, extra, true, ignored);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
|
std::string FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
|
||||||
{
|
{
|
||||||
const std::string cleanedPath = Path::toNativeSeparators(path);
|
const std::string cleanedPath = Path::toNativeSeparators(path);
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ void FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::
|
||||||
WIN32_FIND_DATAA ffd;
|
WIN32_FIND_DATAA ffd;
|
||||||
HANDLE hFind = myFindFirstFile(searchPattern, &ffd);
|
HANDLE hFind = myFindFirstFile(searchPattern, &ffd);
|
||||||
if (INVALID_HANDLE_VALUE == hFind)
|
if (INVALID_HANDLE_VALUE == hFind)
|
||||||
return;
|
return "";
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0')
|
if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0')
|
||||||
|
@ -143,13 +143,17 @@ void FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::
|
||||||
} else {
|
} else {
|
||||||
// Directory
|
// Directory
|
||||||
if (recursive) {
|
if (recursive) {
|
||||||
if (!ignored.match(fname))
|
if (!ignored.match(fname)) {
|
||||||
FileLister::recursiveAddFiles(files, fname, extra, ignored);
|
std::string err = FileLister::recursiveAddFiles(files, fname, extra, ignored);
|
||||||
|
if (!err.empty())
|
||||||
|
return err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (FindNextFileA(hFind, &ffd) != FALSE);
|
} while (FindNextFileA(hFind, &ffd) != FALSE);
|
||||||
|
|
||||||
FindClose(hFind);
|
FindClose(hFind);
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileLister::isDirectory(const std::string &path)
|
bool FileLister::isDirectory(const std::string &path)
|
||||||
|
@ -175,21 +179,22 @@ bool FileLister::fileExists(const std::string &path)
|
||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/errno.h>
|
||||||
|
|
||||||
|
|
||||||
static void addFiles2(std::map<std::string, std::size_t> &files,
|
static std::string addFiles2(std::map<std::string, std::size_t> &files,
|
||||||
const std::string &path,
|
const std::string &path,
|
||||||
const std::set<std::string> &extra,
|
const std::set<std::string> &extra,
|
||||||
bool recursive,
|
bool recursive,
|
||||||
const PathMatch& ignored
|
const PathMatch& ignored
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
if (stat(path.c_str(), &file_stat) != -1) {
|
if (stat(path.c_str(), &file_stat) != -1) {
|
||||||
if ((file_stat.st_mode & S_IFMT) == S_IFDIR) {
|
if ((file_stat.st_mode & S_IFMT) == S_IFDIR) {
|
||||||
DIR * dir = opendir(path.c_str());
|
DIR * dir = opendir(path.c_str());
|
||||||
if (!dir)
|
if (!dir)
|
||||||
return;
|
return "";
|
||||||
|
|
||||||
dirent * dir_result;
|
dirent * dir_result;
|
||||||
// make sure we reserve enough space for the readdir_r() buffer;
|
// make sure we reserve enough space for the readdir_r() buffer;
|
||||||
|
@ -225,12 +230,16 @@ static void addFiles2(std::map<std::string, std::size_t> &files,
|
||||||
#endif
|
#endif
|
||||||
if (path_is_directory) {
|
if (path_is_directory) {
|
||||||
if (recursive && !ignored.match(new_path)) {
|
if (recursive && !ignored.match(new_path)) {
|
||||||
addFiles2(files, new_path, extra, recursive, ignored);
|
std::string err = addFiles2(files, new_path, extra, recursive, ignored);
|
||||||
|
if (!err.empty())
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
|
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
|
||||||
stat(new_path.c_str(), &file_stat);
|
if (stat(new_path.c_str(), &file_stat) != -1)
|
||||||
files[new_path] = file_stat.st_size;
|
files[new_path] = file_stat.st_size;
|
||||||
|
else
|
||||||
|
return "Can't stat " + new_path + " errno: " + std::to_string(errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -238,22 +247,25 @@ static void addFiles2(std::map<std::string, std::size_t> &files,
|
||||||
} else
|
} else
|
||||||
files[path] = file_stat.st_size;
|
files[path] = file_stat.st_size;
|
||||||
}
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
|
std::string FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
|
||||||
{
|
{
|
||||||
addFiles(files, path, extra, true, ignored);
|
return addFiles(files, path, extra, true, ignored);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
|
std::string FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
|
||||||
{
|
{
|
||||||
if (!path.empty()) {
|
if (!path.empty()) {
|
||||||
std::string corrected_path = path;
|
std::string corrected_path = path;
|
||||||
if (endsWith(corrected_path, '/'))
|
if (endsWith(corrected_path, '/'))
|
||||||
corrected_path.erase(corrected_path.end() - 1);
|
corrected_path.erase(corrected_path.end() - 1);
|
||||||
|
|
||||||
addFiles2(files, corrected_path, extra, recursive, ignored);
|
return addFiles2(files, corrected_path, extra, recursive, ignored);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileLister::isDirectory(const std::string &path)
|
bool FileLister::isDirectory(const std::string &path)
|
||||||
|
|
|
@ -40,10 +40,11 @@ public:
|
||||||
* @param files output map that associates the size of each file with its name
|
* @param files output map that associates the size of each file with its name
|
||||||
* @param path root path
|
* @param path root path
|
||||||
* @param ignored ignored paths
|
* @param ignored ignored paths
|
||||||
|
* @return On success, an empty string is returned. On error, a error message is returned.
|
||||||
*/
|
*/
|
||||||
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const PathMatch& ignored) {
|
static std::string recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const PathMatch& ignored) {
|
||||||
const std::set<std::string> extra;
|
const std::set<std::string> extra;
|
||||||
recursiveAddFiles(files, path, extra, ignored);
|
return recursiveAddFiles(files, path, extra, ignored);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,8 +56,9 @@ public:
|
||||||
* @param path root path
|
* @param path root path
|
||||||
* @param extra Extra file extensions
|
* @param extra Extra file extensions
|
||||||
* @param ignored ignored paths
|
* @param ignored ignored paths
|
||||||
|
* @return On success, an empty string is returned. On error, a error message is returned.
|
||||||
*/
|
*/
|
||||||
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored);
|
static std::string recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief (Recursively) add source files to a map.
|
* @brief (Recursively) add source files to a map.
|
||||||
|
@ -68,8 +70,9 @@ public:
|
||||||
* @param extra Extra file extensions
|
* @param extra Extra file extensions
|
||||||
* @param recursive Enable recursion
|
* @param recursive Enable recursion
|
||||||
* @param ignored ignored paths
|
* @param ignored ignored paths
|
||||||
|
* @return On success, an empty string is returned. On error, a error message is returned.
|
||||||
*/
|
*/
|
||||||
static void addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored);
|
static std::string addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Is given path a directory?
|
* @brief Is given path a directory?
|
||||||
|
|
|
@ -55,7 +55,8 @@ private:
|
||||||
std::map<std::string, std::size_t> files;
|
std::map<std::string, std::size_t> files;
|
||||||
std::vector<std::string> masks;
|
std::vector<std::string> masks;
|
||||||
PathMatch matcher(masks);
|
PathMatch matcher(masks);
|
||||||
FileLister::recursiveAddFiles(files, ".", matcher);
|
std::string err = FileLister::recursiveAddFiles(files, ".", matcher);
|
||||||
|
ASSERT(err.empty());
|
||||||
|
|
||||||
// In case there are leading "./"..
|
// In case there are leading "./"..
|
||||||
for (std::map<std::string, std::size_t>::iterator i = files.begin(); i != files.end();) {
|
for (std::map<std::string, std::size_t>::iterator i = files.begin(); i != files.end();) {
|
||||||
|
|
|
@ -53,11 +53,13 @@ private:
|
||||||
std::map<std::string, std::size_t> files;
|
std::map<std::string, std::size_t> files;
|
||||||
const std::vector<std::string> masks;
|
const std::vector<std::string> masks;
|
||||||
const PathMatch matcher(masks);
|
const PathMatch matcher(masks);
|
||||||
|
std::string err =
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
FileLister::recursiveAddFiles(files, "..\\samples", matcher);
|
FileLister::recursiveAddFiles(files, "..\\samples", matcher);
|
||||||
#else
|
#else
|
||||||
FileLister::recursiveAddFiles(files, "samples", matcher);
|
FileLister::recursiveAddFiles(files, "samples", matcher);
|
||||||
#endif
|
#endif
|
||||||
|
ASSERT(err.empty());
|
||||||
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) {
|
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) {
|
||||||
if (i->first.find("memleak") != std::string::npos)
|
if (i->first.find("memleak") != std::string::npos)
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -58,11 +58,7 @@ static void getDeps(const std::string &filename, std::vector<std::string> &depfi
|
||||||
if (filename == "externals/z3_version.h")
|
if (filename == "externals/z3_version.h")
|
||||||
return;
|
return;
|
||||||
|
|
||||||
static const std::vector<std::string> externalfolders{"externals",
|
static const std::vector<std::string> externalfolders{"externals", "externals/picojson", "externals/simplecpp", "externals/tinyxml2" };
|
||||||
"externals/picojson",
|
|
||||||
"externals/simplecpp",
|
|
||||||
"externals/tinyxml2"
|
|
||||||
};
|
|
||||||
|
|
||||||
// Is the dependency already included?
|
// Is the dependency already included?
|
||||||
if (std::find(depfiles.begin(), depfiles.end(), filename) != depfiles.end())
|
if (std::find(depfiles.begin(), depfiles.end(), filename) != depfiles.end())
|
||||||
|
@ -130,19 +126,22 @@ static void compilefiles(std::ostream &fout, const std::vector<std::string> &fil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void getCppFiles(std::vector<std::string> &files, const std::string &path, bool recursive)
|
static std::string getCppFiles(std::vector<std::string> &files, const std::string &path, bool recursive)
|
||||||
{
|
{
|
||||||
std::map<std::string,size_t> filemap;
|
std::map<std::string,size_t> filemap;
|
||||||
const std::set<std::string> extra;
|
const std::set<std::string> extra;
|
||||||
const std::vector<std::string> masks;
|
const std::vector<std::string> masks;
|
||||||
const PathMatch matcher(masks);
|
const PathMatch matcher(masks);
|
||||||
FileLister::addFiles(filemap, path, extra, recursive, matcher);
|
std::string err = FileLister::addFiles(filemap, path, extra, recursive, matcher);
|
||||||
|
if (!err.empty())
|
||||||
|
return err;
|
||||||
|
|
||||||
// add *.cpp files to the "files" vector..
|
// add *.cpp files to the "files" vector..
|
||||||
for (const std::pair<const std::string&, size_t> file : filemap) {
|
for (const std::pair<const std::string&, size_t> file : filemap) {
|
||||||
if (file.first.find(".cpp") != std::string::npos)
|
if (file.first.find(".cpp") != std::string::npos)
|
||||||
files.push_back(file.first);
|
files.push_back(file.first);
|
||||||
}
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -160,19 +159,39 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
// Get files..
|
// Get files..
|
||||||
std::vector<std::string> libfiles;
|
std::vector<std::string> libfiles;
|
||||||
getCppFiles(libfiles, "lib/", false);
|
std::string err = getCppFiles(libfiles, "lib/", false);
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << err << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> extfiles;
|
std::vector<std::string> extfiles;
|
||||||
getCppFiles(extfiles, "externals/", true);
|
err = getCppFiles(extfiles, "externals/", true);
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << err << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> clifiles;
|
std::vector<std::string> clifiles;
|
||||||
getCppFiles(clifiles, "cli/", false);
|
err = getCppFiles(clifiles, "cli/", false);
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << err << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> testfiles;
|
std::vector<std::string> testfiles;
|
||||||
getCppFiles(testfiles, "test/", false);
|
err = getCppFiles(testfiles, "test/", false);
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << err << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> toolsfiles;
|
std::vector<std::string> toolsfiles;
|
||||||
getCppFiles(toolsfiles, "tools/", false);
|
err = getCppFiles(toolsfiles, "tools/", false);
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << err << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if (libfiles.empty() && clifiles.empty() && testfiles.empty()) {
|
if (libfiles.empty() && clifiles.empty() && testfiles.empty()) {
|
||||||
std::cerr << "No files found. Are you in the correct directory?" << std::endl;
|
std::cerr << "No files found. Are you in the correct directory?" << std::endl;
|
||||||
|
|
Loading…
Reference in New Issue