Skip to content

Commit aabaaab

Browse files
committed
do not treat directories like regular files in existence checks
1 parent 0174019 commit aabaaab

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ int main(int argc, char **argv)
121121
std::cout << "error: could not open file '" << filename << "'" << std::endl;
122122
std::exit(1);
123123
}
124+
if (!simplecpp::isFile(filename)) {
125+
std::cout << "error: could not open file '" << filename << "' - not a regular file" << std::endl;
126+
std::exit(1);
127+
}
124128
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
125129
} else {
126130
rawtokens = new simplecpp::TokenList(filename,files,&outputList);

simplecpp.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,9 +2981,11 @@ static std::string openHeaderDirect(std::ifstream &f, const std::string &path)
29812981
if (nonExistingFilesCache.contains(path))
29822982
return ""; // file is known not to exist, skip expensive file open call
29832983
#endif
2984-
f.open(path.c_str());
2985-
if (f.is_open())
2986-
return path;
2984+
if (simplecpp::isFile(path)) {
2985+
f.open(path.c_str());
2986+
if (f.is_open())
2987+
return path;
2988+
}
29872989
#ifdef SIMPLECPP_WINDOWS
29882990
nonExistingFilesCache.add(path);
29892991
#endif
@@ -3104,6 +3106,9 @@ bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id)
31043106
if (stat(path.c_str(), &statbuf) != 0)
31053107
return false;
31063108

3109+
if ((statbuf.st_mode & S_IFMT) != S_IFREG)
3110+
return false;
3111+
31073112
id.dev = statbuf.st_dev;
31083113
id.ino = statbuf.st_ino;
31093114

test.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,44 @@ static void missingHeader4()
20522052
ASSERT_EQUALS("file0,1,syntax_error,No header in #include\n", toString(outputList));
20532053
}
20542054

2055+
#ifndef _WIN32
2056+
static void missingHeader5()
2057+
{
2058+
// this is a directory
2059+
const char code[] = "#include \"/\"\n";
2060+
simplecpp::OutputList outputList;
2061+
ASSERT_EQUALS("", preprocess(code, &outputList));
2062+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/\"\n", toString(outputList));
2063+
}
2064+
2065+
static void missingHeader6()
2066+
{
2067+
// this is a directory
2068+
const char code[] = "#include \"/usr\"\n";
2069+
simplecpp::OutputList outputList;
2070+
ASSERT_EQUALS("", preprocess(code, &outputList));
2071+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/usr\"\n", toString(outputList));
2072+
}
2073+
2074+
static void missingHeader7()
2075+
{
2076+
// this is a directory
2077+
const char code[] = "#include </>\n";
2078+
simplecpp::OutputList outputList;
2079+
ASSERT_EQUALS("", preprocess(code, &outputList));
2080+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </>\n", toString(outputList));
2081+
}
2082+
2083+
static void missingHeader8()
2084+
{
2085+
// this is a directory
2086+
const char code[] = "#include </usr>\n";
2087+
simplecpp::OutputList outputList;
2088+
ASSERT_EQUALS("", preprocess(code, &outputList));
2089+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </usr>\n", toString(outputList));
2090+
}
2091+
#endif
2092+
20552093
static void nestedInclude()
20562094
{
20572095
const char code[] = "#include \"test.h\"\n";
@@ -3312,6 +3350,12 @@ int main(int argc, char **argv)
33123350
TEST_CASE(missingHeader2);
33133351
TEST_CASE(missingHeader3);
33143352
TEST_CASE(missingHeader4);
3353+
#ifndef _WIN32
3354+
TEST_CASE(missingHeader5);
3355+
TEST_CASE(missingHeader6);
3356+
TEST_CASE(missingHeader7);
3357+
TEST_CASE(missingHeader8);
3358+
#endif
33153359
TEST_CASE(nestedInclude);
33163360
TEST_CASE(systemInclude);
33173361

0 commit comments

Comments
 (0)