From 5d1ffcd5f726f0e6aec85ebe89782e3b43ad94e0 Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 15 Feb 2024 20:41:57 +0100 Subject: [PATCH] do not treat directories like regular files in existence checks --- main.cpp | 4 ++++ simplecpp.cpp | 29 ++++++++++++++++------------- test.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index 3f02773..8173e99 100644 --- a/main.cpp +++ b/main.cpp @@ -121,6 +121,10 @@ int main(int argc, char **argv) std::cout << "error: could not open file '" << filename << "'" << std::endl; std::exit(1); } + if (!simplecpp::isFile(filename)) { + std::cout << "error: could not open file '" << filename << "' - not a regular file" << std::endl; + std::exit(1); + } rawtokens = new simplecpp::TokenList(f, files,filename,&outputList); } else { diff --git a/simplecpp.cpp b/simplecpp.cpp index bc62c03..66859d3 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -3084,9 +3084,11 @@ static std::string openHeader(std::ifstream &f, const std::string &path) if (nonExistingFilesCache.contains(simplePath)) return ""; // file is known not to exist, skip expensive file open call #endif - f.open(simplePath.c_str()); - if (f.is_open()) - return simplePath; + if (simplecpp::isFile(simplePath)) { + f.open(simplePath.c_str()); + if (f.is_open()) + return simplePath; + } #ifdef SIMPLECPP_WINDOWS nonExistingFilesCache.add(simplePath); #endif @@ -3191,18 +3193,19 @@ std::map simplecpp::load(const simplecpp::To if (ret.find(filename) != ret.end()) continue; - std::ifstream fin(filename.c_str()); - if (!fin.is_open()) { - if (outputList) { - simplecpp::Output err(filenames); - err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND; - err.location = Location(filenames); - err.msg = "Can not open include file '" + filename + "' that is explicitly included."; - outputList->push_back(err); + { + std::ifstream fin(filename.c_str()); + if (!fin.is_open() || !isFile(filename)) { + if (outputList) { + simplecpp::Output err(filenames); + err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND; + err.location = Location(filenames); + err.msg = "Can not open include file '" + filename + "' that is explicitly included."; + outputList->push_back(err); + } + continue; } - continue; } - fin.close(); TokenList *tokenlist = new TokenList(filename, filenames, outputList); if (!tokenlist->front()) { diff --git a/test.cpp b/test.cpp index d00658a..05f1668 100644 --- a/test.cpp +++ b/test.cpp @@ -1779,6 +1779,44 @@ static void missingHeader3() ASSERT_EQUALS("", toString(outputList)); } +#ifndef _WIN32 +static void missingHeader4() +{ + // this is a directory + const char code[] = "#include \"/\"\n"; + simplecpp::OutputList outputList; + ASSERT_EQUALS("", preprocess(code, &outputList)); + ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/\"\n", toString(outputList)); +} + +static void missingHeader5() +{ + // this is a directory + const char code[] = "#include \"/usr\"\n"; + simplecpp::OutputList outputList; + ASSERT_EQUALS("", preprocess(code, &outputList)); + ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/usr\"\n", toString(outputList)); +} + +static void missingHeader6() +{ + // this is a directory + const char code[] = "#include \n"; + simplecpp::OutputList outputList; + ASSERT_EQUALS("", preprocess(code, &outputList)); + ASSERT_EQUALS("file0,1,missing_header,Header not found: \n", toString(outputList)); +} + +static void missingHeader7() +{ + // this is a directory + const char code[] = "#include \n"; + simplecpp::OutputList outputList; + ASSERT_EQUALS("", preprocess(code, &outputList)); + ASSERT_EQUALS("file0,1,missing_header,Header not found: \n", toString(outputList)); +} +#endif + static void nestedInclude() { const char code[] = "#include \"test.h\"\n"; @@ -2959,6 +2997,12 @@ int main(int argc, char **argv) TEST_CASE(missingHeader1); TEST_CASE(missingHeader2); TEST_CASE(missingHeader3); +#ifndef _WIN32 + TEST_CASE(missingHeader4); + TEST_CASE(missingHeader5); + TEST_CASE(missingHeader6); + TEST_CASE(missingHeader7); +#endif TEST_CASE(nestedInclude); TEST_CASE(systemInclude);