Skip to content

Commit 5aee295

Browse files
committed
do not treat directories like regular files in existence checks
1 parent fe50e8e commit 5aee295

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
@@ -117,6 +117,10 @@ int main(int argc, char **argv)
117117
std::cout << "error: could not open file '" << filename << "'" << std::endl;
118118
std::exit(1);
119119
}
120+
if (!simplecpp::isFile(filename)) {
121+
std::cout << "error: could not open file '" << filename << "' - not a regular file" << std::endl;
122+
std::exit(1);
123+
}
120124
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
121125
} else {
122126
rawtokens = new simplecpp::TokenList(filename,files,&outputList);

simplecpp.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,9 +3160,11 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
31603160
if (nonExistingFilesCache.contains(simplePath))
31613161
return ""; // file is known not to exist, skip expensive file open call
31623162
#endif
3163-
f.open(simplePath.c_str());
3164-
if (f.is_open())
3165-
return simplePath;
3163+
if (simplecpp::isFile(simplePath)) {
3164+
f.open(simplePath.c_str());
3165+
if (f.is_open())
3166+
return simplePath;
3167+
}
31663168
#ifdef SIMPLECPP_WINDOWS
31673169
nonExistingFilesCache.add(simplePath);
31683170
#endif
@@ -3297,18 +3299,19 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
32973299
if (ret.find(filename) != ret.end())
32983300
continue;
32993301

3300-
std::ifstream fin(filename.c_str());
3301-
if (!fin.is_open()) {
3302-
if (outputList) {
3303-
simplecpp::Output err(filenames);
3304-
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3305-
err.location = Location(filenames);
3306-
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3307-
outputList->push_back(err);
3302+
{
3303+
std::ifstream fin(filename.c_str());
3304+
if (!fin.is_open() || !isFile(filename)) {
3305+
if (outputList) {
3306+
simplecpp::Output err(filenames);
3307+
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
3308+
err.location = Location(filenames);
3309+
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3310+
outputList->push_back(err);
3311+
}
3312+
continue;
33083313
}
3309-
continue;
33103314
}
3311-
fin.close();
33123315

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

test.cpp

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

1947+
#ifndef _WIN32
1948+
static void missingHeader5()
1949+
{
1950+
// this is a directory
1951+
const char code[] = "#include \"/\"\n";
1952+
simplecpp::OutputList outputList;
1953+
ASSERT_EQUALS("", preprocess(code, &outputList));
1954+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/\"\n", toString(outputList));
1955+
}
1956+
1957+
static void missingHeader6()
1958+
{
1959+
// this is a directory
1960+
const char code[] = "#include \"/usr\"\n";
1961+
simplecpp::OutputList outputList;
1962+
ASSERT_EQUALS("", preprocess(code, &outputList));
1963+
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/usr\"\n", toString(outputList));
1964+
}
1965+
1966+
static void missingHeader7()
1967+
{
1968+
// this is a directory
1969+
const char code[] = "#include </>\n";
1970+
simplecpp::OutputList outputList;
1971+
ASSERT_EQUALS("", preprocess(code, &outputList));
1972+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </>\n", toString(outputList));
1973+
}
1974+
1975+
static void missingHeader8()
1976+
{
1977+
// this is a directory
1978+
const char code[] = "#include </usr>\n";
1979+
simplecpp::OutputList outputList;
1980+
ASSERT_EQUALS("", preprocess(code, &outputList));
1981+
ASSERT_EQUALS("file0,1,missing_header,Header not found: </usr>\n", toString(outputList));
1982+
}
1983+
#endif
1984+
19471985
static void nestedInclude()
19481986
{
19491987
const char code[] = "#include \"test.h\"\n";
@@ -3180,6 +3218,12 @@ int main(int argc, char **argv)
31803218
TEST_CASE(missingHeader2);
31813219
TEST_CASE(missingHeader3);
31823220
TEST_CASE(missingHeader4);
3221+
#ifndef _WIN32
3222+
TEST_CASE(missingHeader5);
3223+
TEST_CASE(missingHeader6);
3224+
TEST_CASE(missingHeader7);
3225+
TEST_CASE(missingHeader8);
3226+
#endif
31833227
TEST_CASE(nestedInclude);
31843228
TEST_CASE(systemInclude);
31853229

0 commit comments

Comments
 (0)