Skip to content

Commit 27b4508

Browse files
committed
do not treat directories like regular files in existence checks
1 parent 7232114 commit 27b4508

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

main.cpp

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

simplecpp.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,9 +3221,11 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
32213221
if (nonExistingFilesCache.contains(simplePath))
32223222
return ""; // file is known not to exist, skip expensive file open call
32233223
#endif
3224-
f.open(simplePath.c_str());
3225-
if (f.is_open())
3226-
return simplePath;
3224+
if (simplecpp::isFile(simplePath)) {
3225+
f.open(simplePath.c_str());
3226+
if (f.is_open())
3227+
return simplePath;
3228+
}
32273229
#ifdef SIMPLECPP_WINDOWS
32283230
nonExistingFilesCache.add(simplePath);
32293231
#endif
@@ -3389,18 +3391,19 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
33893391
if (ret.find(filename) != ret.end())
33903392
continue;
33913393

3392-
std::ifstream fin(filename.c_str());
3393-
if (!fin.is_open()) {
3394-
if (outputList) {
3395-
simplecpp::Output err(filenames);
3396-
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3397-
err.location = Location(filenames);
3398-
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3399-
outputList->push_back(err);
3394+
{
3395+
std::ifstream fin(filename.c_str());
3396+
if (!fin.is_open() || !isFile(filename)) {
3397+
if (outputList) {
3398+
simplecpp::Output err(filenames);
3399+
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3400+
err.location = Location(filenames);
3401+
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3402+
outputList->push_back(err);
3403+
}
3404+
continue;
34003405
}
3401-
continue;
34023406
}
3403-
fin.close();
34043407

34053408
TokenList *tokenlist = new TokenList(filename, filenames, outputList);
34063409
if (!tokenlist->front()) {

test.cpp

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

1963+
#ifndef _WIN32
1964+
static void missingHeader5()
1965+
{
1966+
// this is a directory
1967+
const char code[] = "#include \"/\"\n";
1968+
simplecpp::OutputList outputList;
1969+
ASSERT_EQUALS("", preprocess(code, &outputList));
1970+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/\"\n", toString(outputList));
1971+
}
1972+
1973+
static void missingHeader6()
1974+
{
1975+
// this is a directory
1976+
const char code[] = "#include \"/usr\"\n";
1977+
simplecpp::OutputList outputList;
1978+
ASSERT_EQUALS("", preprocess(code, &outputList));
1979+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/usr\"\n", toString(outputList));
1980+
}
1981+
1982+
static void missingHeader7()
1983+
{
1984+
// this is a directory
1985+
const char code[] = "#include </>\n";
1986+
simplecpp::OutputList outputList;
1987+
ASSERT_EQUALS("", preprocess(code, &outputList));
1988+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </>\n", toString(outputList));
1989+
}
1990+
1991+
static void missingHeader8()
1992+
{
1993+
// this is a directory
1994+
const char code[] = "#include </usr>\n";
1995+
simplecpp::OutputList outputList;
1996+
ASSERT_EQUALS("", preprocess(code, &outputList));
1997+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </usr>\n", toString(outputList));
1998+
}
1999+
#endif
2000+
19632001
static void nestedInclude()
19642002
{
19652003
const char code[] = "#include \"test.h\"\n";
@@ -3197,6 +3235,12 @@ int main(int argc, char **argv)
31973235
TEST_CASE(missingHeader2);
31983236
TEST_CASE(missingHeader3);
31993237
TEST_CASE(missingHeader4);
3238+
#ifndef _WIN32
3239+
TEST_CASE(missingHeader5);
3240+
TEST_CASE(missingHeader6);
3241+
TEST_CASE(missingHeader7);
3242+
TEST_CASE(missingHeader8);
3243+
#endif
32003244
TEST_CASE(nestedInclude);
32013245
TEST_CASE(systemInclude);
32023246

0 commit comments

Comments
 (0)